[問題] bs4 findAll method 後如何提取content?

看板Python作者 (dynastat)時間8年前 (2017/02/05 14:39), 編輯推噓2(203)
留言5則, 1人參與, 最新討論串1/1
大家好 我在練習用爬蟲爬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

02/05 15:43, , 1F
parser[0].text # 'a.形容詞'
02/05 15:43, 1F

02/05 15:44, , 2F
parser[1].text # 'n.名詞'
02/05 15:44, 2F

02/05 15:44, , 3F
parser[2].text # 'vi.不及物動詞'
02/05 15:44, 3F

02/05 15:47, , 4F
parser可以視作包含Tag的list
02/05 15:47, 4F

02/05 15:48, , 5F
從Tag取text就是那個節點的文字內容了
02/05 15:48, 5F
文章代碼(AID): #1ObiYRRG (Python)
文章代碼(AID): #1ObiYRRG (Python)