像CSS选择器一样使用BeautifulSoup

jeff jie <[email protected]>
reply-to        [email protected]
to      [email protected],
[email protected]
date    Tue, Jun 16, 2009 at 11:04

共享一些代码片段,一直说要共享,一搁就大半年。。哎。。

常见使用方式是:

   1 from BTSelector import findAll
   2 soup = BeautifulSoup(htmlContent)
   3 nodes = findAll('div.navigator #notice',soup)
   4 # findAll返回的是符合选择条件的Dom 对象列表,实际上是BeautifulSoup的标签或字符类。

除了使用findAll函数,还可以直接使用findById,findByTag等函数,等有兴趣的同学看源代码来发现了。

用testCase作为使用示例:

一、上一个复杂一点的用例:

   1 def testMixSelection(self):
   2         target = "#header > div#name > a.highlight"
   3         html = '''
   4         <div id="header">
   5             <div id="name">
   6                 <a class="target">test</a>
   7                 <a class="highlight">right</a>
   8                 <a class="highlight">ok</a>
   9             </div>
  10             <div id="your">
  11             </div>
  12         </div>
  13         <div id="body">fk
  14         </div>
  15         '''
  16         soup = BeautifulSoup(html)
  17 
  18         ret = findAll(target,soup)
  19 
  20         self.assertEqual(2,len(ret))

二、再上一个使用位置操作符的用例:

   1 def testPosition(self):
   2 
   3         target = "h2 + ul > li > a"
   4 
   5         html = '''
   6 
   7         <h2>title</h2>
   8 
   9         <ul>
  10 
  11             <li><a href="#">nothing</a></li>
  12 
  13             <li><a href="#">ok</a></li>
  14 
  15             <li><a href="#">come on!</a></li>
  16 
  17         </ul>
  18 
  19         '''
  20 
  21         soup = BeautifulSoup(html)
  22 
  23         ret = findAll(target,soup)
  24 
  25         self.assertEqual(3,len(ret))

更多的见附件的Unittest。里面没太多技术含量的东西,但希望大家喜欢。

pyquery

金浩 <[email protected]>
reply-to        [email protected]
to      [email protected]
date    Tue, Jun 16, 2009 at 11:10
subject [CPyUG:89577] Re: 像CSS选择器一样使用BeautifulSoup

其实,这个东西类似应该已经有了。比如pyquery http://pyquery.org/

另外听很多人说 BeautifulSoup 效率非常差。

GAE定期任务实操

mutou majia <[email protected]>
reply-to        [email protected]
to      [email protected]
date    Tue, Jun 16, 2009 at 16:48
subject [CPyUG:89635] 请问我的Google appegine 的Cron任务怎么做不正常呢?

问题

自己手动打开那个url一切正常, 但cron自己行动这个url时, 总不正常。

手动输入url时,一切正常, Cron自己运行时却不行,

On time Failed。

fixed

找到原因了, 把url的处理函数不要用

(r'^tasks/', include('tasks.views')),
而要用
(r'^tasks/check', tasks.views.task_check),
才行。。。。

找的好辛苦。。。。 一周了,不知道问题在哪。。。。

不过还得感谢Google啊, 除他还无人提供实验田吧?


反馈

MiscItems/2009-06-16 (last edited 2009-12-25 07:15:17 by localhost)