PythonBeautifulSoup模塊使用教程詳解_第1頁
PythonBeautifulSoup模塊使用教程詳解_第2頁
PythonBeautifulSoup模塊使用教程詳解_第3頁
PythonBeautifulSoup模塊使用教程詳解_第4頁
PythonBeautifulSoup模塊使用教程詳解_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第PythonBeautifulSoup模塊使用教程詳解目錄一、模塊簡介二、方法利用1、引入模塊2、幾個簡單的瀏覽結構化數據的方法三、具體利用1、獲取擁有指定屬性的標簽2、獲取標簽的屬性值3、獲取標簽中的內容4、stripped_strings四、輸出1、格式化輸出prettify()2、get_text()

一、模塊簡介

BeautifulSoup是一個可以從HTML或XML文件中提取數據的Python庫.它能夠通過你喜歡的轉換器實現慣用的文檔導航,查找,修改文檔的方式.BeautifulSoup會幫你節省數小時甚至數天的工作時間.

二、方法利用

1、引入模塊

#引入

html_doc="""

htmlheadtitleTheDormouse'sstory/title/head

body

pbTheDormouse'sstory/b/p

pOnceuponatimetherewerethreelittlesisters;andtheirnameswere

ahref="/elsie"rel="externalnofollow"id="link1"Elsie/a,

ahref="/lacie"rel="externalnofollow"id="link2"Lacie/aand

ahref="/tillie"rel="externalnofollow"id="link3"Tillie/a

andtheylivedatthebottomofawell./p

p.../p

frombs4importBeautifulSoup

soup=BeautifulSoup(html_doc,'html.parser')

四種解析器

2、幾個簡單的瀏覽結構化數據的方法

#獲取Tag,通俗點就是HTML中的一個個標簽

soup.title#獲取整個title標簽字段:titleTheDormouse'sstory/title

#獲取title標簽名稱:title

#獲取title的父級標簽名稱:head

soup.p#獲取第一個p標簽字段:pbTheDormouse'sstory/b/p

soup.p['class']#獲取第一個p中class屬性值:title

soup.p.get('class')#等價于上面

soup.a#獲取第一個a標簽字段

soup.find_all('a')#獲取所有a標簽字段

soup.find(id="link3")#獲取屬性id值為link3的字段

soup.a['class']="newClass"#可以對這些屬性和內容等等進行修改

delbs.a['class']#還可以對這個屬性進行刪除

soup.find('a').get('id')#獲取class值為story的a標簽中id屬性的值

soup.title.string#獲取title標簽的值:TheDormouse'sstory

三、具體利用

1、獲取擁有指定屬性的標簽

方法一:獲取單個屬性

soup.find_all('div',id="even")#獲取所有id=even屬性的div標簽

soup.find_all('div',attrs={'id':"even"})#效果同上

soup.find_all('div',id="even",class_="square")#獲取所有id=even并且'div',attrs={"id":"even","class":"square"})#效果同上

2、獲取標簽的屬性值

方法一:通過下標方式提取

forlinkinsoup.find_all('a'):

print(link['href'])//等同于print(link.get('href'))

方法二:利用attrs參數提取

forlinkinsoup.find_all('a'):

print(link.attrs['href'])

3、獲取標簽中的內容

divs=soup.find_all('div')#獲取所有的div標簽

fordivindivs:#循環遍歷div中的每一個div

a=div.find_all('a')[0]#查找div標簽中的第一個a標簽

print(a.string)#輸出a標簽中的內容

如果結果沒有正確顯示,可以轉換為list列表

4、stripped_strings

去除\n換行符等其他內容stripped_strings

divs=soup.find_all('div')

fordivindivs:

infos=list(div.stripped_strings)#去掉空格換行等

bring(infos)

四、輸出

1、格式化輸出prettify()

prettify()方法將BeautifulSoup的文檔樹格式化后以Unicode編碼輸出,每個XML/HTML標簽都獨占一行

markup='ahref="/"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"Ilinkedtoi/i/a'

soup=BeautifulSoup(markup)

soup.prettify()

#'html\nhead\n/head\nbody\nahref="/"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"\n...'

print(soup.prettify())

#html

#head

#/head

#body

#ahref="/"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"

#Ilinkedto

#

#/i

#/a

#/body

#/html

2、get_text()

如果只想得到tag中包含的文本內容,那么可以調用get_text()方法,這個方法獲取到tag中包含的所有文版內容包括子孫tag中的內容,并將結果作為Unicode字符串返回:

markup='ahref="/"rel="externalnofollow"rel="externalnofollow"rel

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論