簡體   English   中英

為什么Python中的“請求”無法下載此網址圖片,但瀏覽器可以下載?

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

我有以下實際上是圖像的網址。

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

我正在使用下面的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")

我看到request.get只是掛起,並且如果我使用甚至5秒的超時,我都會收到TimeOut Exception。

但是,當我將URL粘貼到瀏覽器中時,我立即看到它已打開。

我真的被困在這里,試圖了解問題所在。

網站似乎需要一個User-Agent 您可以嘗試以下

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)

返回<Response [200]>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM