简体   繁体   English

为什么Python中的“请求”无法下载此网址图片,但浏览器可以下载?

[英]Why is “requests” in Python unable to download this url image but the browser can?

I have the following url that is actually an image. 我有以下实际上是图像的网址。

https://cache.mrporter.com/images/products/908290/908290_mrp_in_l.jpg

I am using the below Python code to download it 我正在使用下面的Python代码下载它

import numpy as np
from PIL import Image
import requests
response = requests.get("https://cache.mrporter.com/images/products/908290/908290_mrp_in_l.jpg")

I see that the requests.get just hangs and if I use a timeout of even 5 seconds I get the TimeOut Exception. 我看到request.get只是挂起,并且如果我使用甚至5秒的超时,我都会收到TimeOut Exception。

However, when I paste the url in the browser, I immediately see it open. 但是,当我将URL粘贴到浏览器中时,我立即看到它已打开。

I am really stuck here and trying to understand what the problem is. 我真的被困在这里,试图了解问题所在。

It looks like the website needs an User-Agent . 网站似乎需要一个User-Agent You could try with the following 您可以尝试以下

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
response = requests.get("https://cache.mrporter.com/images/products/908290/908290_mrp_in_l.jpg", headers=headers)

This returns <Response [200]> 返回<Response [200]>

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

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