Python基礎(chǔ)學(xué)習(xí)19

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())
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一部分 創(chuàng)建爬蟲(chóng) 重點(diǎn)介紹網(wǎng)絡(luò)數(shù)據(jù)采集的基本原理 : 如何用 Python 從網(wǎng)絡(luò)服務(wù)器 請(qǐng)求信息,如何對(duì)服務(wù)器...
    萬(wàn)事皆成閱讀 2,294評(píng)論 0 5
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,161評(píng)論 4 61
  • 聲明:本文講解的實(shí)戰(zhàn)內(nèi)容,均僅用于學(xué)習(xí)交流,請(qǐng)勿用于任何商業(yè)用途! 一、前言 強(qiáng)烈建議:請(qǐng)?jiān)陔娔X的陪同下,閱讀本文...
    Bruce_Szh閱讀 12,998評(píng)論 6 28
  • 圖片發(fā)自簡(jiǎn)書App 引詩(shī):《五絕 風(fēng)》(平水韻) 玉帝降霖令, 風(fēng)神作法威。 云飛光聚散, 雨舞柳條揮。 《憶秦娥...
    青魚吹浪閱讀 554評(píng)論 4 4
  • 前幾天,我也跟了潮流去電影院看了前任三,看的中途,我強(qiáng)忍著眼里的淚水,心里撕裂地心疼,多少愛(ài)情就像這樣不了了之。...
    一記顆舟閱讀 449評(píng)論 0 0

友情鏈接更多精彩內(nèi)容