简体   繁体   English

无法从 requests.get 获取图像

[英]Cannot get an image from requests.get

I have 2 images that I want to process with some logic in python.我有 2 个图像,我想用 python 中的一些逻辑来处理。 Here the 2 URLs to the images:这是图像的 2 个 URL:

https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png https://www.hogan.com/fashion/hogan/HXW4350DM10NCR0RSZ/HXW4350DM10NCR0RSZ-02.png https: //upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png https://www.hogan.com/fashion/hogan/HXW4350DM10NCR0RSZ/HXW4350DM10NCR0RSZ-02.png

To get these images I wrote the following script:为了获得这些图像,我编写了以下脚本:

import requests
from PIL import Image
from io import BytesIO

url = "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png"

response = requests.get(url)
img = Image.open(BytesIO(response.content))

img.show()

This piece of code is ok and I correctly get the image.这段代码没问题,我正确地得到了图像。

But, with the second image, I cannot receive any response from the get method.但是,对于第二张图片,我无法从 get 方法收到任何响应。


url = "https://www.hogan.com/fashion/hogan/HXW4350DM10NCR0RSZ/HXW4350DM10NCR0RSZ-02.png"

response = requests.get(url)
img = Image.open(BytesIO(response.content))

img.show()

Any help would be appreciated.任何帮助,将不胜感激。

python 3.9.4 requests 2.25.1 python 3.9.4 请求 2.25.1

That's because the second url requires an important header parameter , the user-agent那是因为第二个 url需要一个重要的header 参数用户代理

Let's add it into your request:让我们将其添加到您的请求中:

import requests
from PIL import Image
from io import BytesIO

url = "https://www.hogan.com/fashion/hogan/HXW4350DM10NCR0RSZ/HXW4350DM10NCR0RSZ-02.png"

headers = {
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
}
response = requests.get(url, headers=headers)
img = Image.open(BytesIO(response.content))

img.show()

How do we know if some website requires User-agent?我们如何知道某个网站是否需要用户代理?
We just don't know, but we assume that if the browser can get the image properly and a simple request does not, it is missing something, and most of the websites requires headers to validate your request我们只是不知道,但我们假设如果浏览器可以正确获取图像而一个简单的请求却没有,那么它缺少一些东西,并且大多数网站都需要标头来验证您的请求

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

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