繁体   English   中英

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

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

我有一个问题,从 flickr.com 下载图像时,python 函数urllib.urlretrieve()总是返回错误

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

例如:

import urllib

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

我是中国人,不知道“超时”与中国网速有没有关系。

现在它无法关闭 .gif! 我该怎么办? 谢啦~~~

任何建议表示赞赏~~~

我无法重现。

完全相同的代码下载了图片。

我正在使用 python 2.7

它必须与服务器(当时)或您的互联网连接有关。

在此处输入图片说明

您无法从 flickr 下载图像的原因是,中国有一道该死的墙挡住了您! 您可以尝试使用在您的计算机上全局运行的 VPN(这样您的 python 程序也可以在此 VPN 环境下运行),或者,

你设置了代理,比如说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)

尝试“获取”方法。 我最近不得不做同样的事情,我解决了以下问题:

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