我在bs4脚本上堆叠,我需要获得href链接或元内容,我怎么能做到呢?基本上我需要得到这个:
<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']
但我的代码不起作用。。。
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