Python 我有问题的网页抓取图像;当我尝试我的代码时,它只会说[]
这是我的代码:Python 我有问题的网页抓取图像;当我尝试我的代码时,它只会说[],python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,这是我的代码: 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'}) 输出是[],但我不知道为什么 正如@jornsharpe在评论中指出的,您正在传递一个se
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'})
输出是
[]
,但我不知道为什么 正如@jornsharpe在评论中指出的,您正在传递一个set作为目标属性,而不是字典。只需从字符串内部删除冒号即可设置dict的键和值:
post=soup.find_all('img',attr={'alt':'post image})
之后,您可以使用img[src]
获取src链接,使用类似urllib2的模块打开链接,然后随意下载内容
希望这有帮助因为您的attr参数不是字典,而是集合。尝试此操作后,输出仍然为[]。