2016-08-18 39 views
0

我學會了如何從特定URL下載的圖片與蟒蛇爲:某些URL下載的圖片與Python

import urllib 
imgurl="http://www.digimouth.com/news/media/2011/09/google-logo.jpg" 
resource = urllib.urlopen(imgurl) 
output = open("test.jpg","wb") 
output.write(resource.read()) 
output.close() 

,效果不錯,但是當我改變的URL

imgurl="http://farm1.static.flickr.com/96/242125326_607a826afe_o.jpg" 

它不工作,並給了信息

File "face_down.py", line 3, in <module> 
resource = urllib2.urlopen(imgurl) 
File "D:\Python27\another\Lib\urllib2.py", line 154, in urlopen 
return opener.open(url, data, timeout) 
File "D:\Python27\another\Lib\urllib2.py", line 431, in open 
response = self._open(req, data) 
File "D:\Python27\another\Lib\urllib2.py", line 449, in _open 
'_open', req) 
File "D:\Python27\another\Lib\urllib2.py", line 409, in _call_chain 
result = func(*args) 
File "D:\Python27\another\Lib\urllib2.py", line 1227, in http_open 
return self.do_open(httplib.HTTPConnection, req) 
File "D:\Python27\another\Lib\urllib2.py", line 1197, in do_open 
raise URLError(err) 
urllib2.URLError: <urlopen error [Errno 10060] > 

,我試圖打開一圖像URL,它湊ld被顯示爲前者,我沒有想法解決它~~幫助~~~~

回答

0

我查了這兩個地址,第二個不會導致任何地方。這可能是問題所在。

import urllib 
imgurl="webpage url" 
openimg = urllib.urlopen(imgurl) #opens image (prepares it) 
img = open("test.jpg","wb") #opens the img to read it 
img.write(openimg.read()) #prints it to the console 
img.close() #closes img 

再次嘗試鏈接在您的網頁,如果它變成了「網頁不可用」,也可能是問題。

+0

謝謝!我會試試這個! –

+0

它仍然顯示錯誤,我嘗試了原始代碼,然後它的作品,很奇怪!有時候它有效,有時它不會 –

1

您可以嘗試使用請求模塊。該響應將是一些字節。所以,你可以遍歷這些字節塊並寫入文件。

import requests 

url = "http://farm1.static.flickr.com/96/242125326_607a826afe_o.jpg" 
r = requests.get(url) 
path = "filename.jpg" 
with open(path, 'wb') as f: 
    for chunk in r: 
     f.write(chunk) 
+0

哇,我會試試這個! –

+0

它仍然有錯誤10060,也許該網站無法下載TAT –