Python 插件杂谈 (4) ---- BeautifulSoup , Python中的网页分析工具

来源:互联网 发布:淘宝配色怎么改 编辑:程序博客网 时间:2024/05/16 08:29

  嗯哼,Meego中文核心站-- 米趣网 又发新博文啦。
    前面向大家介绍了 PyQuery    ,下面转而介绍一下 BeautifulSoup   , Beautiful Soup 是 Python 内置的网页分析工具,名字叫美丽的蝴蝶。呵呵,某些时候确如美丽蝴蝶一样。
    先来段介绍:
    Beautiful Soup 是一个 Python HTML/XML 处理器,设计用来快速地转换网页抓取。以下的特性支撑着 Beautiful Soup

  • Beautiful Soup 不会选择 即使你给他一个损坏的标签。 他产生一个转换DOM树,尽可能和你原文档内容含义一致 。这种措施通常能够你搜集数据的需求。
  • Beautiful Soup 提供一些简单的方法以及类Python语法 来查找、查找、修改一颗转换树:一个工具集帮助你解析一棵树并释出你需要的内容。你不需要为每一个应用创建自己的解析工具。
  • Beautiful Soup 自动将送进来的文档转换为 Unicode 编码 而且在输出的时候转换为 UTF-8,。 除非这个文档没有指定编码方式或者Beautiful Soup 没能自动检测编码,你需要手动指定编码方式,否则你不需要考虑编码的问题。

    Beautiful Soup 转换任何你给他的内容,然后为你做那些转换的事情。你可以命令他 “找出所有的链接", 或者 "找出所有 class 是 externalLink 的链接" , 再或者是 "找出所有的链接 url 匹配 ”foo.com", 甚至是 "找出那些表头是粗体文字,然后返回给我文字“.
    那些设计不好的网站中的有价值的数据可以被你一次锁定,原本要花数个小时候的工作,通过使用 Beautiful Soup 可以在几分钟内搞定。
    下面让我们快速开始:
     首先引用包:

  1. from BeautifulSoup import BeautifulSoup          # For processing HTML
  2. from BeautifulSoup import BeautifulStoneSoup     # For processing XML
  3. import BeautifulSoup                             # To get everything[/font][/color]
复制代码

   下面使用一段代码演示Beautiful Soup的基本使用方式。你可以拷贝与粘贴这段代码自己运行。

  1. from BeautifulSoup import BeautifulSoup
  2. import re
  3. doc = ['<html><head><title>Page title</title></head>',
  4.        '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
  5.        '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
  6.        '</html>']
  7. soup = BeautifulSoup(''.join(doc))
  8. print soup.prettify()
  9. # <html>
  10. #  <head>
  11. #   <title>
  12. #    Page title
  13. #   </title>
  14. #  </head>
  15. #  <body>
  16. #   <p id="firstpara" align="center">
  17. #    This is paragraph
  18. #    <b>
  19. #     one
  20. #    </b>
  21. #    .
  22. #   </p>
  23. #   <p id="secondpara" align="blah">
  24. #    This is paragraph
  25. #    <b>
  26. #     two
  27. #    </b>
  28. #    .
  29. #   </p>
  30. #  </body>
  31. # </html>
复制代码

下面是一个解析文档的方法:

  1. soup.contents[0].name
  2. # u'html'
  3. soup.contents[0].contents[0].name
  4. # u'head'
  5. head = soup.contents[0].contents[0]
  6. head.parent.name
  7. # u'html'
  8. head.next
  9. # <title>Page title</title>
  10. head.nextSibling.name
  11. # u'body'
  12. head.nextSibling.contents[0]
  13. # <p id="firstpara" align="center">This is paragraph <b>one</b>.</p>
  14. head.nextSibling.contents[0].nextSibling
  15. # <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>
复制代码

接着是一打方法查找一文档中包含的标签,或者含有指定属性的标签

  1. titleTag = soup.html.head.title
  2. titleTag
  3. # <title>Page title</title>
  4. titleTag.string
  5. # u'Page title'
  6. len(soup('p'))
  7. # 2
  8. soup.findAll('p', align="center")
  9. # [<p id="firstpara" align="center">This is paragraph <b>one</b>. </p>]
  10. soup.find('p', align="center")
  11. # <p id="firstpara" align="center">This is paragraph <b>one</b>. </p>
  12. soup('p', align="center")[0]['id']
  13. # u'firstpara'
  14. soup.find('p', align=re.compile('^b.*'))['id']
  15. # u'secondpara'
  16. soup.find('p').b.string
  17. # u'one'
  18. soup('p')[1].b.string
  19. # u'two'
复制代码

当然也可以简单地修改文档

  1. titleTag['id'] = 'theTitle'
  2. titleTag.contents[0].replaceWith("New title")
  3. soup.html.head
  4. # <head><title id="theTitle">New title</title></head>
  5. soup.p.extract()
  6. soup.prettify()
  7. # <html>
  8. #  <head>
  9. #   <title id="theTitle">
  10. #    New title
  11. #   </title>
  12. #  </head>
  13. #  <body>
  14. #   <p id="secondpara" align="blah">
  15. #    This is paragraph
  16. #    <b>
  17. #     two
  18. #    </b>
  19. #    .
  20. #   </p>
  21. #  </body>
  22. # </html>
  23. soup.p.replaceWith(soup.b)
  24. # <html>
  25. #  <head>
  26. #   <title id="theTitle">
  27. #    New title
  28. #   </title>
  29. #  </head>
  30. #  <body>
  31. #   <b>
  32. #    two
  33. #   </b>
  34. #  </body>
  35. # </html>
  36. soup.body.insert(0, "This page used to have ")
  37. soup.body.insert(2, " &lt;p&gt; tags!")
  38. soup.body
  39. # <body>This page used to have <b>two</b> &lt;p&gt; tags!</body>
复制代码

最后,为大家提供 Beautiful Soup 的文档。希望能对您有帮助。

转载文章 ,请注明来自  米趣网