Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我有问题的网页抓取图像;当我尝试我的代码时,它只会说[]_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

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参数不是字典,而是集合。尝试此操作后,输出仍然为[]。