做救助流浪动物网站的产生背景,软件外包什么意思,网站官方认证怎么做,jcms内容管理系统目录
1.简介
2.准备工作
3.解析器
4.基本用法
5.节点选择器
5.1选择元素
5.2提取信息
5.3嵌套选择
5.4关联选择
6.方法选择器
7. css 选择器
7.1 嵌套选择
7.2 获取属性
7.3 获取文本
总结 1.简介 简单来说#xff0c;Beautiful Soup就是Python的一个HTML或XM…目录
1.简介
2.准备工作
3.解析器
4.基本用法
5.节点选择器
5.1选择元素
5.2提取信息
5.3嵌套选择
5.4关联选择
6.方法选择器
7. css 选择器
7.1 嵌套选择
7.2 获取属性
7.3 获取文本
总结 1.简介 简单来说Beautiful Soup就是Python的一个HTML或XML的解析库可以用它来方便地从网 页中提取数据。官方解释如下 Beautiful Soup提供一些简单的、Python式的函数来处理导航、搜索、修改分析树等功能。 它是一个工具箱通过解析文档为用户提供需要抓取的数据因为简单所以不需要多少代 码就可以写出一个完整的应用程序。
Beautiful Soup自动将输入文档转换为Unicode编码输出文档转换为UTF-8编码。你不 需要考虑编码方式除非文档没有指定一个编码方式这时你仅仅需要说明一下原始编码方 式就可以了。 Beautiful Soup已成为和lxml、html61ib 一样出色的Python解释器为用户灵活地提供不 同的解析策略或强劲的速度。 所以说利用它可以省去很多烦琐的提取工作提高了解析效率。
所以说 利用它可以省去很多烦琐的提取工作提高了解析效率。
2.准备工作
在开始之前请确保已经正确安装好了 Beautiful Soup和lxml 如果没有安装 可以参考。3.解析器
Beautiful Soup 在解析时实际上依赖解析器它除了支持 Python 标准库中的 HTML 解析器外还
支持一些第三方解析器比如 lxml。通过以上对比可以看出lxml解析器有解析HTML和XML的功能而且速度快容错能力强 所以推荐使用它。 如果使用lxml,那么在初始化Beautiful Soup时可以把第二个参数改为lxml即可
from bs4 import BeautifulSoup
soup BeautifulSoup(pHello/p, lxml)
print(soup.p.string)
返回 4.基本用法
下面首先用实例来看看Beautiful Soup的基本用法
html
htmlheadtitleThe Dormouses story/title/head
body
p classtitle namedromousexbThe Dormouses story/bx/p
p classstoryOnce upon a time there were three little sisters; and their names were
a hrefhttp://example.com/elsie classsister idlinkl!-- Elsie --/a,
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.prettify())
print(soup.title.string)
返回 这里首先声明变量html它是一个HTML字符串。但是需要注意的是它并不是一个完整的HTML 字符串因为body和html节点都没有闭合。接着我们将它当作第一个参数传给BeautifulSoup对象该对象的第二个参数为解析器的类型(这里使用lxml),此时就完成了 BeaufulSoup对象的初始化。 然后将这个对象赋值给soup变量。 接下来就可以调用soup的各个方法和属性解析这串HTML代码了。 首先调用prettify()方法。这个方法可以把要解析的字符串以标准的缩进格式输出。这里需要 注意的是输出结果里面包含body和html节点也就是说对于不标准的HTML字符串BeautifulSoup, 可以自动更正格式。这一步不是由prettify()方法做的而是在初始化BeautifulSoup时就完成了。 然后调用soup.title.string,这实际上是输出HTML中title节点的文本内容。所以,soup.title 可以选出HTML中的title节点再调用string属性就可以得到里面的文本了所以我们可以通过 简单调用几个属性完成文本提取。
5.节点选择器 直接调用节点的名称就可以选择节点元素再调用string属性就可以得到节点内的文本了这种选择方式速度非常快。如果单个节点结构层次非常清晰可以选用这种方式来解析。
5.1选择元素
下面再用一个例子详细说明选择元素的方法:
html
htmlheadtitleThe Dormouses story/title/head
body
p classtitle namedromousebThe Dormouses story/b/p
p classstoryOnce upon a time there were three little sisters;and their names were
a hrefhttp://example.com/elsie classsister idlinkl!-- Elsie --/a,
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.title)
print(type(soup.title))
print(soup.title.string)
print(soup.head)
print(soup.p)
返回 这里依然选用刚才的HTML代码首先打印输出title节点的选择结果输出结果正是title节 点加里面的文字内容。接下来输出它的类型是bs4.element.Tag类型这是Beautiful Soup中一个 重要的数据结构。经过选择器选择后选择结果都是这种Tag类型。Tag具有一些属性比如string 属性调用该属性可以得到节点的文本内容所以接下来的输出结果正是节点的文本内容。 接下来我们又尝试选择了 head节点结果也是节点加其内部的所有内容。最后选择了 p节点。 不过这次情况比较特殊我们发现结果是第一个p节点的内容后面的几个p节点并没有选到。也就 是说当有多个节点时这种选择方式只会选择到第一个匹配的节点其他的后面节点都会忽略。
5.2提取信息 上面演示了调用string属性来获取文本的值那么如何获取节点属性的值呢如何获取节点名 呢下面我们来统一梳理一下信息的提取方式。
(1)获取名称 可以利用name属性获取节点的名称。这里还是以上面的文本为例选取title节点然后调用 name属性就可以得到节点名称
html
htmlheadtitleThe Dormouses story/title/head
body
p classtitle namedromousebThe Dormouses story/b/p
p classstoryOnce upon a time there were three little sisters;and their names were
a hrefhttp://example.com/elsie classsister idlinkl!-- Elsie --/a,
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.title.name)
返回
(2) 获取属性 每个节点可能有多个属性比如id和class等选择这个节点元素后可以调用attrs获取所有 属性
html
htmlheadtitleThe Dormouses story/title/head
body
p classtitle namedromousebThe Dormouses story/b/p
p classstoryOnce upon a time there were three little sisters;and their names were
a hrefhttp://example.com/elsie classsister idlinkl!-- Elsie --/a,
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.p.attrs)
print(soup.p.attrs[name])
返回 可以看到attrs的返回结果是字典形式它把选择的节点的所有属性和属性值组合成一个字典 接下来如果要获取name属性就相当于从字典中获取某个键值只需要用中括号加属性名就可以 To比如要获取name属性就可以通过attrs[name]来得到 其实这样有点烦琐还有一种更简单的获取方式可以不用写attrs,直接在节点元素后面加中 括号传人属性名就可以获取属性值了。样例如下
html
htmlheadtitleThe Dormouses story/title/head
body
p classtitle namedromousebThe Dormouses story/b/p
p classstoryOnce upon a time there were three little sisters;and their names were
a hrefhttp://example.com/elsie classsister idlinkl!-- Elsie --/a,
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.p[name])
print(soup.p[class])
返回 这里需要注意的是有的返回结果是字符串有的返回结果是字符串组成的列表。比如name属 性的值是唯一的返回的结果就是单个字符串。而对于class, 一个节点元素可能有多个class,所以 返回的是列表。在实际处理过程中我们要注意判断类型。 (3 )获取内容 可以利用string属性获取节点元素包含的文本内容比如要获取第一个p节点的文本
html
htmlheadtitleThe Dormouses story/title/head
body
p classtitle namedromousebThe Dormouses story/b/p
p classstoryOnce upon a time there were three little sisters;and their names were
a hrefhttp://example.com/elsie classsister idlinkl!-- Elsie --/a,
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.p.string)
返回 再次注意一下这里选择到的p节点是第一个p节点获取的文本也是第一个p节点里面的文本。
5.3嵌套选择 在上面的例子中我们知道每一个返回结果都是bs4.element.Tag类型它同样可以继续调用节 点进行下一步的选择。比如我们获取了 head节点元素我们可以继续调用head来选取其内部的head 节点元素
html
htmlheadtitleThe Dormouses story/title/head
body from bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.head.title)
print(type(soup.head.title))
print(soup.head.title.string)
返回 第一行结果是调用head之后再次调用title而选择的title节点元素。然后打印输出了它的类型 可以看到它仍然是bs4.element.Tag类型。也就是说我们在Tag类型的基础上再次选择得到的依 然还是Tag类型每次返回的结果都相同所以这样就可以做嵌套选择了。 最后输出它的string属性也就是节点里的文本内容。
5.4 关联选择 在做选择的时候有时候不能做到一步就选到想要的节点元素需要先选中某一个节点元素然 后以它为基准再选择它的子节点、父节点、兄弟节点等这里就来介绍如何选择这些节点元素。
(1)子节点和子孙节点 选取节点元素之后如果想要获取它的直接子节点可以调用contents属性示例如下:
html
html
head
titleThe Dormouses story/title
/head
body
p classstory Once upon a time there were three little sisters; and their names were a hrefhttp://example.com/elsie classsister idlinkl
spanElsie/span
/a
a hrefhttp://example.com/lacie classsister idlink2Lacie/a
and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a
and they lived at the bottom of a well.
/p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.p.contents)
返回 可以看到返回结果是列表形式。p节点里既包含文本又包含节点最后会将它们以列表形式统一返回。 需要注意的是列表中的每个元素都是p节点的直接子节点。比如第一个a节点里面包含一层span 节点这相当于孙子节点了但是返回结果并没有单独把span节点选出来。所以说contents属性得到的结果是直接子节点的列表。 同样我们可以调用children属性得到相应的结果
html
html
head
titleThe Dormouses story/title
/head
body
p classstory Once upon a time there were three little sisters; and their names were a hrefhttp://example.com/elsie classsister idlinkl
spanElsie/span
/a
a hrefhttp://example.com/lacie classsister idlink2Lacie/a
and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a
and they lived at the bottom of a well.
/p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.p.children)
for i, child in enumerate(soup.p.children): print(i, child)
返回 还是同样的HTML文本这里调用了children属性来选择返回结果是生成器类型。接下来 我们用for循环输出相应的内容。
如果要得到所有的子孙节点的话可以调用descendants属性
html
html
head
titleThe Dormouses story/title
/head
body
p classstory Once upon a time there were three little sisters; and their names were a hrefhttp://example.com/elsie classsister idlinkl
spanElsie/span
/a
a hrefhttp://example.com/lacie classsister idlink2Lacie/a
and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a
and they lived at the bottom of a well.
/p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
for i, child in enumerate(soup.p.descendants): print(i, child)
返回 此时返回结果还是生成器。遍历输出一下可以看到这次的输出结果就包含了 span节点。descendants 会递归查询所有子节点得到所有的子孙节点。
(2)父节点和祖先节点
如果要获取某个节点元素的父节点可以调用parent属性
html
html
head
titleThe Dormouses story/title
/head
body
p classstory Once upon a time there were three little sisters; and their names were a hrefhttp://example.com/elsie classsister idlinkl
spanElsie/span
/a
a hrefhttp://example.com/lacie classsister idlink2Lacie/a
and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a
and they lived at the bottom of a well.
/p
p classstory.../pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.a.parent)
返回 这里我们选择的是第一个a节点的父节点元素。很明显它的父节点是p节点输出结果便是p 节点及其内部的内容。 需要注意的是这里输出的仅仅是a节点的直接父节点而没有再向外寻找父节点的祖先节点。 如果想获取所有的祖先节点可以调用parents属性
html
html
body
p classstory
a hrefhttp://example.com/elsie classsister idlinkl
spanElsie/span
/afrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(type(soup.a.parents))
print(list(enumerate(soup.a.parents)))
返回 可以发现返回结果是生成器类型。这里用列表输出了它的索引和内容而列表中的元素就是a 节点的祖先节点。
(3)兄弟节点 上面说明了子节点和父节点的获取方式如果要获取同级的节点(也就是兄弟节点)应该怎么 办呢示例如下
html
html
body
p classstory Once upon a time there were three little sisters; and their names were a hrefhttp://example.com/elsie classsister idlinkl
spanElsie/span
/a Hello
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a and they lived at the bottom of a well.from bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(Next Sibling, soup.a.next_sibling)
print(Prev Sibling, soup.a.previous_sibling)
print(Next Siblings, list(enumerate(soup.a.next_siblings)))
print(Prev Siblings, list(enumerate(soup.a.previous_siblings)))
返回 可以看到这里调用了 4个属性其中next_sibling和previous_sibling分另U获取节点的下一个 和上一个兄弟元素next_siblings和previous_siblings则分别返回所有前面和后面的兄弟节点的生成器。
(4)提取信息 前面讲解了关联元素节点的选择方法如果想要获取它们的一些信息比如文本、属性等也用 同样的方法示例如下
html
html
body
p classstory Once upon a time there were three little sisters; and their names were
a hrefhttp://example.com/elsie classsister idlinklBob/aa hrefhttp://example.com/lacie
classsister idlink2Lacie/a
/pfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(Next Sibling:)
print(type(soup.a.next_sibling))
print(soup.a.next_sibling)
print(soup.a.next_sibling.string)
print(Parent:)
print(type(soup.a.parents))
print(list(soup.a.parents)[0])
print(list(soup.a.parents)[0].attrs[class])
返回 如果返回结果是单个节点那么可以直接调用string, attrs等属性获得其文本和属性如果返 回结果是多个节点的生成器则可以转为列表后取出某个元素然后再调用string, attrs等属性获 取其对应节点的文本和属性。
6.方法选择器 前面所讲的选择方法都是通过属性来选择的这种方法非常快但是如果进行比较复杂的选择的 话它就比较烦琐不够灵活了。幸好Beautiful Soup还为我们提供了一些查询方法比如find_all() 和find()等调用它们然后传人相应的参数就可以灵活查询了。 • find_all() find_all顾名思义就是查询所有符合条件的元素。给它传人一些属性或文本就可以得到符 合条件的元素它的功能十分强大。 它的API如下
find_all(name , attrs , recursive , text , **kwargs)
(l) name 我们可以根据节点名来查询元素示例如下html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-l
li classelementFoo/li
li classelementBar/li
li classelement]ay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.find_all(nameul))
print(type(soup.find_all(nameul)[0])) 返回 这里我们调用了 find_all()方法传人name参数其参数值为ul。也就是说我们想要查询所 有ul节点返回结果是列表类型长度为2,每个元素依然都是bs4.element.Tag类型。 html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-l
li classelementFoo/li
li classelementBar/li
li classelement]ay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
for ul in soup.find_all(nameul): print(ul.find_all(nameli)) 返回 返回结果是列表类型列表中的每个元素依然还是Tag类型。
接下来就可以遍历每个li获取它的文本了
html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-l
li classelementFoo/li
li classelementBar/li
li classelementJay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
for ul in soup.find_all(nameul): print(ul.find_all(nameli)) for li in ul.find_all(nameli): print(li.string)
返回 (2)attrs
除了根据节点名査询我们也可以传人一些属性来查询示例如下
html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-1 nameelements
li classelementFoo/li
li classelementBar/li
li classelementJay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.find_all(attrs{id: list-1}))
print(soup.find_all(attrs{name: elements}))
返回 这里查询的时候传人的是attrs参数参数的类型是字典类型。比如要查询id为list-1的节 点可以传入attrs{id: * list-1}的查询条件得到的结果是列表形式包含的内容就是符合id 为list-1的所有节点。在上面的例子中符合条件的元素个数是1所以结果是长度为1的列表。 对于一些常用的属性比如id和class等,我们可以不用attrs来传递。比如要查询id为list-1 的节点可以直接传人id这个参数。还是上面的文本我们换一种方式来查询
html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-1 nameelements
li classelementFoo/li
li classelementBar/li
li classelementJay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.find_all(idlist-1))
print(soup.find_all(class_element))
返回 这里直接传人idlist-r就可以查询id为list-l的节点元素了。而对于class来说由于class 在Python里是一个关键字所以后面需要加一个下划线即class,^element*,返回的结果依然还是 Tag组成的列表。
(3) text text参数可用来匹配节点的文本传人的形式可以是字符串可以是正则表达式对象示例如下import re
html
div classpanel
div classpanel-body
aHello, this is a link/a
aHello, this is a link, too/a
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.find_all(textre.compile(link))) 返回 这里有两个a节点其内部包含文本信息。这里在find_all()方法中传人text参数该参数为正 则表达式对象结果返回所有匹配正则表达式的节点文本组成的列表。 • find() 除了 find_all()方法还有find()方法只不过后者返回的是单个元素也就是第一个匹配的元 素而前者返回的是所有匹配的元素组成的列表。示例如下
html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-1 nameelements
li classelementFoo/li
li classelementBar/li
li classelementJay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.find(nameul))
print(type(soup.find(nameul)))
print(soup.find(class_list))
返回 这里的返回结果不再是列表形式而是第一个匹配的节点元素类型依然是Tag类型。 另外还有许多查询方法其用法与前面介绍的find_all()、find()方法完全相同只不过查询 范围不同这里简单说明一下。
find_parents()和find_parent():前者返回所有祖先节点后者返回直接父节点。 find_next_siblings()和行11(1_/16乂1:__511)1:1|^():前者返回后面所有的兄弟节点后者返回后面 第一兄i节点。 find_previous_siblings()和find_previous_sibling():前者返回前面所有的兄弟节点后者 返回前面第一个兄弟节点。find_all_next()和find_next()前者返回节点后所有符合条件的节点后者返回第一个符合 条件的节点。find_all_previous()和find_previous():前者返回节点后所有符合条件的节点后者返回第 一个符合条件的节点。
7. css 选择器 Beautiful Soup还提供了另外一种选择器那就是CSS选择器。如果对Web开发熟悉的话那么对 CSS选择器肯定也不陌生。如果不熟悉的话可以参考CSS 选择器参考手册了解。
使用CSS选择器时只需要调用select()方法传人相应的CSS选择器即可示例如下
html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-1 nameelements
li classelementFoo/li
li classelementBar/li
li classelementJay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
print(soup.select(.panel .panel-heading))
print(soup.select(ul li))
print(soup.select(#list-2 .element))
print(type(soup.select(ul)[0]))
返回 这里我们用了 3次CSS选择器返回的结果均是符合CSS选择器的节点组成的列表。例如 select(ul li)则是选择所有ul节点下面的所有li节点结果便是所有的li节点组成的列表。
最后一句打印输出了列表中元素的类型。可以看到类型依然是Tag类型。
7.1 嵌套选择 select()方法同样支持嵌套选择。例如先选择所有ul节点再遍历每个ul节点选择其li节点样例如下
html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-1 nameelements
li classelementFoo/li
li classelementBar/li
li classelementJay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
for ul in soup.select(ul): print(ul.select(li))
返回 可以看到这里正常输出了所有ul节点下所有li节点组成的列表。
7.2 获取属性 我们知道节点类型是Tag类型所以获取属性还可以用原来的方法。仍然是上面的HTML文本 这里尝试获取每个ul节点的id属性
html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-1 nameelements
li classelementFoo/li
li classelementBar/li
li classelementJay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
for ul in soup.select(ul): print(ul[id]) print(ul.attrs[id])
返回 可以看到直接传人中括号和属性名以及通过attrs属性获取属性值都可以成功。
7.3 获取文本
要获取文本当然也可以用前面所讲的string属性。此外还有一个方法那就是get_text(), 示例如下
html
div classpanel
div classpanel-heading
h4Hello/h4
/div
div classpanel-body
ul classlist idlist-1 nameelements
li classelementFoo/li
li classelementBar/li
li classelementJay/li
/ul
ul classlist list-small idlist-2
li classelementFoo/li
li classelementBar/li
/ul
/div
/divfrom bs4 import BeautifulSoup
soup BeautifulSoup(html, lxml)
for li in soup.select(li): print(Get Text:, li.get_text()) print(String:, li.string)
返回 可以看到二者的效果完全一致。
到此Beautiful Soup的用法基本就介绍完了最后做一下简单的总结。
总结
推荐使用lxml解析库必要时使用html.parser节点选择筛选功能弱但是速度快建议使用find()或者find_all()查询匹配单个结果或者多个结果如果对CSS选择器熟悉的话可以使用selecto方法选择