python-3.x 发出带参数的get请求后,无法生成200状态

nnvyjq4y  于 2023-07-01  发布在  Python
关注(0)|答案(1)|浏览(126)

我正在尝试向网页发送GET HTTP请求,并收到200状态响应。在发出请求时,需要通过链路发送适当的参数。但是,当我使用linkpayload发送请求时,我总是收到410状态码。
当我在下面的代码中直接使用从开发工具复制的URL时,我得到一个200状态。
请注意,我已经修改了AnchorProdSetIdProdSetId的值,然后才用链接发送有效负载。
在使用带有有效负载的链接发出请求后,如何获得200(检测到时为403)状态?

import json
import requests

complete_url_from_d_tools = 'https://www.mcmaster.com/mv1687879695/WebParts/Content/ContentWebPart.aspx?cntnrIDtxt=ProdPageContent&srchidtxt=5875341805879&cntnrwdth=769&srchrslttxt=screws&GrpUsrInps=[{%22AnchorProdSetId%22:%22-2674186034712253%22,%22AnchorStateIsSet%22:true,%22ClearAllProdSetId%22:-1,%22ClearAllStateIsSet%22:true,%22GrpEID%22:%22%22,%22ProdSetId%22:%22-2674186034712253%22}]&viewporthgt=192&cssAlias=undefined&usedynamicimageswapping=true&conditionalclientlandingpage=true&forcemdmfiltering=true&workstationdata=true&fwdSsToNav=true&fwdSsWpNav=true&fwdCrcWpNav=true&sortRowsOnServer=true&showbroadlandingpagecopy=true&filtSsAttrEarly=true&legImgContainsHash=true&envrmgrcharsetind=3&useEs6=true'

link = 'https://www.mcmaster.com/mv1687538569/WebParts/Content/ContentWebPart.aspx'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.9,bn;q=0.8',
    'Referer': 'https://www.mcmaster.com/products/screws/',
    'X-Requested-With': 'XMLHttpRequest',
}
payload = {
    'cntnrIDtxt': 'ProdPageContent',
    'srchidtxt': '5875341805879',
    'cntnrwdth': '769',
    'srchrslttxt': 'screws',
    'GrpUsrInps': '[{"AnchorProdSetId":"","AnchorStateIsSet":"true","ClearAllProdSetId":-1,"ClearAllStateIsSet":"true","GrpEID":"","ProdSetId":""}]',
    'viewporthgt': '192',
    'cssAlias': 'undefined',
    'usedynamicimageswapping': 'true',
    'conditionalclientlandingpage': 'true',
    'forcemdmfiltering': 'true',
    'workstationdata': 'true',
    'fwdSsToNav': 'true',
    'fwdSsWpNav': 'true',
    'fwdCrcWpNav': 'true',
    'sortRowsOnServer': 'true',
    'showbroadlandingpagecopy': 'true',
    'filtSsAttrEarly': 'true',
    'legImgContainsHash': 'true',
    'envrmgrcharsetind': '3',
    'useEs6': 'true',
}

payload['GrpUsrInps'] = json.loads(payload['GrpUsrInps'])
payload['GrpUsrInps'][0]['AnchorProdSetId'] = '-2674186034712253'
payload['GrpUsrInps'][0]['ProdSetId'] = '-2674186034712253'
payload['GrpUsrInps'] = json.dumps(payload['GrpUsrInps'])

with requests.Session() as s:
    s.headers.update(headers)
    # res = s.get(complete_url_from_d_tools)
    res = s.get(link,params=payload)
    print(res.status_code)
dxxyhpgq

dxxyhpgq1#

似乎这个服务器返回brotli压缩响应。要从服务器获取200响应,请安装brotli包:

pip install brotli

然后:

import requests

url = "https://www.mcmaster.com/mv1687879695/WebParts/Content/ContentWebPart.aspx?cntnrIDtxt=ProdPageContent&srchidtxt=5875341805879&cntnrwdth=1001&srchrslttxt=screws&GrpUsrInps=%5B%7B%22AnchorProdSetId%22%3A%22-3383254440195390%22%2C%22AnchorStateIsSet%22%3Atrue%2C%22ClearAllProdSetId%22%3A-1%2C%22ClearAllStateIsSet%22%3Atrue%2C%22GrpEID%22%3A%22%22%2C%22ProdSetId%22%3A%22-3383254440195390%22%7D%5D&viewporthgt=296&cssAlias=undefined&usedynamicimageswapping=true&conditionalclientlandingpage=true&forcemdmfiltering=true&workstationdata=true&fwdSsToNav=true&fwdSsWpNav=true&fwdCrcWpNav=true&sortRowsOnServer=true&showbroadlandingpagecopy=true&filtSsAttrEarly=true&legImgContainsHash=true&clientNavigationEvents=%5B%5D&envrmgrcharsetind=5&useEs6=true"
r = requests.get(url)
print(r.status_code)

图纸:

200

编辑:要获取“内六角螺钉”的内容:

import requests

AnchorProdSetId = '-3383254440195390'
ProdSetId = '-3383254440195390'

params = {
    "cntnrIDtxt": "ProdPageContent",
    "srchidtxt": "5875341805879",
    "cntnrwdth": "1001",
    "srchrslttxt": "screws",
    "GrpUsrInps": '[{"AnchorProdSetId":"' + AnchorProdSetId + '","AnchorStateIsSet":true,"ClearAllProdSetId":-1,"ClearAllStateIsSet":true,"GrpEID":"","ProdSetId":"' + ProdSetId + '"}]',
    "viewporthgt": "296",
    "cssAlias": "undefined",
    "usedynamicimageswapping": "true",
    "conditionalclientlandingpage": "true",
    "forcemdmfiltering": "true",
    "workstationdata": "true",
    "fwdSsToNav": "true",
    "fwdSsWpNav": "true",
    "fwdCrcWpNav": "true",
    "sortRowsOnServer": "true",
    "showbroadlandingpagecopy": "true",
    "filtSsAttrEarly": "true",
    "legImgContainsHash": "true",
    "clientNavigationEvents": "[]",
    "envrmgrcharsetind": "5",
    "useEs6": "true",
}

url = 'https://www.mcmaster.com/mv1687879695/WebParts/Content/ContentWebPart.aspx'
r = requests.get(url, params=params)

print(r.text)

相关问题