作者dynastat (dynastat)
看板Python
標題[問題] bs4 findAll method 後如何提取content?
時間Sun Feb 5 14:39:19 2017
大家好
我在練習用爬蟲爬yahoo字典的資料
原始碼如下:
import requests
from bs4 import BeautifulSoup
Url = requests.get('
http://tw.dictionary.search.yahoo.com/search?p=fat')
TargetHtml = BeautifulSoup(Url.text,"html.parser")
parser = TargetHtml.findAll('span', class_='fc-6th fz-15px')
print (parser.contents)
卻會出現
AttributeError: 'ResultSet' object has no attribute 'contents'
可是我看Stackoverflow 都是這樣提取內文的啊?
要如何正確提取內文呢?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.163.12.74
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1486276763.A.6D0.html
推 crazycy: parser[0].text # 'a.形容詞' 02/05 15:43
→ crazycy: parser[1].text # 'n.名詞' 02/05 15:44
→ crazycy: parser[2].text # 'vi.不及物動詞' 02/05 15:44
推 crazycy: parser可以視作包含Tag的list 02/05 15:47
→ crazycy: 從Tag取text就是那個節點的文字內容了 02/05 15:48