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

发布于 2024-12-09 13:55:20 字数 415 浏览 0 评论 0原文

我有一个问题,当从 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!我该怎么办?谢谢~~~

任何建议都表示赞赏~~~

I have a problem that when dowloading a image from flickr.com,the python function urllib.urlretrieve() always return an error

[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! what should i do about this? THX~~~

Any suggestion is appreciated~~~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隐诗 2024-12-16 13:55:20

我无法重现。

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

我正在使用 python 2.7

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

在此处输入图像描述

I can't reproduce.

The exact same code downloaded the picture.

I'm using python 2.7

It has to do either with the server (at that time) or with your internet connection.

enter image description here

我的痛♀有谁懂 2024-12-16 13:55:20

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

Consider using the urllib2 library instead, which allows you to specify the timeout (in Python 2.6+).

时光礼记 2024-12-16 13:55:20

你无法从 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)

The reason you cannot download the image from flickr is that, China has a freaking WALL that's blocking you! 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,

you set up the proxies in, let's say requests, then you can download images from those websites that are blocked from China.

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)
花开浅夏 2024-12-16 13:55:20

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

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.")

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.")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文