2017-05-27 54 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