GET请求可用于postman,但不能用于python请求和curl

2024-10-04 03:28:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我已尝试使用python创建一个特定的GET请求来访问该站点: https://sgfm.elcorteingles.es/SGFM/dctm/MEDIA03/202006/24/00117731276964____5__210x210.jpg

但是我没有收到任何来自浏览器的响应,它一直在等待。 然后我尝试创建一个curl请求,同样的事情发生了(一直在等待)。 在所有这些之后,我尝试创建一个邮递员请求,它工作得非常完美!我不明白为什么它可以与postman一起使用,而不能与python和curl一起使用,因为所有这些平台都不像浏览器。 我使用postman方法将postman请求转换为python和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请求吗?甚至卷曲我也能处理


Tags: httpsurlgetes浏览器curlpostmanrequests
1条回答
网友
1楼 · 发布于 2024-10-04 03:28:17

服务器不接受您的标题。我试过这些,它对我有效

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(ur;)

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'))

相关问题 更多 >