2017-05-27 55 views
1

我目前遇到的問題是試圖下載顯示爲動畫gif的圖像,但顯示編碼爲jpg。我說它似乎編碼爲jpg,因爲文件擴展名和mime類型都是.jpg添加圖像/ jpeg。Python3下載網址錯誤編碼的圖像

在下載文件到我的本地機(Mac OSX版),然後試圖打開我得到了錯誤的文件:

The file could not be opened. It may be damaged or use a file format that Preview doesn’t recognize. 

雖然我知道有些人會也許只是忽略圖像,如果可以修復,我正在尋找一個解決方案來做到這一點,而不是忽略它。

相關網址是在這裏:

http://www.supergrove.com/wp-content/uploads/2017/03/gif-images-22-1000-about-gif-on-pinterest.jpg 

這裏是我的代碼,我願意接受建議:

from PIL import Image 
import requests 

response = requests.get(media, stream = True) 
response.raise_for_status() 

with open(uploadedFile, 'wb') as img: 
    for chunk in response.iter_content(chunk_size=1024): 
     if chunk: 
      img.write(chunk) 
    img.close() 
+0

如果你去找到並更改文件擴展名怎麼辦? –

+0

您是否嘗試通過右鍵單擊下載 - >將圖像另存爲,並查看它是否打開?在我的情況下(Debian 8),firefox正確打開它。 – raratiru

+0

@ whackamadoodle3000沒有區別。那是我嘗試的第一件事情之一。還嘗試更改文件擴展名以將文件保存到磁盤之前。 – stwhite

回答

1

在這種情況下必須回答我自己的問題,但對這個問題的答案是爲請求添加referer。很可能是一個htaccess文件阻止在映像的服務器上直接訪問文件,除非請求來自他們自己的服務器。

from fake_useragent import UserAgent 
from io import StringIO,BytesIO 
import io 
import imghdr 
import requests 

# Set url 
mediaURL = 'http://www.supergrove.com/wp-content/uploads/2017/03/gif-images-22-1000-about-gif-on-pinterest.jpg' 

# Create a user agent 
ua = UserAgent() 

# Create a request session 
s = requests.Session() 

# Set some headers for the request 
s.headers.update({ 'User-Agent': ua.chrome, 'Referrer': media }) 


# Make the request to get the image from the url 
response = s.get(mediaURL, allow_redirects=False) 


# The request was about to be redirected 
if response.status_code == 302: 

    # Get the next location that we would have been redirected to 
    location = response.headers['Location'] 

    # Set the previous page url as referer 
    s.headers.update({'referer': location}) 

    # Try the request again, this time with a referer 
    response = s.get(mediaURL, allow_redirects=False, cookies=response.cookies) 

    print(response.headers) 

帽尖@raratiru用於建議使用allow_redirects

在他們的回答中還指出,圖片的服務器可能會故意阻止訪問,以防止一般刮板查看他們的圖像。很難說,但無論如何,這個解決方案的工作。

1

Wheregoes,圖像的鏈接:

  • http://www.supergrove.com/wp-content/uploads/2017/03/gif-images-22-1000-about-gif-on-pinterest.jpg

收到302重定向到包含它的頁面:

  • http://www.supergrove.com/gif-images/gif-images-22-1000-about-gif-on-pinterest/

因此,您的代碼試圖下載一個網頁爲圖像。

tried

r = requests.get(the_url, headers=headers, allow_redirects=False)

但它返回零含量和status_code = 302

(事實上,這是顯而易見的,應該發生...)

這個服務器是一種方式,它永遠不會滿足該請求配置。

繞過這種限制聽起來很不對頭非常困難,盡我所能的限制知識。

+0

我試圖使用'allow_redirects = False'不幸的是仍然沒有圖像標題:'{'Server':'nginx', 'Date':'Mon,29 May 2017 22:15:29 GMT','Content-Type':'text/html;字符串= UTF-8','Content-Length':'0','Connection':'keep-alive','Keep-Alive':'timeout = 60','X-Powered-By' 5.6.30','Location':'http://www.supergrove.com/gif-images/gif-images-22-1000-about-gif-on-pinterest/'}' – stwhite

+0

在這一點上,我是真的不確定。我甚至試圖阻止重定向,抓取cookie,然後再次請求cookie,但即使這似乎並沒有工作(我假設需要cookie來訪問圖像 - 可能防止網頁刮板)。 – stwhite

+0

@stwhite很明顯,這些人不想直接訪問圖像。 'allow_redirect = False'返回零內容和'status_code = 302'。我不確定是否可以繞過這種情況,而不要求他們直接訪問服務器的設置! – raratiru