简单来讲,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。官方解释以下:html
Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,经过解析文档为用户提供须要抓取的数据,由于简单,因此不须要多少代码就能够写出一个完整的应用程序。html5
Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不须要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了。而后,你仅仅须要说明一下原始编码方式就能够了。python
Beautiful Soup已成为和lxml、html6lib同样出色的python解释器,为用户灵活地提供不一样的解析策略或强劲的速度。正则表达式
Beautiful Soup 3 目前已经中止开发,推荐在如今的项目中使用Beautiful Soup 4,不过它已经被移植到BS4了,也就是说导入时咱们须要 from bs4 import BeautifulSoup 。因此这里咱们用的版本是 Beautiful Soup 4.3.2 (简称BS4)。express
一、快速安装浏览器
pip install beautifulsoup4
二、若是想安装最新的版本,请直接下载安装包来手动安装,也是十分方便的方法ide
一、Beautiful Soup3.2.1函数
https://pypi.python.org/pypi/BeautifulSoup/3.2.1工具
二、Beautiful Soup4.3.2ui
https://pypi.python.org/pypi/beautifulsoup4/
下载完成以后解压
运行下面的命令便可完成安装
python setup.py install
三、而后须要安装 lxml
pip install lxml
另外一个可供选择的解析器是纯Python实现的 html5lib , html5lib的解析方式与浏览器相同,能够选择下列方法来安装html5lib:
pip install html5lib
Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,若是咱们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更增强大,速度更快,推荐安装。
官方文档:http://beautifulsoup.readthedocs.io/zh_CN/latest/
一、导入
from bs4 import BeautifulSoup
二、咱们首先建立一个html文件,为了模拟下面的操做。
html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></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>; and they lived at the bottom of a well.</p> <p class="story">...</p> """
三、建立 beautifulsoup 对象
soup = BeautifulSoup(html)
另外,咱们还能够打开本地的html文件。
soup = BeautifulSoup(open('index.html'))
四、格式化输入
soup = BeautifulSoup(html,"lxml") print(soup.prettify())
输出:
<html> <head> <title> The Dormouse's story </title> </head>
这个颇有用的哦,若是咱们要分析本地的html文件没有格式化输出的时候,看起来就很是乱了,因此咱们须要格式化输入后咱们就能一目了然这个html文件的结构。
五、四大对象种类
Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每一个节点都是Python对象,全部对象能够概括为4种
tag是什么鬼,tag中文意思是标签的意思,学过html的同窗确定明白,标签例如<a href="https://www.baidu.com">my name a</a>
感觉一下tag的用法
print(soup.title) #<title>The Dormouse's story</title>
print(soup.head) #<head><title>The Dormouse's story</title></head>
细心的同窗会发现,我有不少p标签,可是只能打印到从上往下的第一个匹配到的标签
print(soup.p) #<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
print(soup.a) #<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>
soup = BeautifulSoup(html,"lxml") print(soup.p) #<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
tag还有两个经常使用的属性,name和attrs
name:
soup = BeautifulSoup(html,"lxml") print(soup.name) print(soup.head.name) #[document] #head
attrs:
soup = BeautifulSoup(html,"lxml") print(soup.p.attrs) #{'name': 'dromouse', 'class': ['title']}
获取属性值的两种不一样方法:
soup = BeautifulSoup(html,"lxml") print(soup.p.attrs) print(soup.p.get("class")) print(soup.p["class"]) #{'class': ['title'], 'name': 'dromouse'} #['title'] #['title']
能够获取,固然也能够修改和删除
修改:
soup = BeautifulSoup(html,"lxml") print(soup.p) soup.p["class"]="newclass" print(soup.p) #<p class="title" name="dromouse"><b>The Dormouse's story</b></p> #<p class="newclass" name="dromouse"><b>The Dormouse's story</b></p>
删除:
soup = BeautifulSoup(html,"lxml") print(soup.p) del soup.p["class"] print(soup.p) #<p class="title" name="dromouse"><b>The Dormouse's story</b></p> #<p name="dromouse"><b>The Dormouse's story</b></p>
(2)NavigableString
一、咱们已经经过tag方法找到标签,可是若是想找某个标签的内容怎么办。
soup = BeautifulSoup(html,"lxml") print(soup.p.string) #The Dormouse's story
(3)BeautifulSoup
对象表示的是一个文档的所有内容.大部分时候,能够把它看成 Tag 对象,是一个特殊的 Tag,咱们能够分别获取它的类型,名称,以及属性。
soup = BeautifulSoup(html,"lxml") print(type(soup.name)) print(soup.name) print(soup.attrs) #<class 'str'> #[document] #{}
(4)Comment
Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号,可是若是很差好处理它,可能会对咱们的文本处理形成意想不到的麻烦。
soup = BeautifulSoup(html,"lxml") print(soup.a) print(soup.a.string) print(type(soup.a.string)) #<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a> #Elsie #<class 'bs4.element.Comment'>
a 标签里的内容其实是注释,可是若是咱们利用 .string 来输出它的内容,咱们发现它已经把注释符号去掉了,因此这可能会给咱们带来没必要要的麻烦。
另外咱们打印输出下它的类型,发现它是一个 Comment 类型,因此,咱们在使用前最好作一下判断,判断代码以下
import bs4 soup = BeautifulSoup(html,"lxml") if type(soup.a.string)==bs4.element.Comment: print(soup.a.string)
(6)遍历文档树
contents和children的区别
一、contents
tag 的 .content 属性能够将tag的子节点以列表的方式输出
soup = BeautifulSoup(html,"lxml") print(soup.p.contents) #[<b>The Dormouse's story</b>]
列表的话咱们就能够经过下标取里面值
soup = BeautifulSoup(html,"lxml") print(soup.p.contents[0]) #<b>The Dormouse's story</b>
二、children
它返回的不是一个 list,不过咱们能够经过遍历获取全部子节点。
咱们打印输出 .children 看一下,能够发现它是一个 list 生成器对象
soup = BeautifulSoup(html,"lxml") print(soup.p.children) #<list_iterator object at 0x01BAE310>
list能够经过for循环遍历取值
soup = BeautifulSoup(html,"lxml") print(soup.p.children) for line in soup.p.children: print(line)
三、全部子孙节点(.descendants)
.contents 和 .children 属性仅包含tag的直接子节点.例如,<head>标签只有一个直接子节点<title>
.descendants
soup = BeautifulSoup(html,"lxml") for line in soup.descendants: print(line)
children和contents只会把html文件打印一遍,只是children须要用for循环遍历一下而已,可是descendantes会把html中每个tag都遍历一遍的前提是子子孙孙都会遍历一下(有些朋友可能仍是有点不明白,直接上代码你就懂了)
<html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie<span>Test<a>TEST</a></span></a>; and they lived at the bottom of a well.</p> <p class="story">...</p> </body></html> <head><title>The Dormouse's story</title></head> <title>The Dormouse's story</title> The Dormouse's story <body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie<span>Test<a>TEST</a></span></a>; and they lived at the bottom of a well.</p> <p class="story">...</p> </body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <b>The Dormouse's story</b> The Dormouse's story <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie<span>Test<a>TEST</a></span></a>; and they lived at the bottom of a well.</p> Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a> Elsie , <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> Lacie and <a class="sister" href="http://example.com/tillie" id="link3">Tillie<span>Test<a>TEST</a></span></a> Tillie <span>Test<a>TEST</a></span> Test <a>TEST</a> TEST ; and they lived at the bottom of a well. <p class="story">...</p> ...
四、节点内容(string)
通俗点说就是:若是一个标签里面没有标签了,那么 .string 就会返回标签里面的内容。若是标签里面只有惟一的一个标签了,那么 .string 也会返回最里面的内容(若是标签里面有不少不少的内容,它就不知道该找谁了,结果返回一个None)
soup = BeautifulSoup(html,"lxml") print(soup.head.string) print(soup.title.string) #The Dormouse's story #The Dormouse's story
若是标签下有不少的内容,返回的就是None了
soup = BeautifulSoup(html,"lxml") print(soup.html.string) #None
可能有些同窗会说,不要紧啊,内容多了你能够用for循环遍历一下不就成了么,那咱们来试试。
结果:报错了
还能够这么说,好比,你找到了一个a标签,可是这个a标签有本身的内容,a标签下面还有一个a标签或者别的标签,这个a标签也有本身的内容,这个时候你要是用string的话确定是None。
soup = BeautifulSoup(html,"lxml") for line in soup.html.string: print(line) #TypeError: 'NoneType' object is not iterable
五、多个内容
.strings .stripped_strings
strings
获取多个内容,不过须要遍历获取,好比下面的例子
soup = BeautifulSoup(html,"lxml") for line in soup.strings: print(repr(line))
"The Dormouse's story" '\n' '\n' "The Dormouse's story" '\n' 'Once upon a time there were three little sisters; and their names were\n' ',\n' 'Lacie' ' and\n' 'Tillie' 'Test' 'TEST' ';\nand they lived at the bottom of a well.' '\n' '...' '\n'
stripped_strings
输出的字符串中可能包含了不少空格或空行,使用 .stripped_strings 能够去除多余空白内容
soup = BeautifulSoup(html,"lxml") for line in soup.stripped_strings: print(repr(line))
"The Dormouse's story" "The Dormouse's story" 'Once upon a time there were three little sisters; and their names were' ',' 'Lacie' 'and' 'Tillie' 'Test' 'TEST' ';\nand they lived at the bottom of a well.' '...'
六、父节点
.parent 属性
其实就是把当前要找的标签的上一级标签打印出来(注意,会把上一级标签的全部子标签都打印回来)
例一:
soup = BeautifulSoup(html,"lxml") print(soup.title.parent) #<head><title>The Dormouse's story</title></head>
例二:
soup = BeautifulSoup(html,"lxml") print(soup.p.parent)
结果:
<body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie<span>Test<a>TEST</a></span></a>; and they lived at the bottom of a well.</p> <p class="story">...</p> </body>
七、所有父节点
.parents 属性
soup = BeautifulSoup(html,"lxml") content = soup.head.title.string for parent in content.parents: print(parent.name)
结果你会发现,parents是递归查找到你指定标签的父标签名字,指定标签的爷爷标签,太爷爷标签,就这么一直找下去。(若是你想看一下你找到这个标签是具体在什么位置,能够这么找)
title
head
html
[document]
八、兄弟标签
.next_sibling .previous_sibling 属性
兄弟节点能够理解为和本节点处在统一级的节点,.next_sibling 属性获取了该节点的下一个兄弟节点,.previous_sibling 则与之相反,若是节点不存在,则返回 None
注意:实际文档中的tag的 .next_sibling 和 .previous_sibling 属性一般是字符串或空白,由于空白或者换行也能够被视做一个节点,因此获得的结果多是空白或者换行
一、next_sibling
soup = BeautifulSoup(html,"lxml") print(soup.p.next_sibling.next_sibling)
<p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie<span>Test<a>TEST</a></span></a>; and they lived at the bottom of a well.</p>
二、previous_sibling
soup = BeautifulSoup(html,"lxml") print(soup.p.previous_sibling.previous_sibling)
#None
九、所有兄弟节点
.next_siblings .previous_siblings 属性
一、next_siblings
打印了初当前标签的全部子标签。(能够找form表单)
soup = BeautifulSoup(html,"lxml") for a in soup.a.next_siblings: print(a)
结果:
, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie<span>Test<a>TEST</a></span></a> ; and they lived at the bottom of a well.
十、先后节点
.next_element .previous_element 属性
与 .next_sibling .previous_sibling 不一样,它并非针对于兄弟节点,而是在全部节点,不分层次
注意:咱们说的前一个或者下一个其实除了标签外标签内的内容其实也是一个属性
一、next_element(后)
soup = BeautifulSoup(html,"lxml") print(soup.p.next_element ) #<b>The Dormouse's story</b>
二、.previous_sibling(前)
html中第一个a标签的前一个标签的是html中第二个p标签(这个p标签的兄弟姐妹,不是全部)
soup = BeautifulSoup(html,"lxml") print(soup.a.previous_sibling ) #Once upon a time there were three little sisters; and their names were
十一、搜索文档树
find_all() 方法搜索当前tag的全部tag子节点,并判断是否符合过滤器的条件
1)name 参数
name参数能够查找全部名字为 name 的tag,字符串对象会被自动忽略
A)传个字符串看看以列表的形式找出全部a标签
soup = BeautifulSoup(html,"lxml") print(soup.find_all('a') ) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
B)正则表达式
若是传入正则表达式做为参数,Beautiful Soup会经过正则表达式的 match() 来匹配内容.下面例子中找出全部以b开头的标签,这表示<body>和<b>标签都应该被找到
soup = BeautifulSoup(html,"lxml") print(soup.find_all(re.compile('^a')) )
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
C)传个列表看看
soup = BeautifulSoup(html,"lxml") print(soup.find_all(['a','p']) )
结果:
[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>, <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; and they lived at the bottom of a well.</p>, <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>, <p class="story">...</p>]
D)传 True
若是没有合适过滤器,那么还能够定义一个方法,方法只接受一个元素参数 [4] ,若是这个方法返回 True 表示当前元素匹配而且被找到,若是不是则反回 False
下面方法校验了当前元素,若是包含 class 属性却不包含 id 属性,那么将返回 True:
def has_class_but_no_id(tag): return tag.has_attr('class') and not tag.has_attr('id')
将这个方法做为参数传入 find_all() 方法,将获得全部<p>标签:
[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>, <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; and they lived at the bottom of a well.</p>, <p class="story">...</p>]
二、keyword 参数
注意:若是一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数看成指定名字tag的属性来搜索,若是包含一个名字为 id 的参数,Beautiful Soup会搜索每一个tag的”id”属性
soup = BeautifulSoup(html,"lxml") print(soup.find_all(id='link2')) #[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
上正则表达式
soup = BeautifulSoup(html,"lxml") print(soup.find_all(href=re.compile('elsie'))) # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
还能够同时传多个属性参数进行进准查找
soup = BeautifulSoup(html,"lxml") print(soup.find_all(href=re.compile("elsie"), id='link1')) # [<a class="sister" href="http://example.com/elsie" id="link1">three</a>]
在这里咱们想用 class 过滤,不过 class 是 python 的关键词,这怎么办?加个下划线就能够
soup = BeautifulSoup(html,"lxml") print(soup.find_all("a", class_="sister")) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
有些tag属性在搜索不能使用,好比HTML5中的 data-* 属性
data_soup = BeautifulSoup('<div data-foo="value">foo!</div>') data_soup.find_all(data-foo="value") # SyntaxError: keyword can't be an expression
三、text参数
soup = BeautifulSoup(html,"lxml") print(soup.find_all(text=" Elsie ")) # [' Elsie '] print(soup.find_all(text=["Tillie", " Elsie ", "Lacie"])) #[' Elsie ', 'Lacie', 'Tillie'] print(soup.find_all(text=re.compile("Dormouse"))) #["The Dormouse's story", "The Dormouse's story"]
四、limit参数
find_all() 方法返回所有的搜索结构,若是文档树很大那么搜索会很慢.若是咱们不须要所有结果,可使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字相似,当搜索到的结果数量达到 limit 的限制时,就中止搜索返回结果.
文档树中有3个tag符合搜索条件,但结果只返回了2个,由于咱们限制了返回数量
soup = BeautifulSoup(html,"lxml") print(soup.find_all('a',limit=2)) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
五、recursive 参数
soup = BeautifulSoup(html,"lxml") print(soup.find_all('p')) print(soup.find_all('p',recursive=False)) [<p class="title" name="dromouse"><b>The Dormouse's story</b></p>, <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; and they lived at the bottom of a well.</p>, <p class="story">...</p>] []
它与 find_all() 方法惟一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果
soup = BeautifulSoup(html,"lxml") print(soup.find_all('a')) print(soup.find('a')) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] #<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>
这2个方法经过 .next_siblings 属性对当 tag 的全部后面解析的兄弟 tag 节点进行迭代, find_next_siblings() 方法返回全部符合条件的后面的兄弟节点,find_next_sibling() 只返回符合条件的后面的第一个tag节点
这2个方法经过 .previous_siblings 属性对当前 tag 的前面解析的兄弟 tag 节点进行迭代, find_previous_siblings()方法返回全部符合条件的前面的兄弟节点, find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点
这2个方法经过 .next_elements 属性对当前 tag 的以后的 tag 和字符串进行迭代, find_all_next() 方法返回全部符合条件的节点, find_next() 方法返回第一个符合条件的节点
这2个方法经过 .previous_elements 属性对当前节点前面的 tag 和字符串进行迭代, find_all_previous() 方法返回全部符合条件的节点, find_previous()方法返回第一个符合条件的节点
注:以上(2)(3)(4)(5)(6)(7)方法参数用法与 find_all() 彻底相同,原理均相似,在此再也不赘
十二、CSS选择器
咱们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里咱们也能够利用相似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list
soup = BeautifulSoup(html,"lxml") print(soup.select('title')) #[<title>The Dormouse's story</title>]
soup = BeautifulSoup(html,"lxml") print(soup.select('a')) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup = BeautifulSoup(html,"lxml") print(soup.select('b')) #[<b>The Dormouse's story</b>]
soup = BeautifulSoup(html,"lxml") print(soup.select('.sister')) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup = BeautifulSoup(html,"lxml") print(soup.select('#link1')) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是同样的,例如查找 p 标签中,id 等于 link1的内容,两者须要用空格分开
soup = BeautifulSoup(html,"lxml") print(soup.select('p #link1')) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
查找时还能够加入属性元素,属性须要用中括号括起来,注意属性和标签属于同一节点,因此中间不能加空格,不然会没法匹配到。
soup = BeautifulSoup(html,"lxml") print(soup.select('a[class="sister"]')) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup = BeautifulSoup(html,"lxml") print(soup.select('a[href="http://example.com/elsie"]')) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
一样,属性仍然能够与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格
soup = BeautifulSoup(html,"lxml") print(soup.select('p a[href="http://example.com/elsie"]')) #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
以上的 select 方法返回的结果都是列表形式,能够遍历形式输出,而后用 get_text() 方法来获取它的内容。
soup = BeautifulSoup(html, 'lxml') print(type(soup.select('title'))) #<class 'list'> print(soup.select('title')[0].get_text()) #The Dormouse's story for title in soup.select('title'): print(title.get_text()) #The Dormouse's story