【问题标题】:I have problem web scraping image; when I try my code, it just says []我有问题网络抓取图像;当我尝试我的代码时,它只是说 []
【发布时间】:2020-01-04 22:20:55
【问题描述】:

这是我的代码:

import requests
from bs4 import BeautifulSoup
r = requests.get('https://www.reddit.com/r/memes/')
soup = BeautifulSoup(r.content,'html.parser')
post = soup.find_all('img',attr={'alt:Post image'})

输出是[],但我不知道为什么。

【问题讨论】:

  • 因为你的 attr 参数不是字典,它是一个集合。

标签: python web-scraping beautifulsoup


【解决方案1】:

正如@jonrsharpe 在 cmets 中指出的那样,您将一个集合作为目标属性而不是字典传递。只需从字符串内部删除冒号即可设置字典的键和值:

post = soup.find_all('img',attr={'alt':'Post image'})

之后,你可以使用img[src]获取src链接,用urllib2之类的模块打开链接,然后下载你喜欢的内容。

希望这有帮助

【讨论】:

  • 试过了,输出还是[]。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 2020-09-19
  • 1970-01-01
相关资源
最近更新 更多