一、 BeautifulSoup4 介绍
复制代码
pip3 install beautifulsoup4
二、 bs4 遍历文档树
复制代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" id='id_p'>lqz is handsome<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>
"""
soup=BeautifulSoup(html_doc,'html.parser')
'''
#遍历文档树:即直接通过标签名字选择,特点是选择速度快,但如果存在多个相同的标签则只返回第一个
#1、用法
#2、获取标签的名称
#3、获取标签的属性
#4、获取标签的内容
#5、嵌套选择
#6、子节点、子孙节点
#7、父节点、祖先节点
#8、兄弟节点
'''
res=soup.p
res1=soup.p.b
res1=soup.html.body.p.b
print(res1)
res1=soup.body
print(res1.name)
res1=soup.body.p
print(res1.attrs['id'])
res=soup.a['href']
print(res)
res=soup.a
print(res.text)
print(res.string)
print(list(res.strings))
res=soup.p
print(res.text)
print(res.string)
print(list(res.strings))
print(soup.head.title.string)
print(type(soup.head))
print(soup.p.contents)
print(soup.p.children)
print(list(soup.p.descendants))
print(soup.a.parent)
print(list(soup.a.parents))
print(soup.a.next_sibling)
print(soup.a.previous_sibling)
print(list(soup.a.next_siblings))
print(list(soup.a.previous_siblings))
三、 bs4搜索文档树
复制代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" id='id_p'>lqz is handsome<b class='sister'>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>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
"""
#### 五种过滤器: 字符串、正则表达式、列表、True、方法 ####
"""
p=soup.find(name='p',class_='title')
p=soup.find(id='id_p')
p=soup.find(id='id_p')
res=soup.find(text="The Dormouse's story").parent
print(res)
import re
res=soup.find_all(name=re.compile('^b'))
res=soup.find_all(class_=re.compile('^s'))
print(res)
res=soup.find_all(name=['body','b'])
res=soup.find_all(class_=['sister','story'])
print(res)
res=soup.find_all(id=True)
res=soup.find_all(text=False)
print(res)
def has_class_but_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id')
print(soup.find_all(name=has_class_but_no_id))
res=soup.find_all(class_=True,limit=1)
res=soup.body.find_all(name='p',recursive=False)
res=soup.find_all(class_=True)
res=soup.find_all(attrs={'class':True})
print(res)
四、 css选择器搜索文档树
复制代码
'''
div 标签名
.类名 类名
#id id号
div>p div下紧邻的p标签
div p div下子子孙孙有p就拿出来
'''
res=soup.select('#id_p')[0]['class']
print(res)
案例: 爬取新闻
复制代码
复制代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
import requests
from bs4 import BeautifulSoup
res = requests.get('https://www.autohome.com.cn/news/1/#liststart')
soup = BeautifulSoup(res.text, 'html.parser')
ul_list = soup.find_all(name='ul', class_='article')
for ul in ul_list:
li_list = ul.find_all(name='li')
for li in li_list:
h3 = li.find(name='h3')
if h3:
title = h3.text
desc = li.find(name='p').text
url = 'http:' + li.find(name='a').attrs['href']
img = 'http:' + li.find(name='img')['src']
print('''
新闻标题:%s
新闻摘要:%s
新闻地址:%s
新闻图片:%s
''' % (title, desc, url,img))
posted @
2022-08-02 20:48
马氵寿
阅读(
353)
评论()
编辑
收藏
举报
点击右上角即可分享
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix