pythonbs4函數(shù),pythonbs4模塊

python中bs4.beautiful解析有時(shí)候不加前邊的bs4 有什么區(qū)別?

這個(gè)就是看你是如何import包的

創(chuàng)新互聯(lián)公司專注于三沙企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),商城網(wǎng)站制作。三沙網(wǎng)站建設(shè)公司,為三沙等地區(qū)提供建站服務(wù)。全流程定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

如果是import bs4,那么就應(yīng)該是bs4.BeautifulSoup

如果是from bs4 import?BeautifulSoup,就可以直接使用BeautifulSoup

python bs4 的使用

html_doc = """

html

head

titleThe Dormouse's story/title

/head

body

p class="title aq"

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="" class="sister" id="link1"Elsie/a,

a href="" class="sister" id="link2"Lacie/a

and

a href="" class="sister" id="link3"Tillie/a;

and they lived at the bottom of a well.

./p

p class="story".../p

"""

html字符串創(chuàng)建BeautifulSoup對(duì)象 :

soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')

輸出第一個(gè) title 標(biāo)簽 :

print soup.title

輸出第一個(gè) title 標(biāo)簽的標(biāo)簽名稱 :

print soup.title.name

輸出第一個(gè) title 標(biāo)簽的包含內(nèi)容 :

print soup.title.string

輸出第一個(gè) title 標(biāo)簽的父標(biāo)簽的標(biāo)簽名稱 :

print soup.title.parent.name

輸出第一個(gè) p 標(biāo)簽

print soup.p

輸出第一個(gè) p 標(biāo)簽的 class 屬性內(nèi)容 :

print soup.p['class']

輸出第一個(gè) a 標(biāo)簽的 href 屬性內(nèi)容 :

print soup.a['href']

'''''

soup的屬性可以被添加,刪除或修改. 再說一次, soup的屬性操作方法與字典一樣

'''

修改第一個(gè) a 標(biāo)簽的href屬性為

soup.a['href'] = ' '

給第一個(gè) a 標(biāo)簽添加 name 屬性 :

soup.a['name'] = u'百度'

刪除第一個(gè) a 標(biāo)簽的 class 屬性為 :

del soup.a['class']

輸出第一個(gè) p 標(biāo)簽的所有子節(jié)點(diǎn) :

print soup.p.contents

輸出第一個(gè) a 標(biāo)簽 :

print soup.a

輸出所有的 a 標(biāo)簽,以列表形式顯示 :

print soup.find_all('a')

輸出第一個(gè) id 屬性等于 link3 的 a 標(biāo)簽 :

print soup.find(id="link3")

獲取所有文字內(nèi)容 :

print(soup.get_text())

輸出第一個(gè) a 標(biāo)簽的所有屬性信息 :

print soup.a.attrs

for link in soup.find_all('a'):

獲取 link 的 href 屬性內(nèi)容

print(link.get('href'))

對(duì)soup.p的子節(jié)點(diǎn)進(jìn)行循環(huán)輸出 :

for child in soup.p.children:

print(child)

正則匹配,名字中帶有b的標(biāo)簽 :

for tag in soup.find_all(re.compile("b")):

print(tag.name)

import bs4#導(dǎo)入BeautifulSoup庫(kù)

Soup = BeautifulSoup(html)#其中html 可以是字符串,也可以是句柄

需要注意的是,BeautifulSoup會(huì)自動(dòng)檢測(cè)傳入文件的編碼格式,然后轉(zhuǎn)化為Unicode格式

通過如上兩句話,BS自動(dòng)把文檔生成為如上圖中的解析樹。

python2.5可以調(diào)用bs4嗎

一般情況下都是通過import腳本,然后直接調(diào)用腳本里的函數(shù),調(diào)用函數(shù)就可以直接傳遞參數(shù);因?yàn)镻ython并不像C語(yǔ)言那樣有main函數(shù)。 import B(腳本名稱)B.hello(參數(shù)A,參數(shù)B)

文章題目:pythonbs4函數(shù),pythonbs4模塊
轉(zhuǎn)載來源:http://muchs.cn/article0/hcipoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、品牌網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化網(wǎng)站內(nèi)鏈、手機(jī)網(wǎng)站建設(shè)網(wǎng)站設(shè)計(jì)公司

廣告

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

小程序開發(fā)