Python3 爬虫学习笔记 C08【解析库 Beautiful Soup】


Python3 爬虫学习笔记第八章 —— 【解析库 Beautiful Soup】

文章目录

  • 【8.1】关于 Beautiful Soup
  • 【8.2】Beautiful Soup 的基本使用
  • 【8.3】节点选择器
    • 【8.3.1】元素选择
    • 【8.3.2】提取信息
    • 【8.3.3】嵌套选择
    • 【8.3.4】关联选择
  • 【8.4】方法选择器
    • 【8.4.1】find_all() 方法
    • 【8.4.2】find() 方法
  • 【8.5】CSS 选择器
  • 【8.6】附表:Beautiful Soup 库 soup 对象常用属性与方法


【8.1】关于 Beautiful Soup

Beautiful Soup 可以从 HTML 或者 XML 文件中提取数据,Beautiful Soup 可以提供一些简单的、Python 式的函数用来处理导航、搜索、修改分析树等,它借助网页的结构和属性等特性来解析网页,lxml 只会局部遍历,而 Beautiful Soup 是基于 HTML DOM 的,会载入整个文档,解析整个 DOM 树,因此时间和内存开销都会大很多,所以性能要低于lxml

抓取工具速度使用难度安装难度
正则最快困难无(内置)
lxml简单一般
BeautifulSoup最简单简单

【8.2】Beautiful Soup 的基本使用

需要使用命令 pip install bs4 安装库,Beautiful Soup 在解析时依赖解析器,除了支持 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, “xml”)速度快、唯一支持 XML 的解析器需要安装 C 语言库
html5libBeautifulSoup(markup, “html5lib”)最好的容错性、以浏览器的方式解析文档、生成 HTML5 格式的文档速度慢、不依赖外部扩展

基本使用:

from bs4 import BeautifulSoup
soup = BeautifulSoup('<p>Hello</p>', 'lxml')
# soup = BeautifulSoup(open('soup.html', encoding='utf8'), 'lxml')
print(soup.p.string)

输出结果:

Hello

【8.3】节点选择器

直接调用节点的名称就可以选择节点元素,再调用 string 属性就可以得到节点内的文本

【8.3.1】元素选择

新建 soup.html 文件:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8" /><title>测试bs4</title>
</head>
<body>
<div>甄姬<p>百里守约</p><p>李白</p>太乙真人
</div>
<div class="song"><p>李清照</p><p>王安石</p><p>苏轼</p><p>柳宗元</p><a href="http://www.song.com/" title="赵匡义" target="_self">宋朝是最强大的王朝,不是军队的强大,而是经济很强大,国民都很有钱。</a><img src="http://www.baidu.com/meinv.jpg" alt=""><a href="" class="du">总为浮云能蔽日,长安不见使人愁</a>
</div>
<div class="tang"><ul><li><a href="http://www.baidu.com" title="qing">清明时节雨纷纷,路上行人欲断魂,借问酒家何处有,牧童遥指杏花村。</a> </li><li><a href="http://www.163.com" title="qin">秦时明月汉时关,万里长征人未还,但使龙城飞将在,不教胡马度阴山。</a> </li><li><a href="http://www.126.com" alt="qi">岐王宅里寻常见,崔九堂前几度闻,正是江南好风景,落花时节又逢君。</a> </li><li><a href="http://www.sina.com" class="du">杜甫</a> </li><li><b>唐朝</b></li><li><i>宋朝</i></li><li><a href="http://www.haha.com" id="feng">凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘。</a> </li></ul>
</div></body>
</html>
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('soup.html', encoding='utf8'), 'lxml')
print(soup.title)
print(type(soup.title))
print(soup.title.string)
print(soup.head)
print(soup.p)

依次查找 title、head、p 节点。输出结果:

<title>测试bs4</title>
<class 'bs4.element.Tag'>
测试bs4
<head>
<meta charset="utf-8"/>
<title>测试bs4</title>
</head>
<p>百里守约</p>

【8.3.2】提取信息

  • string 属性:获取节点包含的文本值(如果标签里面还有标签,那么string获取到的结果为None)
  • text 属性:获取节点包含的文本值
  • get_text() 属性:获取节点包含的文本值
  • name 属性:获取节点的名称
  • attrs :获取所有属性
  • attrs[‘属性名’] :获取指定属性

依然以 soup.html 为例:

from bs4 import BeautifulSoup
soup = BeautifulSoup(open('soup.html', encoding='utf8'), 'lxml')
print(soup.title)
print(soup.title.text)
print(soup.title.get_text())
print(soup.title.string)
print(soup.div.string)
print(soup.div.text)
print(soup.title.name)
print(soup.a['href'])  # 获取href属性
print(soup.a['title'])  # 获取title属性
print(soup.a['target'])  # 获取target属性
print(soup.a.attrs)  # 获取所有属性
print(soup.a.attrs['href'])  # 获取href属性

输出结果:

<title>测试bs4</title>
测试bs4
测试bs4
测试bs4
None甄姬百里守约
李白太乙真人title
http://www.song.com/
赵匡义
_self
{'href': 'http://www.song.com/', 'title': '赵匡义', 'target': '_self'}
http://www.song.com/

【8.3.3】嵌套选择

from bs4 import BeautifulSouphtml = """
<html><head><title>This is a demo</title></head>
<body>
"""
soup = BeautifulSoup(html, 'lxml')
print(soup.head.title)
print(type(soup.head.title))
print(soup.head.title.string)

获取 head 节点里面的 title 节点,输出结果:

<title>This is a demo</title>
<class 'bs4.element.Tag'>
This is a demo

【8.3.4】关联选择

  • contents 属性:获取某个节点元素的直接子节点
  • children 属性:遍历某个节点元素的子节点
  • descendants 属性:获取某个节点元素所有的子孙节点
  • parent 属性:获取某个节点元素的父节点
  • parents 属性:获取某个节点元素所有的祖先节点
  • next_sibling 属性:获取节点的下一个兄弟元素
  • previous_sibling 属性:获取节点的上一个兄弟元素
  • next_siblings 属性:获取某个节点所有后面的兄弟元素
  • previous_siblings 属性:获取某个节点所有前面的兄弟元素

contents 属性应用示例

from bs4 import BeautifulSouphtml = """
<html><head><title>The Dormouse's story</title></head><body><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"><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>and they lived at the bottom of a well.</p><p class="story">...</p>
"""
soup = BeautifulSoup(html, 'lxml')
print(soup.p.contents)

获取 p 节点元素的直接子节点,输出结果:

['\n            Once upon a time there were three little sisters; and their names were\n            ', <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>, '\n', <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, ' \n            and\n            ', <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>, '\n            and they lived at the bottom of a well.\n        ']

children 属性应用示例:

from bs4 import BeautifulSouphtml = """
<html><head><title>The Dormouse's story</title></head><body><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"><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>and they lived at the bottom of a well.</p><p class="story">...</p>
"""
soup = BeautifulSoup(html, 'lxml')
print(soup.p.children)
for i, child in enumerate(soup.p.children):print(i, child)

遍历 p 节点元素的子节点,输出结果:

<list_iterator object at 0x00000228E3C205F8>
0 Once upon a time there were three little sisters; and their names were1 <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
2 3 <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
4  and5 <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
6 and they lived at the bottom of a well.

descendants 属性应用示例:

from bs4 import BeautifulSouphtml = """
<html><head><title>The Dormouse's story</title></head><body><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"><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>and they lived at the bottom of a well.</p><p class="story">...</p>
"""
soup = BeautifulSoup(html, 'lxml')
print(soup.p.descendants)
for i, child in enumerate(soup.p.descendants):print(i, child)

获取 p 节点元素所有的子孙节点,输出结果:

<generator object descendants at 0x0000018404A4C3B8>
0 Once upon a time there were three little sisters; and their names were1 <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
2 3 <span>Elsie</span>
4 Elsie
5 6 7 <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
8 Lacie
9  and10 <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
11 Tillie
12 and they lived at the bottom of a well.

parent 属性应用示例:

from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title></head><body><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"><span>Elsie</span></a></p></body>
</html>
"""
soup = BeautifulSoup(html, 'lxml')
print(soup.a.parent)

获取 a 节点元素的父节点,输出结果:

<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">
<span>Elsie</span>
</a>
</p>

parents 属性应用示例:

from bs4 import BeautifulSouphtml = """
<html><body><p class="story"><a href="http://example.com/elsie" class="sister" id="link1"><span>Elsie</span></a></p>
"""
soup = BeautifulSoup(html, 'lxml')
print(type(soup.a.parents))
print(list(enumerate(soup.a.parents)))

获取 a 节点元素所有的祖先节点,输出结果:

<class 'generator'>
[(0, <p class="story">
<a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
</p>), (1, <body>
<p class="story">
<a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
</p>
</body>), (2, <html>
<body>
<p class="story">
<a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
</p>
</body></html>), (3, <html>
<body>
<p class="story">
<a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
</p>
</body></html>)]

next_sibling、previous_sibling、next_siblings、previous_siblings 属性应用示例:

html = """
<html><body><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"><span>Elsie</span></a>Hello<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>   </body>
</html>
"""
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)))

next_sibling 和 previous_sibling 分别获取 a 节点的下一个和上一个兄弟元素,next_siblings 和 previous_siblings 则分别返回 a 节点后面和前面的兄弟节点,输出结果:

Next Sibling HelloPrev Sibling Once upon a time there were three little sisters; and their names wereNext Siblings [(0, '\n            Hello\n            '), (1, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>), (2, ' \n            and\n            '), (3, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>), (4, '\n            and they lived at the bottom of a well.\n        ')]
Prev Siblings [(0, '\n            Once upon a time there were three little sisters; and their names were\n            ')]

【8.4】方法选择器

节点选择器直接调用节点的名称就可以选择节点元素,如果进行比较复杂的选择的话,方法选择器是一个不错的选择,它更灵活,常见的方法有 find_all、find 等,调用它们,直接传入相应的参数,就可以灵活查询了。

【8.4.1】find_all() 方法

find_all 方法可以查询所有符合条件的元素,给它传入一些属性或文本来得到符合条件的元素。find_all 方法的 API:find_all(name , attrs , recursive , text , **kwargs)
新建 soup.html:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8" /><title>测试bs4</title>
</head>
<body>
<div>甄姬<p>百里守约</p><p>李白</p>太乙真人
</div>
<div class="song"><p>李清照</p><p>王安石</p><p>苏轼</p><p>柳宗元</p><a href="http://www.song.com/" title="赵匡义" target="_self">宋朝是最强大的王朝,不是军队的强大,而是经济很强大,国民都很有钱。</a><img src="http://www.baidu.com/meinv.jpg" alt=""><a href="" class="du">总为浮云能蔽日,长安不见使人愁</a>
</div>
<div class="tang"><ul><li><a href="http://www.baidu.com" title="qing">清明时节雨纷纷,路上行人欲断魂,借问酒家何处有,牧童遥指杏花村。</a> </li><li><a href="http://www.163.com" title="qin">秦时明月汉时关,万里长征人未还,但使龙城飞将在,不教胡马度阴山。</a> </li><li><a href="http://www.126.com" name="qi">岐王宅里寻常见,崔九堂前几度闻,正是江南好风景,落花时节又逢君。</a> </li><li><a href="http://www.sina.com" class="du">杜甫</a> </li><li><b>唐朝</b></li><li><i>宋朝</i></li><li><a href="http://www.haha.com" id="feng">凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘。</a> </li></ul>
</div></body>
</html>

示例代码:

from bs4 import BeautifulSoupsoup = BeautifulSoup(open('soup.html', encoding='utf8'), 'lxml')
print(soup.find_all('a'), '\n')
print(soup.find_all('a')[1], '\n')
print(soup.find_all('a')[1].text, '\n')
print(soup.find_all(['a', 'b', 'i']), '\n')
print(soup.find_all('a', limit=2), '\n')
print(soup.find_all(title='qing'), '\n')
print(soup.find_all(attrs={'id': 'feng'}), '\n')

输出结果:

[<a href="http://www.song.com/" target="_self" title="赵匡义">宋朝是最强大的王朝,不是军队的强大,而是经济很强大,国民都很有钱。</a>, <a class="du" href="">总为浮云能蔽日,长安不见使人愁</a>, <a href="http://www.baidu.com" title="qing">清明时节雨纷纷,路上行人欲断魂,借问酒家何处有,牧童遥指杏花村。</a>, <a href="http://www.163.com" title="qin">秦时明月汉时关,万里长征人未还,但使龙城飞将在,不教胡马度阴山。</a>, <a href="http://www.126.com" name="qi">岐王宅里寻常见,崔九堂前几度闻,正是江南好风景,落花时节又逢君。</a>, <a class="du" href="http://www.sina.com">杜甫</a>, <a href="http://www.haha.com" id="feng">凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘。</a>] <a class="du" href="">总为浮云能蔽日,长安不见使人愁</a> 总为浮云能蔽日,长安不见使人愁 [<a href="http://www.song.com/" target="_self" title="赵匡义">宋朝是最强大的王朝,不是军队的强大,而是经济很强大,国民都很有钱。</a>, <a class="du" href="">总为浮云能蔽日,长安不见使人愁</a>, <a href="http://www.baidu.com" title="qing">清明时节雨纷纷,路上行人欲断魂,借问酒家何处有,牧童遥指杏花村。</a>, <a href="http://www.163.com" title="qin">秦时明月汉时关,万里长征人未还,但使龙城飞将在,不教胡马度阴山。</a>, <a href="http://www.126.com" name="qi">岐王宅里寻常见,崔九堂前几度闻,正是江南好风景,落花时节又逢君。</a>, <a class="du" href="http://www.sina.com">杜甫</a>, <b>唐朝</b>, <i>宋朝</i>, <a href="http://www.haha.com" id="feng">凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘。</a>] [<a href="http://www.song.com/" target="_self" title="赵匡义">宋朝是最强大的王朝,不是军队的强大,而是经济很强大,国民都很有钱。</a>, <a class="du" href="">总为浮云能蔽日,长安不见使人愁</a>] [<a href="http://www.baidu.com" title="qing">清明时节雨纷纷,路上行人欲断魂,借问酒家何处有,牧童遥指杏花村。</a>] [<a href="http://www.haha.com" id="feng">凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘。</a>] 

【8.4.2】find() 方法

find() 方法使用方法与 find_all() 方法相同,不同的是,find 方法返回的是单个元素,也就是第一个匹配的元素,而 find_all 返回的是所有匹配的元素组成的列表
特别的:

  • find_parents 和 find_parent:前者返回所有祖先节点,后者返回直接父节点。

  • find_next_siblings 和 find_next_sibling:前者返回后面所有的兄弟节点,后者返回后面第一个兄弟节点。

  • find_previous_siblings 和 find_previous_sibling:前者返回前面所有的兄弟节点,后者返回前面第一个兄弟节点。

  • find_all_next 和 find_next:前者返回节点后所有符合条件的节点,后者返回第一个符合条件的节点。

  • find_all_previous 和 find_previous:前者返回节点前所有符合条件的节点,后者返回第一个符合条件的节点。

【8.5】CSS 选择器

使用 CSS 选择器,只需要调用 select 方法,传入相应的 CSS 选择器即可
新建 soup.html 文件:

<!DOCTYPE html>
<html lang="en">
<div class="tang"><ul><li><a href="http://www.baidu.com" title="qing">清明时节雨纷纷,路上行人欲断魂,借问酒家何处有,牧童遥指杏花村。</a> </li><li><a href="http://www.163.com" title="qin">秦时明月汉时关,万里长征人未还,但使龙城飞将在,不教胡马度阴山。</a> </li><li><a href="http://www.126.com" name="qi">岐王宅里寻常见,崔九堂前几度闻,正是江南好风景,落花时节又逢君。</a> </li><li><a href="http://www.sina.com" class="du">杜甫</a> </li><li><a href="http://www.haha.com" id="feng">凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘。</a> </li></ul>
</div></body>
</html>

通过 CSS 选择器依次选择 class=“tang” 的 div 节点下的 a 节点、id 为 feng 的节点以及其 href 元素:

from bs4 import BeautifulSoupsoup = BeautifulSoup(open('soup.html', encoding='utf8'), 'lxml')
print(soup.select('li'), '\n')
print(soup.select('.tang > ul > li > a')[2], '\n')
print(soup.select('#feng')[0].text, '\n')
print(soup.select('#feng')[0]['href'], '\n')

输出结果:

[<li><a href="http://www.baidu.com" title="qing">清明时节雨纷纷,路上行人欲断魂,借问酒家何处有,牧童遥指杏花村。</a> </li>, <li><a href="http://www.163.com" title="qin">秦时明月汉时关,万里长征人未还,但使龙城飞将在,不教胡马度阴山。</a> </li>, <li><a href="http://www.126.com" name="qi">岐王宅里寻常见,崔九堂前几度闻,正是江南好风景,落花时节又逢君。</a> </li>, <li><a class="du" href="http://www.sina.com">杜甫</a> </li>, <li><a href="http://www.haha.com" id="feng">凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘。</a> </li>] <a href="http://www.126.com" name="qi">岐王宅里寻常见,崔九堂前几度闻,正是江南好风景,落花时节又逢君。</a> 凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘。 http://www.haha.com 

附表:CSS 选择器,来源:https://www.w3school.com.cn/cssref/css_selectors.asp
“CSS” 列指示该属性是在哪个 CSS 版本中定义的。(CSS1、CSS2 还是 CSS3。)

选择器例子例子描述CSS
.class.intro选择 class=“intro” 的所有元素1
#id#firstname选择 id=“firstname” 的所有元素1
**选择所有元素2
elementp选择所有

元素

1
element,elementdiv,p选择所有
元素和所有

元素

1
element elementdiv p选择
元素内部的所有

元素

1
element>elementdiv>p选择父元素为
元素的所有

元素

2
element+elementdiv+p选择紧接在
元素之后的所有

元素

2
[attribute][target]选择带有 target 属性所有元素2
[attribute=value][target=_blank]选择 target="_blank" 的所有元素2
[attribute~=value][title~=flower]选择 title 属性包含单词 “flower” 的所有元素2
[attribute=value][lang=en]
:linka:link选择所有未被访问的链接1
:visiteda:visited选择所有已被访问的链接1
:activea:active选择活动链接1
:hovera:hover选择鼠标指针位于其上的链接1
:focusinput:focus选择获得焦点的 input 元素2
:first-letterp:first-letter选择每个

元素的首字母

1
:first-linep:first-line选择每个

元素的首行

1
:first-childp:first-child选择属于父元素的第一个子元素的每个

元素

2
:beforep:before在每个

元素的内容之前插入内容

2
:afterp:after在每个

元素的内容之后插入内容

2
:lang(language)p:lang(it)选择带有以 “it” 开头的 lang 属性值的每个

元素

2
element1~element2p~ul选择前面有

元素的每个

  • 元素

3
[attribute^=value]a[src^=“https”]选择其 src 属性值以 “https” 开头的每个 元素3
[attribute$=value]a[src$=".pdf"]选择其 src 属性以 “.pdf” 结尾的所有 元素3
[attribute*=value]a[src*=“abc”]选择其 src 属性中包含 “abc” 子串的每个 元素3
:first-of-typep:first-of-type选择属于其父元素的首个

元素的每个

元素

3
:last-of-typep:last-of-type选择属于其父元素的最后

元素的每个

元素

3
:only-of-typep:only-of-type选择属于其父元素唯一的

元素的每个

元素

3
:only-childp:only-child选择属于其父元素的唯一子元素的每个

元素

3
:nth-child(n)p:nth-child(2)选择属于其父元素的第二个子元素的每个

元素

3
:nth-last-child(n)p:nth-last-child(2)同上,从最后一个子元素开始计数3
:nth-of-type(n)p:nth-of-type(2)选择属于其父元素第二个

元素的每个

元素

3
:nth-last-of-type(n)p:nth-last-of-type(2)同上,但是从最后一个子元素开始计数3
:last-childp:last-child选择属于其父元素最后一个子元素每个

元素

3
:root:root选择文档的根元素3
:emptyp:empty选择没有子元素的每个

元素(包括文本节点)

3
:target#news:target选择当前活动的 #news 元素3
:enabledinput:enabled选择每个启用的 元素3
:disabledinput:disabled选择每个禁用的 元素3
:checkedinput:checked选择每个被选中的 元素3
:not(selector):not§选择非

元素的每个元素

3
::selection::selection选择被用户选取的元素部分3

【8.6】附表:Beautiful Soup 库 soup 对象常用属性与方法

基本元素说明返回类型
tagsoup.abs4.element.Tag
namesoup.a.namestr
attrssoup.a.attrsdict
contents子节点list
children遍历子节点list_iterator
descendants遍历所有子孙节点generator
parent返回父亲标签bs4.element.Tag
parents上行遍历父辈标签generator
prettify()添加/nstr
find_all(name,attr)soup.find_all(‘a’)/([‘a’,‘b’])/(True)/(‘p’,‘course’)/(id=‘link1’)/(string=‘python’)bs4.element.ResultSet
find()soup.find(‘a’)/返回第一个a标签bs4.element.Tag

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/437945.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Sharepoint学习笔记—Ribbon系列

为便于查阅&#xff0c;这里整理并列出了我的Sharepoint学习笔记中涉及Ribbon开发的关文章&#xff0c;有些内容可能会在以后更新。 Sharepoint学习笔记—Ribbon系列-- 1. Ribbon的架构 Sharepoint学习笔记—Ribbon系列-- 2. 在Ribbon中添加新Tab Sharepoint学习笔记—Ribbo…

Python3 爬虫学习笔记 C09【数据储存系列 — 文件储存】

Python3 爬虫学习笔记第九章 —— 【数据储存系列 — 文件储存】文章目录【9.1】TXT 文本存储【9.1.1】基本示例【9.1.2】打开方式【9.2】JSON 文件存储【9.2.1】对象和数组【9.2.2】读取 JSON【9.2.3】写入 JSON 文件【9.3】CSV 文本存储【9.3.1】写入【9.3.2】读取用解析器解…

Knockout学习笔记之二($root,$parent及$data的区别)

以下是我从Google上找到的一个例子&#xff0c;非常生动形象&#xff0c;我修改了部分代码&#xff0c;具体内容如下&#xff1a; 对于$root 与$parent的区别&#xff1a; $root refers to the view model applied to the DOM with ko.applyBindings;译&#xff1a;$root 是指…

GitHub 学生认证,申请 GitHub 学生包

GitHub 面对学生推出了学生认证服务&#xff0c;通过认证后就可以得到学生包&#xff0c;学生包大概有十几项优惠&#xff0c;包括 DATADOG Pro 帐户、免费两年的10台服务器&#xff0c;Icons8 3个月的带图标&#xff0c;照片&#xff0c;插图和音乐订阅服务、JETBRAINS 专业桌…

Sharepoint学习笔记—架构系列

为便于查阅&#xff0c;这里整理并列出了我的Sharepoint学习笔记中涉及架构方面的有关文章&#xff0c;有些内容可能会在以后更新。 Sharepoin学习笔记—架构系列-- Sharepoint的网页(Page)&#xff0c;网页解析(Parsing)与解析安全处理(Security) Sharepoin学习笔记 —架构…

Python3 使用 pymysql 连接 MySQL 建表时出现 Warning3719 UTF8 警告

在学习 Python3 爬虫关系型数据库储存时&#xff0c;利用 pymysql 连接 MySQL 建表&#xff0c;测试用的代码如下&#xff0c;第一句 SQL 用于获取当前 MySQL 的版本信息&#xff0c;第二句 SQL 执行创建 spiders 数据库的操作&#xff0c;如果程序代码正确&#xff0c;将会输出…

Sharepoin学习笔记—架构系列--01 Sharepoint的网页(Page),网页解析(Parsing)与解析安全处理(Security)

Microsoft SharePoint Foundation 中主要有两种类型的页面&#xff0c;分别是应用程序页(Application Page) 和网站页(Site Page)。 应用程序页(Application Page) 和网站页(Site Page)都从同一母版页继承其布局。 应用程序页(Application Page)与传统的 Microsoft ASP.NET 3.5…

完美解决 bash: hexo: command not found

背景介绍&#xff1a;有好几天没动过 Hexo 博客了&#xff0c;今天准备更新的时候输入 hexo s&#xff0c;报错 bash: hexo: command not found&#xff0c;这是啥情况&#xff1f;以前都好好的&#xff0c;想了一下&#xff0c;大概是这几天折腾各种 Python 库的原因&#xff…

Sharepoin学习笔记 —架构系列--02 Sharepoint的处理(Process)与执行模型(Trust Model) 1

Sharepoint210有四种执行模型: 1、完全信任执行模型(Full Trust) 2、Bin/CAS 执行模型 &#xff08;1与2都属于场解决方案&#xff09; 3、沙盒执行模型(Sand Box) 4、 混合执行方法&#xff08;Hybrid Approach&#xff09; Sharepoint最简单的处理模型就是一个完整的Asp.net应…

Python3 爬虫学习笔记 C10【数据储存系列 — MySQL】

Python3 爬虫学习笔记第十章 —— 【数据储存系列 — MySQL】文章目录【10.1】MySQL 基本操作语句数据库操作表操作表的结构表的数据【10.2】Python 连接 MySQL【10.3】创建表【10.4】插入数据【10.5】更新数据【10.6】删除数据【10.7】查询数据【10.8】实战训练 — 爬取CSDN博…

Sharepoin学习笔记—架构系列--03 Sharepoint的处理(Process)与执行模型(Trust Model) 2

上文我们了解了一个外部Http Request进入IIS 工作进程(W3WP)的处理与执行信任模型&#xff0c;这个阶段是Sharepoint的四种执行模型都必须经过的处理阶段&#xff0c;其中Sharepoint场解决方案与任何 ASP.NET 应用程序一样就是在 IIS 工作进程(w3wp)中运行的&#xff0c;所以上…

Python3 爬虫学习笔记 C11【数据储存系列 — MongoDB】

Python3 爬虫学习笔记第十一章 —— 【数据储存系列 — MongoDB】文章目录【11.1】关于 MongoDB【11.2】MongoDB 基本操作语句【11.3】连接 MongoDB【11.4】指定数据库【11.5】指定集合【11.6】插入数据【11.6】数据查询【11.7】数据计数【11.8】数据排序【11.9】数据偏移【11.…

Sharepoin学习笔记—架构系列--04 Sharepoint的四种执行模型 1

Sharepoint210有四种执行模型 1、完全信任执行模型(Full Trust) 2、Bin/CAS 执行模型 &#xff08;1与2都属于场解决方案&#xff09; 3、沙盒执行模型(Sand Box) 4、 混合执行方法 (Hybrid Approach) 下面分别来看看它们是怎么回事 一、场解决方案 场解决方案是在 Share…

Python3 爬虫学习笔记 C12【验证码对抗系列 — 图形验证码】

Python3 爬虫学习笔记第十二章 —— 【验证码对抗系列 — 图形验证码】文章目录【12.1】关于普通图形验证码【12.2】tesserocr 库识别验证码【12.3】pytesseract 库识别验证码【12.4】验证码处理【12.5】tesserocr 与 pytesserocr 相关资料【12.1】关于普通图形验证码 普通图形…

Sharepoin学习笔记—架构系列--05 Sharepoint的四种执行模型 2

上一篇我们看了场解决方案与沙盒方案两种执行模型&#xff0c;其中场解决方案包括有完全信任方式与Bin/CAS方式两种&#xff0c;这里让我们继续来看看最后一个执行模型&#xff0c;即混合模型(或混合模式)。 三、混合模式&#xff08;hybrid approaches&#xff09; 所谓混合模…

Python3 爬虫学习笔记 C13【验证码对抗系列 — 滑动验证码】

Python3 爬虫学习笔记第十三章 —— 【验证码对抗系列 — 滑动验证码】文章目录【13.1】关于滑动验证码【13.2】滑动验证码攻克思路【13.3】模拟登录 bilibili — 总体思路【13.4】主函数【13.5】初始化函数【13.6】登录函数【13.7】验证码元素查找函数【13.8】元素可见性设置函…

Sharepoin学习笔记—架构系列—06 Sharepoint服务(Services)与服务应用程序框架(Service Application Framework) 1

Sharepoint服务是Sharepoint的重要组成&#xff0c;可以说Sharepoint的许多网站功能都是基于这些服务构架起来的。这里把Sharepoint服务的相关要点总结一下。 1、什么是 SharePoint 服务&#xff1f; SharePoint 服务是一项 IT 服务&#xff0c;它是运行在后台&#xff0c;为调…

Python3 爬虫学习笔记 C14【验证码对抗系列 — 点触验证码】

Python3 爬虫学习笔记第十四章 —— 【验证码对抗系列 — 点触验证码】文章目录【14.1】关于点触验证码【14.2】点触验证码攻克思路【14.3】模拟登录 12306 — 总体思路【14.4】主函数【14.5】初始化函数【14.6】破解入口函数【14.7】账号密码输入函数【14.8】页面截图函数【14…

Sharepoin学习笔记—架构系列—07nSharepoint服务(Services)与服务应用程序框架(Service Application Framework) 2

上一篇我们以问答的方式明确了Sharepoint服务的一些概念&#xff0c;这里我们重点来看两个方面:Sharepoint服务器构架对象模型以及Sharepoint 服务应用程序的某些拓扑结构 一、Sharepoint服务器构架对象模型 转存失败重新上传取消 二、Sharepoint 服务应用程序的某些拓扑结…

Python3 爬虫学习笔记 C15【代理的基本使用】

Python3 爬虫学习笔记第十五章 —— 【代理的基本使用】文章目录【15.1】代理初识【15.2】urllib 库使用代理【15.3】requests 库使用代理【15.4】Selenium 使用代理【15.4.1】Chrome【15.4.1】PhantomJS【15.1】代理初识 大多数网站都有反爬虫机制&#xff0c;如果一段时间内…