BeautifulSoup安裝庫(kù)
~ pip3 install bs4
Collecting bs4
Downloading https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz
Collecting beautifulsoup4 (from bs4)
Downloading https://files.pythonhosted.org/packages/1d/5d/3260694a59df0ec52f8b4883f5d23b130bc237602a1411fa670eae12351e/beautifulsoup4-4.7.1-py3-none-any.whl (94kB)
100% |████████████████████████████████| 102kB 230kB/s
Collecting soupsieve>=1.2 (from beautifulsoup4->bs4)
Downloading https://files.pythonhosted.org/packages/77/78/bca00cc9fa70bba1226ee70a42bf375c4e048fe69066a0d9b5e69bc2a79a/soupsieve-1.8-py2.py3-none-any.whl (88kB)
100% |████████████████████████████████| 92kB 114kB/s
Building wheels for collected packages: bs4
Building wheel for bs4 (setup.py) ... done
Stored in directory: /Users/insight2026/Library/Caches/pip/wheels/a0/b0/b2/4f80b9456b87abedbc0bf2d52235414c3467d8889be38dd472
Successfully built bs4
Installing collected packages: soupsieve, beautifulsoup4, bs4
Successfully installed beautifulsoup4-4.7.1 bs4-0.0.1 soupsieve-1.8
BeautifulSoup替代正則提取html內(nèi)容應(yīng)用案例:
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><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" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a> and
<a class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'lxml')#定義lxml格式承載html格式,如沒(méi)有可用pip3 install lxml安裝
print(soup.prettify())
#
# 找到title標(biāo)簽
print(soup.title)
#
# title 標(biāo)簽里的內(nèi)容
print(soup.title.string)
# # 找到p標(biāo)簽
print(soup.p)
#
# 找到p標(biāo)簽class的名字
print(soup.p['class'])
#
# 找到第一個(gè)a標(biāo)簽
print(soup.a)
#
# 找到所有的a標(biāo)簽
print(soup.find_all('a'))
#
#
# # 找到id為link3的的標(biāo)簽
print(soup.find(id="link3"))
#
# 找到所有<a>標(biāo)簽的鏈接
for link in soup.find_all('a'):
print(link.get('href'))
#
# 找到文檔中所有的文本內(nèi)容
print(soup.get_text())