调用GoMechanic站点API抓取服务价格提示需登录的解决方案求助
问题修复说明
你代码的核心错误是发起了两次重复请求:第一次已经携带了有效认证头发起请求,但你没有使用这次的返回结果,反而第二次不带任何认证头发起了无权限请求,使用了这次的报错响应做解析,自然触发登录提示。
修改后可正常运行的代码
import requests import json # 目标接口地址 url = "https://gomechanic.app/api/v2/oauth/customer/get-services-details-by-category?car_id=135&city_id=1&category_id=0&user_car_id=null" # 请求头配置 header = { "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8", # 每次运行前从浏览器开发者工具的请求头中复制最新的Authorization值替换此处 "Authorization": "Bearer 替换为你每次获取的最新密钥", "Connection": "keep-alive", "Content-Type": "application/json", "Host": "gomechanic.app", "Origin": "https://gomechanic.in", "Referer": "https://gomechanic.in/", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "Windows", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "cross-site", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36" } # 仅发起一次带认证头的请求即可 response = requests.get(url, headers=header) # 解析返回的JSON数据 data = response.json() # 可打印查看全量返回数据 print(data)
运行注意事项
- 每次运行代码前,先从浏览器开发者工具的
get-service-details-by-category请求头中,复制最新的Authorization值替换代码中对应位置的字符串 - 其余请求头参数保持和浏览器配置一致即可,无需额外修改,可避免触发站点反爬拦截
- 直接使用
response.json()方法即可解析返回的JSON结构,不需要手动调用json.loads处理响应文本
内容的提问来源于stack exchange,提问作者mukul dhiman
相关产品推荐
相关产品推荐

