一 介绍

Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,官网推荐在现在的项目中使用Beautiful Soup 4, 移植到BS4

  1. #安装 Beautiful Soup
  2. pip install beautifulsoup4
  3. #安装解析器
  4. Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml .根据操作系统不同,可以选择下列方法来安装lxml:
  5. $ apt-get install Python-lxml
  6. $ easy_install lxml
  7. $ pip install lxml
  8. 另一个可供选择的解析器是纯Python实现的 html5lib , html5lib的解析方式与浏览器相同,可以选择下列方法来安装html5lib:
  9. $ apt-get install Python-html5lib
  10. $ easy_install html5lib
  11. $ pip install html5lib

下表列出了主要的解析器,以及它们的优缺点,官网推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前的版本,必须安装lxml或html5lib, 因为那些Python版本的标准库中内置的HTML解析方法不够稳定.

解析器 使用方法 优势 劣势
Python标准库 BeautifulSoup(markup, “html.parser”)
- Python的内置标准库
- 执行速度适中
- 文档容错能力强

- Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差
lxml HTML 解析器 BeautifulSoup(markup, “lxml”)
- 速度快
- 文档容错能力强

- 需要安装C语言库
lxml XML 解析器 BeautifulSoup(markup, [“lxml”, “xml”])
BeautifulSoup(markup, “xml”)

- 速度快
- 唯一支持XML的解析器

- 需要安装C语言库
html5lib BeautifulSoup(markup, “html5lib”)
- 最好的容错性
- 以浏览器的方式解析文档
- 生成HTML5格式的文档

- 速度慢
- 不依赖外部扩展

中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

二 基本使用

  1. html_doc = """
  2. <html><head><title>The Dormouse's story</title></head>
  3. <body>
  4. <p class="title"><b>The Dormouse's story</b></p>
  5. <p class="story">Once upon a time there were three little sisters; and their names were
  6. <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
  7. <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
  8. <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
  9. and they lived at the bottom of a well.</p>
  10. <p class="story">...</p>
  11. """
  12. #基本使用:容错处理,文档的容错能力指的是在html代码不完整的情况下,使用该模块可以识别该错误。使用BeautifulSoup解析上述代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出
  13. from bs4 import BeautifulSoup
  14. soup=BeautifulSoup(html_doc,'lxml') #具有容错功能
  15. res=soup.prettify() #处理好缩进,结构化显示
  16. print(res)

三 遍历文档树

  1. #遍历文档树:即直接通过标签名字选择,特点是选择速度快,但如果存在多个相同的标签则只返回第一个
  2. #1、用法
  3. #2、获取标签的名称
  4. #3、获取标签的属性
  5. #4、获取标签的内容
  6. #5、嵌套选择
  7. #6、子节点、子孙节点
  8. #7、父节点、祖先节点
  9. #8、兄弟节点
  1. #遍历文档树:即直接通过标签名字选择,特点是选择速度快,但如果存在多个相同的标签则只返回第一个
  2. html_doc = """
  3. <html><head><title>The Dormouse's story</title></head>
  4. <body>
  5. <p id="my p" class="title"><b id="bbb" class="boldest">The Dormouse's story</b></p>
  6. <p class="story">Once upon a time there were three little sisters; and their names were
  7. <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
  8. <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
  9. <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
  10. and they lived at the bottom of a well.</p>
  11. <p class="story">...</p>
  12. """
  13. #1、用法
  14. from bs4 import BeautifulSoup
  15. soup=BeautifulSoup(html_doc,'lxml')
  16. # soup=BeautifulSoup(open('a.html'),'lxml')
  17. print(soup.p) #存在多个相同的标签则只返回第一个
  18. print(soup.a) #存在多个相同的标签则只返回第一个
  19. #2、获取标签的名称
  20. print(soup.p.name)
  21. #3、获取标签的属性
  22. print(soup.p.attrs)
  23. #4、获取标签的内容
  24. print(soup.p.string) # p下的文本只有一个时,取到,否则为None
  25. print(soup.p.strings) #拿到一个生成器对象, 取到p下所有的文本内容
  26. print(soup.p.text) #取到p下所有的文本内容
  27. for line in soup.stripped_strings: #去掉空白
  28. print(line)
  29. '''
  30. 如果tag包含了多个子节点,tag就无法确定 .string 方法应该调用哪个子节点的内容, .string 的输出结果是 None,如果只有一个子节点那么就输出该子节点的文本,比如下面的这种结构,soup.p.string 返回为None,但soup.p.strings就可以找到所有文本
  31. <p id='list-1'>
  32. 哈哈哈哈
  33. <a class='sss'>
  34. <span>
  35. <h1>aaaa</h1>
  36. </span>
  37. </a>
  38. <b>bbbbb</b>
  39. </p>
  40. '''
  41. #5、嵌套选择
  42. print(soup.head.title.string)
  43. print(soup.body.a.string)
  44. #6、子节点、子孙节点
  45. print(soup.p.contents) #p下所有子节点
  46. print(soup.p.children) #得到一个迭代器,包含p下所有子节点
  47. for i,child in enumerate(soup.p.children):
  48. print(i,child)
  49. print(soup.p.descendants) #获取子孙节点,p下所有的标签都会选择出来
  50. for i,child in enumerate(soup.p.descendants):
  51. print(i,child)
  52. #7、父节点、祖先节点
  53. print(soup.a.parent) #获取a标签的父节点
  54. print(soup.a.parents) #找到a标签所有的祖先节点,父亲的父亲,父亲的父亲的父亲...
  55. #8、兄弟节点
  56. print('=====>')
  57. print(soup.a.next_sibling) #下一个兄弟
  58. print(soup.a.previous_sibling) #上一个兄弟
  59. print(list(soup.a.next_siblings)) #下面的兄弟们=>生成器对象
  60. print(soup.a.previous_siblings) #上面的兄弟们=>生成器对象

四 搜索文档树

1、五种过滤器

  1. #搜索文档树:BeautifulSoup定义了很多搜索方法,这里着重介绍2个: find() 和 find_all() .其它方法的参数和用法类似
  2. html_doc = """
  3. <html><head><title>The Dormouse's story</title></head>
  4. <body>
  5. <p id="my p" class="title"><b id="bbb" class="boldest">The Dormouse's story</b>
  6. </p>
  7. <p class="story">Once upon a time there were three little sisters; and their names were
  8. <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
  9. <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
  10. <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
  11. and they lived at the bottom of a well.</p>
  12. <p class="story">...</p>
  13. """
  14. from bs4 import BeautifulSoup
  15. soup=BeautifulSoup(html_doc,'lxml')
  16. #1、五种过滤器: 字符串、正则表达式、列表、True、方法
  17. #1.1、字符串:即标签名
  18. print(soup.find_all('b'))
  19. #1.2、正则表达式
  20. import re
  21. print(soup.find_all(re.compile('^b'))) #找出b开头的标签,结果有body和b标签
  22. #1.3、列表:如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有<a>标签和<b>标签:
  23. print(soup.find_all(['a','b']))
  24. #1.4、True:可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点
  25. print(soup.find_all(True))
  26. for tag in soup.find_all(True):
  27. print(tag.name)
  28. #1.5、方法:如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数 ,如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False
  29. def has_class_but_no_id(tag):
  30. return tag.has_attr('class') and not tag.has_attr('id')
  31. print(soup.find_all(has_class_but_no_id))

2、find_all( name , attrs , recursive , text , **kwargs )

  1. #2、find_all( name , attrs , recursive , text , **kwargs )
  2. #2.1、name: 搜索name参数的值可以使任一类型的 过滤器 ,字符窜,正则表达式,列表,方法或是 True .
  3. print(soup.find_all(name=re.compile('^t')))
  4. #2.2、keyword: key=value的形式,value可以是过滤器:字符串 , 正则表达式 , 列表, True .
  5. print(soup.find_all(id=re.compile('my')))
  6. print(soup.find_all(href=re.compile('lacie'),id=re.compile('\d'))) #注意类要用class_
  7. print(soup.find_all(id=True)) #查找有id属性的标签
  8. # 有些tag属性在搜索不能使用,比如HTML5中的 data-* 属性:
  9. data_soup = BeautifulSoup('<div data-foo="value">foo!</div>','lxml')
  10. # data_soup.find_all(data-foo="value") #报错:SyntaxError: keyword can't be an expression
  11. # 但是可以通过 find_all() 方法的 attrs 参数定义一个字典参数来搜索包含特殊属性的tag:
  12. print(data_soup.find_all(attrs={"data-foo": "value"}))
  13. # [<div data-foo="value">foo!</div>]
  14. #2.3、按照类名查找,注意关键字是class_,class_=value,value可以是五种选择器之一
  15. print(soup.find_all('a',class_='sister')) #查找类为sister的a标签
  16. print(soup.find_all('a',class_='sister ssss')) #查找类为sister和sss的a标签,顺序错误也匹配不成功
  17. print(soup.find_all(class_=re.compile('^sis'))) #查找类为sister的所有标签
  18. #2.4、attrs
  19. print(soup.find_all('p',attrs={'class':'story'}))
  20. #2.5、text: 值可以是:字符,列表,True,正则
  21. print(soup.find_all(text='Elsie'))
  22. print(soup.find_all('a',text='Elsie'))
  23. #2.6、limit参数:如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果
  24. print(soup.find_all('a',limit=2))
  25. #2.7、recursive:调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False .
  26. print(soup.html.find_all('a'))
  27. print(soup.html.find_all('a',recursive=False))
  28. '''
  29. 像调用 find_all() 一样调用tag
  30. find_all() 几乎是Beautiful Soup中最常用的搜索方法,所以我们定义了它的简写方法. BeautifulSoup 对象和 tag 对象可以被当作一个方法来使用,这个方法的执行结果与调用这个对象的 find_all() 方法相同,下面两行代码是等价的:
  31. soup.find_all("a")
  32. soup("a")
  33. 这两行代码也是等价的:
  34. soup.title.find_all(text=True)
  35. soup.title(text=True)
  36. '''

3、find( name , attrs , recursive , text , **kwargs )

#3、find( name , attrs , recursive , text , **kwargs )
find_all() 方法将返回文档中符合条件的所有tag,尽管有时候我们只想得到一个结果.比如文档中只有一个<body>标签,那么使用 find_all() 方法来查找<body>标签就不太合适, 使用 find_all 方法并设置 limit=1 参数不如直接使用 find() 方法.下面两行代码是等价的:

soup.find_all('title', limit=1)
# [<title>The Dormouse's story</title>]
soup.find('title')
# <title>The Dormouse's story</title>

唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果.
find_all() 方法没有找到目标是返回空列表, find() 方法找不到目标时,返回 None .
print(soup.find("nosuchtag"))
# None

soup.head.title 是 tag的名字 方法的简写.这个简写的原理就是多次调用当前tag的 find() 方法:

soup.head.title
# <title>The Dormouse's story</title>
soup.find("head").find("title")
# <title>The Dormouse's story</title>

4、其他方法

#见官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-parents-find-parent

5、CSS选择器

#该模块提供了select方法来支持css,详见官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id37
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title">
    <b>The Dormouse's story</b>
    Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1">
        <span>Elsie</span>
    </a>
    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    <div class='panel-1'>
        <ul class='list' id='list-1'>
            <li class='element'>Foo</li>
            <li class='element'>Bar</li>
            <li class='element'>Jay</li>
        </ul>
        <ul class='list list-small' id='list-2'>
            <li class='element'><h1 class='yyyy'>Foo</h1></li>
            <li class='element xxx'>Bar</li>
            <li class='element'>Jay</li>
        </ul>
    </div>
    and they lived at the bottom of a well.
</p>
<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup=BeautifulSoup(html_doc,'lxml')

#1、CSS选择器
print(soup.p.select('.sister'))
print(soup.select('.sister span'))

print(soup.select('#link1'))
print(soup.select('#link1 span'))

print(soup.select('#list-2 .element.xxx'))

print(soup.select('#list-2')[0].select('.element')) #可以一直select,但其实没必要,一条select就可以了

# 2、获取属性
print(soup.select('#list-2 h1')[0].attrs)

# 3、获取内容
print(soup.select('#list-2 h1')[0].get_text())

五 修改文档树

链接https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id40

六 总结

# 总结:
#1、推荐使用lxml解析库
#2、讲了三种选择器:标签选择器,find与find_all,css选择器
    1、标签选择器筛选功能弱,但是速度快
    2、建议使用find,find_all查询匹配单个结果或者多个结果
    3、如果对css选择器非常熟悉建议使用select
#3、记住常用的获取属性attrs和文本值get_text()的方法