search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

beautifulsoup tag in tag 用法教學 - Python 教學筆記本


beautifulsoup tag 的 function到底要在什麼時機使用呢?
說真的有時候我自己都會搞混,所以幫大家整理出來

網頁結構



soup.title

指的是印出包含在title tag 那段html codeThe Dormouse's story

soup.title.name


會直接輸出title string
'title'

soup.title.string


多加string會輸出夾在title裡面的字串出來 'The Dormouse's story'

soup.title.parent.name


多加parent這個funtion的功能主要是要找出父輩的tag,意思就是有親子關係一般的上層tag(就是要找他老爸啦 !)輸出結果如下  'head'

soup.p

找到p tag 那段html代碼

The Dormouse's story



soup.p['class']


把藏在 p tag 裡面的atrribute class 字串給抽出來 'title'

soup.a

將 a tag 的html抽出來 Elsie

soup.find_all('a')


將所有網頁內含有a tag 的html全部抽出來 Elsie, Lacie, Tillie

soup.find(id="link3")

找到網頁內含有id 含有link3的html並將其抽出來 Tillie


如果想抽出a標籤的link該怎麼做呢?

for link in soup.find_all('a'): print(link.get('href')) http://example.com/elsie http://example.com/lacie http://example.com/tillie


熱門推薦

本文由 python-learnnotebookblogspotcom 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦