我堆疊了 bs4 腳本,我需要獲取 href 鏈接或元內(nèi)容,我該怎么做?基本上我需要得到這個:<meta itemprop="image" content="https://resources.reed.co.uk/profileimages/logos/thumbs/Logo_71709.png?v=20200828172950">或者<img src="https://resources.reed.co.uk/profileimages/logos/thumbs/Logo_71709.png?v=20200828172950" alt="Posted by Publica Group " width="120" height="50" class=" b-loaded" style="display: inline;">我嘗試這樣做: logoscrap = soup.find('meta', attrs={'itemprop': 'image'})和 logoscrap = soup.find('img', class_="b-loaded").attrs['src']但我的代碼不起作用...
1 回答

叮當(dāng)貓咪
TA貢獻(xiàn)1776條經(jīng)驗 獲得超12個贊
soup.find 返回 dict 對象,您可以直接從 dict 訪問屬性
img = soup.find('meta', attrs={'itemprop': 'image'})
logoscrap = img['content']
#output:
https://resources.reed.co.uk/profileimages/logos/thumbs/Logo_71709.png?v=20200828172950
或者
img = soup.find('img', class_="b-loaded")
logoscrap = img['src']
#output:
https://resources.reed.co.uk/profileimages/logos/thumbs/Logo_71709.png?v=20200828172950
添加回答
舉報
0/150
提交
取消