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
[Errno socket error] (10060, 'Operation timed out')
for example:
import urllib
url = "http://farm3.static.flickr.co开发者_JS百科m/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~~~
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.
Consider using the urllib2
library instead, which allows you to specify the timeout (in Python 2.6+).
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)
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.")
精彩评论