简体   繁体   English

使用请求下载图像 url 问题

[英]Download image using requests url problem

I use this code to download image:我使用此代码下载图像:

import shutil
import requests
url = 'https://cdn.pixabay.com/photo/2017/09/25/11/55/cyberspace-2784907__340.jpg'

response = requests.get(url, stream=True)
with open('img.png', 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)
del response

and it works.它有效。 But when i use this type of link it dont work:但是当我使用这种类型的链接时它不起作用:

https://somesite.net/tor/poster.php/AOtJBawmVb0w7aHCjX5JmSo86CHVNKCFCz,KxohMSXIwMTIzNDU2Nzg5QUJDREVGR0hJSktMTU44N9qmOjPfy3FR58Dkq2XIpKhcjA1UPqrHlqXnX0kVs32sOTRn5f1QmKifHaeJ3Lwm80fo2bLATNETW1jZzTA=

if i put it in a web explorer, it shows, what is the best way to approach this?如果我把它放在 web 资源管理器中,它会显示,解决这个问题的最佳方法是什么?

I don't get an error when the image is downloaded, the file just says is not supported and i cannot get it to open in windows:下载图像时我没有收到错误,文件只是说不支持,我无法在 windows 中打开它:

https://i.imgur.com/I2CUZ2v.png https://i.imgur.com/I2CUZ2v.png

The link works: the link to image链接有效:图片链接

Maybe you should try to add也许你应该尝试添加

response.raw.decode_content = True

before

shutil.copyfileobj(response.raw, out_file)

If it does not work, could you please specify your problem and add correct "non-working" image URL?如果它不起作用,您能否指定您的问题并添加正确的“非工作”图像 URL?

The image may not be png image.图像可能不是 png 图像。 Check the first few bytes to see if it is png or jpeg or anything else.检查前几个字节以查看它是 png 还是 jpeg 或其他任何内容。

png: 89 50 4E 47 0D 0A 1A 0A PNG: 89 50 4E 47 0D 0A 1A 0A

jpeg: FF D8 FF E0 00 10 4A 46 49 46 00 01 or FF D8 FF EE or FF D8 FF E1?? ?? 45 78 69 66 00 00 jpeg: FF D8 FF E0 00 10 4A 46 49 46 00 01FF D8 FF EEFF D8 FF E1?? ?? 45 78 69 66 00 00 FF D8 FF E1?? ?? 45 78 69 66 00 00

bmp: 42 4D bmp: 42 4D

webp: 52 49 46 46?? ?? ?? ?? 57 45 42 50 webp: 52 49 46 46?? ?? ?? ?? 57 45 42 50 52 49 46 46?? ?? ?? ?? 57 45 42 50

where question marks ( ?? ) are random bytes.问号( ?? )是随机字节。

Check this out for full list of file signatures and there offsets检查此文件签名的完整列表和偏移量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM