簡體   English   中英

GET 請求適用於 postman 但不適用於 python 請求和 curl

[英]GET request works with postman but not with python requests and curl

我試圖在 python 中創建一個特定的 GET 請求到該站點: https://sgfm.elcorteingles.es/SGFM/dctm/MEDIA03/202006/24/00117731271964__

但我沒有得到瀏覽器的任何響應,它一直在等待。 然后我嘗試創建一個 curl 請求,同樣的事情發生了(永遠等待)。 畢竟,我嘗試創建一個 POSTMAN 請求,並且效果很好。 And I did not understood why it works with postman but not with python and curl as all these platforms are not browser like: I used the postman method to convert postman request to python and curl:

#python
import requests

url = "https://sgfm.elcorteingles.es/SGFM/dctm/MEDIA03/202006/24/00117731276964____5__210x210.jpg"

payload = {}
headers= {}

response = requests.request("GET", url, headers=headers, json = payload)

print(response.text.encode('utf8'))

#curl

curl --location --request GET 'https://sgfm.elcorteingles.es/SGFM/dctm/MEDIA03/202006/24/00117731276964____5__210x210.jpg'

但他們都沒有得到任何回應。 有誰知道為什么會發生這種情況並將其轉換為 python 請求? 甚至 curl 我也可以處理。

服務器不接受您的標頭。 我試過這些,它對我有用。

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36', "Upgrade-Insecure-Requests": "1","DNT": "1","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language": "en-US,en;q=0.5","Accept-Encoding": "gzip, deflate"}

此外,您需要將您發出的請求更改為requests.requests('GET')不是發出GET請求的正確方法。 正確的是requests.get(url) 它們是相同的方法,但對於更簡潔的代碼,您應該堅持使用requests.get(url)

import requests

url = "https://sgfm.elcorteingles.es/SGFM/dctm/MEDIA03/202006/24/00117731276964____5__210x210.jpg"

payload = {}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36', "Upgrade-Insecure-Requests": "1","DNT": "1","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language": "en-US,en;q=0.5","Accept-Encoding": "gzip, deflate"}

response = requests.get(url, headers=headers)

print(response.text.encode('utf8'))

可能是標題,但也可能是 Postman 使用系統代理。 使用 python 設置相同的代理

url = "https://example.com"
headers = {'User-Agent':...}
proxies = {"https":"<same postman proxy>"}
response = requests.get(url, headers=headers, proxies=proxies)

或者

url = "https://example.com"
s = requests.Session()
s.proxies.update({"https":"<same postman proxy>"})
s.headers.update({"User-Agent":...})
s.get(url)

暫無
暫無

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

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