【问题标题】:GET request works with postman but not with python requests and curlGET 请求适用于邮递员,但不适用于 python 请求和 curl
【发布时间】:2020-11-08 10:06:48
【问题描述】:

我试图在 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 请求?甚至 curl 我也可以处理。

【问题讨论】:

  • 你必须使用requests.get(url, ...)而不是requests.request()
  • 网站/服务器正在阻止来自 curl/requests 等客户端的调用。
  • @FishingCode 使用 .get() 而不是 .request("url", ...) 有什么区别?

标签: python curl python-requests postman urllib


【解决方案1】:

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

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

【讨论】:

  • 只需要 User-Agent 标头。其他可以跳过。
  • @Aleksander Ikleiw 它对我有用一次,然后我得到了 403 错误
  • @AleksanderIkleiw 我可以对这个块做些什么吗?
  • @lby 您的 IP 可能被阻止或服务器不接受来自您 IP 的请求。尝试使用代理。
  • 完美运行
猜你喜欢
  • 2019-07-03
  • 2021-10-06
  • 2020-06-13
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 2019-08-28
  • 2021-04-13
相关资源
最近更新 更多