BeautifulSoup(简称bs4)是一个用于解析HTML和XML文档的Python库,它能够解析HTML页面,从而方便地提取网页数据。
-
「安装bs4库」 :在Python环境中,可以通过pip安装命令来安装bs4:
pip install
beautifulsoup4
-
「导入bs4」 :在Python脚本中,我们需要先导入bs4库:
from bs4 import BeautifulSoup
-
「解析文档」 :使用bs4解析HTML文档,通常需要一个解析器。常用的解析器有、和。例如,使用:
soup =
BeautifulSoup(html_content, 'html.parser')
-
「查找元素」 :bs4提供了多种方法来查找页面元素,如、等。可以根据标签名、属性、CSS类等来定位元素。
-
「提取数据」 :找到元素后,可以通过、等属性来提取数据。
常用方法
-
「」 :查找文档树中第一个匹配的元素。
first_heading
= soup.find('h1')
-
「」 :查找文档树中所有匹配的元素。
paragraphs =
soup.find_all('p')
-
「」 :使用CSS选择器查找元素。
links =
soup.select('a[href]')
-
「」 :获取元素的文本内容,并且可以设置参数来控制空白字符的处理。
text =
first_paragraph.get_text()
-
「」 :获取元素的属性值。
href =
first_link.get('href')
-
「」 :将元素或元素列表转换成一个字符串。
html =
table.string
简单实例
实例1:提取网页标题
from bs4 import BeautifulSoup
import requests
# 获取网页内容
url = 'http://example.com'
response =
requests.get(url)
html_content = response.text
# 解析文档
soup =
BeautifulSoup(html_content, 'html.parser')
# 提取网页标题
title = soup.find('title').text
print('网页标题:', title)
实例2:解析表格数据
from bs4 import BeautifulSoup
import requests
# 获取网页内容
url = 'http://example.com/table-page'
response =
requests.get(url)
html_content = response.text
# 解析文档
soup =
BeautifulSoup(html_content, 'html.parser')
# 查找表格
table = soup.find('table')
# 提取表格标题
headers =
[header.text for header in table.find_all('th')]
# 提取表格行数据
rows = table.find_all('tr')
for row in
rows:
cols = row.find_all('td')
cols_data = [ele.text.strip() for ele in cols]
print('行数据:',
cols_data)
实例3:提取链接和图片
from bs4 import BeautifulSoup
import requests
# 获取网页内容
url = 'http://example.com/image-page'
response =
requests.get(url)
html_content = response.text
# 解析文档
soup =
BeautifulSoup(html_content, 'html.parser')
# 查找所有链接
links = soup.find_all('a')
for link in
links:
href = link.get('href')
print('链接地址:',
href)
#
查找所有图片
images = soup.find_all('img')
for img
in images:
src =
img.get('src')
print('图片地址:', src)
实例4:使用CSS选择器提取数据
from bs4 import BeautifulSoup
# 假设html_content是获取到的HTML内容
soup =
BeautifulSoup(html_content, 'html.parser')
# 使用CSS类选择器
items_with_class =
soup.select('.my-
class')
# 使用CSS属性选择器
items_with_data_attr = soup.select('[data-some-
attribute]')
# 使用CSS组合选择器
items_with_specific_parents = soup.select('section > a')
实例5:获取所有标签
from bs4 import BeautifulSoup
import requests
url = 'http://example.com'
response =
requests.get(url)
html_content = response.text
soup =
BeautifulSoup(html_content, 'html.parser')
#获取所有标签
for tag in
soup.find_all(True):
attrs =
tag.attrs
for attr, value
in attrs.items():
print(f'Tag: {tag.name},
Attr: {attr}, Value: {value}')