问题描述
我正在尝试下载图像,但它似乎确实有效.是不是被ddos保护拦截了?
I'm trying to download an image, however it does seem to work. Is it being blocked by ddos protection?
代码如下:
urllib.request.urlretrieve("http://archive.is/Xx9t3/scr.png", "test.png")
基本上将该图像下载为test.png".我正在使用 python3,因此在 urlretrieve 之前使用了 urllib.request.
Basically download that image as "test.png." I'm using python3 hence the urllib.request before urlretrieve.
import urllib.request
也有.
有什么办法可以下载图片吗?谢谢!
Any way I can download the image? thanks!
推荐答案
出于我无法想象的原因,服务器需要一个众所周知的用户代理.所以你必须假装使用例如 firefox 并且它会接受发送图像:
For reasons that I cannot even imagine, the server requires a well known user agent. So you must pretend to use for example firefox and it will accept to send the image:
# first build a request object
req = urllib.request.Request("http://archive.is/Xx9t3/scr.png",
headers = {
'User-agent':
'Mozilla/5.0 (Windows NT 5.1; rv:43.0) Gecko/20100101 Firefox/43.0'})
#then use it
resp = urllib.request.urlopen(req)
with open("test.png","wb") as fd:
fd.write(resp.read())
相当愚蠢,但是当服务器管理员发疯时,就像他一样愚蠢......
Rather stupid, but when a server admin goes mad, just be as stupid as he is...
这篇关于urlretrieve 不适用于此站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!