You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用自定义参数发送GET请求返回410,如何获取200状态码?

问题描述

我尝试向网页发送带参数的GET请求,期望获得200状态码响应,但使用代码中的link和payload发送请求时,始终收到410状态码;直接使用从开发者工具复制的完整URL请求时,能得到200状态码。已修改payload中GrpUsrInps内的AnchorProdSetId和ProdSetId的值,请问如何通过带参数的link发送请求以获取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)

解决方案

问题根源

  1. 动态路径过期:代码中link的mv1687538569是网站生成的临时会话/资源版本标识,该标识已过期,因此返回410(资源已不存在);而开发者工具复制的URL使用的是当前有效的mv1687879695标识。
  2. 参数编码错误:GrpUsrInps参数需要是URL编码后的JSON字符串,但代码中用json.dumps生成的字符串会被requests的params再次编码,导致最终参数格式与浏览器发送的不一致,服务器无法正确解析。

修改后的代码

import json
import requests
import urllib.parse

# 使用开发者工具中获取的有效动态路径URL
link = 'https://www.mcmaster.com/mv1687879695/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',
}

# 构造GrpUsrInps的原始JSON结构,再手动进行URL编码
grp_usr_inps = [
    {
        "AnchorProdSetId": "-2674186034712253",
        "AnchorStateIsSet": True,
        "ClearAllProdSetId": -1,
        "ClearAllStateIsSet": True,
        "GrpEID": "",
        "ProdSetId": "-2674186034712253"
    }
]
# 对JSON字符串做URL编码,避免requests重复编码
grp_usr_inps_encoded = urllib.parse.quote(json.dumps(grp_usr_inps))

payload = {
    'cntnrIDtxt': 'ProdPageContent',
    'srchidtxt': '5875341805879',
    'cntnrwdth': '769',
    'srchrslttxt': 'screws',
    'GrpUsrInps': grp_usr_inps_encoded,
    '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',
}

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

关键说明

  • 动态路径维护:mvxxxxxx这类标识是临时的,后续可能会失效。建议每次请求前先访问产品页面,从页面源码或网络请求中提取最新的路径片段。
  • 参数编码处理:使用urllib.parse.quote对JSON字符串进行URL编码,确保最终参数格式与浏览器发送的完全一致,避免服务器解析失败。

内容的提问来源于stack exchange,提问作者robots.txt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 10:37:52