简体   繁体   English

如何通过 Python urllib.urlretrieve() 在 flickr 中下载图像?

[英]How to download a image in flickr by Python urllib.urlretrieve()?

I have a problem that when dowloading a image from flickr.com,the python function urllib.urlretrieve() always return an error我有一个问题,从 flickr.com 下载图像时,python 函数urllib.urlretrieve()总是返回错误

[Errno socket error] (10060, 'Operation timed out')

for example:例如:

import urllib

url = "http://farm3.static.flickr.com/2659/4207030547_23e6000d29_o.gif"
urllib.urlretrieve(url,"C://tmp.gif")

I am Chinese,and I dont know if the "time out" has anything to do with the speed of the the internet in China.我是中国人,不知道“超时”与中国网速有没有关系。

Now it falied in downing the .gif!现在它无法关闭 .gif! what should i do about this?我该怎么办? THX~~~谢啦~~~

Any suggestion is appreciated~~~任何建议表示赞赏~~~

I can't reproduce.我无法重现。

The exact same code downloaded the picture.完全相同的代码下载了图片。

I'm using python 2.7我正在使用 python 2.7

It has to do either with the server (at that time) or with your internet connection.它必须与服务器(当时)或您的互联网连接有关。

在此处输入图片说明

The reason you cannot download the image from flickr is that, China has a freaking WALL that's blocking you!您无法从 flickr 下载图像的原因是,中国有一道该死的墙挡住了您! You can try to use a VPN that works globally on your computer (so that your python program also runs under this VPN environment), or,您可以尝试使用在您的计算机上全局运行的 VPN(这样您的 python 程序也可以在此 VPN 环境下运行),或者,

you set up the proxies in, let's say requests , then you can download images from those websites that are blocked from China.你设置了代理,比如说requests ,然后你可以从那些被中国阻止的网站下载图像。

import requests

proxies = {
 “http”: “http://10.10.10.10:8000”,   # just an example
 “https”: “http://10.10.10.10:8000”,  # just an example
}
r = requests.get(“http://example_url.com”, proxies=proxies)

Try the "get" method.尝试“获取”方法。 I have recently had to do the same thing and I solved the problem with the following:我最近不得不做同样的事情,我解决了以下问题:

import requests
url = "https://www.python.org/static/community_logos/python-logo-master-v3-TM.png"
try:
    r = requests.get(url,allow_redirects = True)
    if(r.status_code == 200):
        open("Image.jpg","wb").write(r.content)
    elif(r.status_code == 404):
        print("Image not found at the URL")
    else:
        print("Unknown error occurred.")
except:
    print("Could not establish connection with the site URL.")

考虑改用urllib2库,它允许您指定超时(在 Python 2.6+ 中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM