Zi 字媒體
2017-07-25T20:27:27+00:00
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
寫了
5860316篇文章,獲得
23313次喜歡