Python爬蟲之?dāng)?shù)據(jù)解析模塊bs4基礎(chǔ)

介紹:

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供西安網(wǎng)站建設(shè)、西安做網(wǎng)站、西安網(wǎng)站設(shè)計(jì)、西安網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、西安企業(yè)網(wǎng)站模板建站服務(wù),10年西安做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

最近在學(xué)Python爬蟲,在這里對數(shù)據(jù)解析模塊bs4做個學(xué)習(xí)筆記。

用途:

bs4用于解析xml文檔,而html只是xml的一種

bs4 官方文檔地址:

https://www.crummy.com/software/BeautifulSoup/bs4/doc/

學(xué)習(xí)筆記:

from bs4 import BeautifulSoup

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>

"""

soup = BeautifulSoup(html_doc,'html.parser')? ? #創(chuàng)建一個BeautifulSoup對象,添加html文件解析器,在不同平臺可能不同,在Linux上就不需要

print(soup.prettify())? ? #美化輸出

print(soup.get_text())? ??#將html_doc變量中保存的全部內(nèi)容輸出(Linux系統(tǒng)會以\n隔開)

print('')

print(type(soup.title))

print(dir(soup.title))

print(soup.title)? ? #獲取html標(biāo)題

????<title>The Dormouse's story</title>

print(soup.title.text)? ? #獲取html標(biāo)題內(nèi)容

????"The Dormouse's story"

print(soup.a)? ? ? ?#獲取a標(biāo)簽(第一個)

????<a class="sister" id="link1">Elsie</a>

print(soup.a.attrs)? ?#獲取第一個a標(biāo)簽的所有屬性,組成一個字典

????{'href': 'http://example.com/elsie', 'class': ['sister'], 'id': 'link1'}

print(soup.a.attrs['href'])? ? #獲取第一個a標(biāo)簽的href屬性

????'http://example.com/elsie'

print(soup.a.has_attr('class'))? ? ?#判斷class屬性是否存在

????True

print(soup.p)? ? #獲取p標(biāo)簽(第一個)

????<p class="title"><b>The Dormouse's story</b></p>

print(soup.p.children)? ? #獲取第一個p標(biāo)簽下的所有子節(jié)點(diǎn)

????<list_iterator object at 0x7fe8185261d0>

print(list(soup.p.children))

????[<b>The Dormouse's story</b>]

print(list(soup.p.children)[0])

????<b>The Dormouse's story</b>

print(list(soup.p.children)[0].text)

????"The Dormouse's story"

print(soup.find_all('a'))? ? #獲取所有的a標(biāo)簽

????[<a class="sister" id="link1">Elsie</a>, <a class="sister" id=a class="sister" id="link3">Tillie</a>]

for a in soup.find_all('a'):? ?#遍歷所有的a標(biāo)簽

? ? print(a.attrs['href'])

print(soup.find(id='link3'))? ? #獲取id=link3的標(biāo)簽

????<a class="sister" id="link3">Tillie</a>

print('#'*150)

#支持CSS選擇器

#查找類名為story的節(jié)點(diǎn)

print(soup.select('.story'))

print('')

print(soup.select('.story a'))

print('')

#查找id=link1的節(jié)點(diǎn)

print(soup.select('#link1'))

網(wǎng)站標(biāo)題:Python爬蟲之?dāng)?shù)據(jù)解析模塊bs4基礎(chǔ)
本文網(wǎng)址:http://www.muchs.cn/article42/gjggec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)商城網(wǎng)站、網(wǎng)站改版、App開發(fā)、搜索引擎優(yōu)化、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)