为什么此请求在 Insomnia/Postman 中有效,但在 Python 请求中无效?

问题描述 投票:0回答:2

这是 cURL 命令,工作正常并返回数据:

curl --request GET \
  --url 'https://api.reserveamerica.com/jaxrs-json/products/AK/1180246?prdId=2400&detailed=true&gal=14&fc=true&gad=2023-06-27&next=false&displayGISMap=true' \
  --header 'Accept: application/json' \
  --header 'Accept-Encoding: gzip, deflate, br' \
  --header 'Accept-Language: en-US,en;q=0.5' \
  --header 'Authorization: eyJraWQiOiJiOUZPc0pUblF1NTRBdVEzWlR1NE55Ylkzakp3cVg5WFJZZ3NvQlBDOThBPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJjNDVlZjJkMy0yZjc4LTRhZTUtODcxNS1iODM1Yjk0OTVmNTMiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYWRkcmVzcyI6eyJmb3JtYXR0ZWQiOiJBRERSRVNTIDExMTEifSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLnVzLWVhc3QtMS5hbWF6b25hd3MuY29tXC91cy1lYXN0LTFfbHVzbDZPSmFkIiwicGhvbmVfbnVtYmVyX3ZlcmlmaWVkIjp0cnVlLCJjb2duaXRvOnVzZXJuYW1lIjoiYzQ1ZWYyZDMtMmY3OC00YWU1LTg3MTUtYjgzNWI5NDk1ZjUzIiwib3JpZ2luX2p0aSI6ImRhNWU0NGI4LWE5YjUtNDQyNi1iNTIxLWRhODNiZWRhM2EzMyIsImF1ZCI6IjM3NGY1aGxiaGJwNnJhczZmNW5haTRwNDIwIiwiZXZlbnRfaWQiOiIwZWYyYWI2Ni02MGM2LTQ3N2MtYTQyNS03Zjg0ZTNiM2JmZjkiLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTY4NjY5NjMzNiwibmFtZSI6Ikd1ZXN0VXNlciIsInBob25lX251bWJlciI6IisxNTU1NTU1NTU1NSIsImV4cCI6MTY4NjY5OTkzNiwiaWF0IjoxNjg2Njk2MzM2LCJqdGkiOiI5MGVhNTU4YS05YWQ2LTQxMWMtYWFmMS1mYzE4YzZlZjE0OTIiLCJlbWFpbCI6InByb2RndWVzdHVzZXJAYXNwaXJhY29ubmVjdC5jb20ifQ.b1d--A6EMth6jz5f0yMJjRW6IlIzZSUdUWT-AIk1ElduvuTi7AxGQu7pJbxC8MMZrX75k0xg8tXcKhyc85BwrME1lBoZ-hCejPGKbqgku3aiHLyMU0X5A0-IaLLblW1FnvQktEQT_ukbs-vASUSnNIALCZZUG25SIpeWxe3XcZrX7n1I60jIGN8ZSAre4l-5lOz5fmFyuT7Ac4fREByBNczng6Whr5Z6PqeMzRbqXuFAeNhHEuwfBWSwcOEZF63F0pkwnf2NzhXkukMq5pbMfDJ57uqoWGehTVpkLqoCitOLDM97B5UtWttBCxEIuvU_hH6Hcqm-stEYnondSJO5jQ' \
  --header 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0' \
  --header 'a1Data: {"sessionID":"4D441B5AE76AE0D435FBE1F3A32380FE.ue1-prod-aspiraone-web-10","sessionType":"SELFSERVICE","maxInactive":1800000,"AWSALB":"3MbtREdsquTV2MIZtkGpTIuB8kVWmwiOf+Q9GK0mWy6OsjQ8VBr6sSGzhiE2Xa/iPoTilVAH0sML5EK6NMLgipGThJ+z6rQKpGj5e2HI2Y2/6xjyrHr+ViXh58I4","AWSALBCORS":"3MbtREdsquTV2MIZtkGpTIuB8kVWmwiOf+Q9GK0mWy6OsjQ8VBr6sSGzhiE2Xa/iPoTilVAH0sML5EK6NMLgipGThJ+z6rQKpGj5e2HI2Y2/6xjyrHr+ViXh58I4"}'

对应的requests版本,返回400且无数据:

import requests

url = "https://api.reserveamerica.com/jaxrs-json/products/AK/1180246"

querystring = {
    "prdId": "2400",
    "detailed": "true",
    "gal": "14",
    "fc": "true",
    "gad": "2023-06-27",
    "next": "false",
    "displayGISMap": "true",
}

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0",
    "Accept": "application/json",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br",
    "a1Data": '{"sessionID":"4D441B5AE76AE0D435FBE1F3A32380FE.ue1-prod-aspiraone-web-10","sessionType":"SELFSERVICE","maxInactive":1800000,"AWSALB":"3MbtREdsquTV2MIZtkGpTIuB8kVWmwiOf+Q9GK0mWy6OsjQ8VBr6sSGzhiE2Xa/iPoTilVAH0sML5EK6NMLgipGThJ+z6rQKpGj5e2HI2Y2/6xjyrHr+ViXh58I4","AWSALBCORS":"3MbtREdsquTV2MIZtkGpTIuB8kVWmwiOf+Q9GK0mWy6OsjQ8VBr6sSGzhiE2Xa/iPoTilVAH0sML5EK6NMLgipGThJ+z6rQKpGj5e2HI2Y2/6xjyrHr+ViXh58I4"}',
    "Authorization": "eyJraWQiOiJiOUZPc0pUblF1NTRBdVEzWlR1NE55Ylkzakp3cVg5WFJZZ3NvQlBDOThBPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJjNDVlZjJkMy0yZjc4LTRhZTUtODcxNS1iODM1Yjk0OTVmNTMiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYWRkcmVzcyI6eyJmb3JtYXR0ZWQiOiJBRERSRVNTIDExMTEifSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLnVzLWVhc3QtMS5hbWF6b25hd3MuY29tXC91cy1lYXN0LTFfbHVzbDZPSmFkIiwicGhvbmVfbnVtYmVyX3ZlcmlmaWVkIjp0cnVlLCJjb2duaXRvOnVzZXJuYW1lIjoiYzQ1ZWYyZDMtMmY3OC00YWU1LTg3MTUtYjgzNWI5NDk1ZjUzIiwib3JpZ2luX2p0aSI6ImRhNWU0NGI4LWE5YjUtNDQyNi1iNTIxLWRhODNiZWRhM2EzMyIsImF1ZCI6IjM3NGY1aGxiaGJwNnJhczZmNW5haTRwNDIwIiwiZXZlbnRfaWQiOiIwZWYyYWI2Ni02MGM2LTQ3N2MtYTQyNS03Zjg0ZTNiM2JmZjkiLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTY4NjY5NjMzNiwibmFtZSI6Ikd1ZXN0VXNlciIsInBob25lX251bWJlciI6IisxNTU1NTU1NTU1NSIsImV4cCI6MTY4NjY5OTkzNiwiaWF0IjoxNjg2Njk2MzM2LCJqdGkiOiI5MGVhNTU4YS05YWQ2LTQxMWMtYWFmMS1mYzE4YzZlZjE0OTIiLCJlbWFpbCI6InByb2RndWVzdHVzZXJAYXNwaXJhY29ubmVjdC5jb20ifQ.b1d--A6EMth6jz5f0yMJjRW6IlIzZSUdUWT-AIk1ElduvuTi7AxGQu7pJbxC8MMZrX75k0xg8tXcKhyc85BwrME1lBoZ-hCejPGKbqgku3aiHLyMU0X5A0-IaLLblW1FnvQktEQT_ukbs-vASUSnNIALCZZUG25SIpeWxe3XcZrX7n1I60jIGN8ZSAre4l-5lOz5fmFyuT7Ac4fREByBNczng6Whr5Z6PqeMzRbqXuFAeNhHEuwfBWSwcOEZF63F0pkwnf2NzhXkukMq5pbMfDJ57uqoWGehTVpkLqoCitOLDM97B5UtWttBCxEIuvU_hH6Hcqm-stEYnondSJO5jQ",
}

response = requests.request("GET", url, headers=headers, params=querystring)

print(response)
web-scraping python-requests postman insomnia
2个回答
0
投票

您可以使用 http.client 或 aiohttp,两者都可以工作,至少在我的用例中是这样。 我使用 Insomnia,因此我可以在软件内生成代码,但您可以使用像 this 这样的网站。

使用 http.client 尝试一下:

import http.client

conn = http.client.HTTPSConnection("api.reserveamerica.com")

payload = ""

headers = {
    'Accept': "application/json",
    'Accept-Encoding': "gzip, deflate, br",
    'Accept-Language': "en-US,en;q=0.5",
    'Authorization': "eyJraWQiOiJiOUZPc0pUblF1NTRBdVEzWlR1NE55Ylkzakp3cVg5WFJZZ3NvQlBDOThBPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJjNDVlZjJkMy0yZjc4LTRhZTUtODcxNS1iODM1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ehTVpkLqoCitOLDM97B5UtWttBCxEIuvU_hH6Hcqm-stEYnondSJO5jQ",
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0",
    'a1Data': '{"sessionID":"4D441B5AE76AE0D435FBE1F3A32380FE.ue1-prod-aspiraone-web-10","sessionType":"SELFSERVICE","maxInactive":1800000,"AWSALB":"3MbtREdsquTV2MIZtkGpTIuB8kVWmwiOf+Q9GK0mWy6OsjQ8VBr6sSGzhiE2Xa/iPoTilVAH0sML5EK6NMLgipGThJ+z6rQKpGj5e2HI2Y2/6xjyrHr+ViXh58I4","AWSALBCORS":"3MbtREdsquTV2MIZtkGpTIuB8kVWmwiOf+Q9GK0mWy6OsjQ8VBr6sSGzhiE2Xa/iPoTilVAH0sML5EK6NMLgipGThJ+z6rQKpGj5e2HI2Y2/6xjyrHr+ViXh58I4"}'
    }

conn.request("GET", "/jaxrs-json/products/AK/1180246?prdId=2400&detailed=true&gal=14&fc=true&gad=2023-06-27&next=false&displayGISMap=true", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

或者如果你想使用aiohttp


-1
投票

以下是您的 API 的一些潜在问题:

  • CORS配置;
  • 网关配置;

也许该请求正在构建无效的查询。我建议用 URL 中的隐式查询字符串替换参数。

由于请求库错误地解析数据类型,我遇到了类似的错误。

© www.soinside.com 2019 - 2024. All rights reserved.