【发布时间】:2019-06-13 15:29:05
【问题描述】:
我无法保存从汤对象获取的图像,如果我将其复制并粘贴到浏览器中,图像源是正确的,但我似乎无法下载它
我使用BeautifulSoup 查找图像然后requests 下载它,我也尝试使用urllib.urlretrieve 下载它但没有成功最后我使用lxml.html 解析并获取图像并下载它使用二进制解码
import bs4,urllib2,requests
REGISTER_URL="http://example.webscraping.com/places/default/user/register?_next=/places/default/index%22"
html=urllib2.urlopen(REGISTER_URL)
soup=bs4.BeautifulSoup(html,"html.parser")
image=soup.find("img",src=True)
print image['src']
#print image['src']
response=requests.get(image['src'])
'''
f=open("Cas.jpg")
for block in response.iter_content(1024):
f.write(block)
f.close()
'''
我想知道为什么requests 和urllib.urlretrieve 下载它不起作用,注意:urllib.urlretrieve 下载黑色图像,而请求只会给出错误。
我的预期结果只是下载验证码图像
注意1:图片是来自Python web-scraping example的验证码,当然每次加载页面都会收到一张新图片。
注意2:这绝不是对网站的攻击或任何有害行为,本网站仅作为测试爬虫的示例。
【问题讨论】:
-
对于那些给予 -rep 的人,请在 cmets 中解释原因,以便我可以在未来处理我的“问题”
标签: python python-2.7 web-scraping beautifulsoup