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

如何用Python自动获取网站Access Token以实现Wizink网页爬取

问题:自动获取Wizink网站API的Bearer Token方法

我尝试爬取Wizink的个人贷款页面,但无法自动获取Access Token,每次爬取都得手动更新请求头里的Bearer token,想知道有没有办法自动实现这个操作。

我的手动实现代码

import requests

url_WiZink = 'https://www.creditopessoal.wizink.pt/gravitee/gateway/api-chn-loans/v1/loans/quotation'
    
headers_WiZink = {
    'Accept': 'application/json',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'pt-PT',
    'Authorization': 'Bearer de6ea490-381e-417f-ab77-3aad0d7eb63c', 
    'Connection': 'keep-alive',
    'Content-Type': 'application/json;charset=UTF-8',
    'Host': 'www.creditopessoal.wizink.pt',
    'Origin': 'https://www.wizink.pt',
    'Referer': 'https://www.wizink.pt/',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
    'sec-ch-ua-mobile': '?0',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
    'X-Channel-Id': 'LOANSIMULATOR',
    'X-Client-Id': 'simWzkPt',
    'X-Country-Id': 'PRT',
    'X-Device-UUID': 'd14e9b629804cbba1ac7c3e78ab39a56'
}
        

payload_WiZink = {
    "productCode":"WZP01",
    "fixedTermLoanId":84,
    "impositionAmount":{"amount":10000,"currency":"EUR"},
    "settlementDay":"5",
    "dueOrAdvanceInterestIndicator":"3",
    "nominalInterest":"8.0000000",
    "feeRateId":"05",
    "settlementFrequencyId":"0001",
    "deprecationFrequencyId":"0001"
}

response_WiZink = requests.post(url_WiZink, headers=headers_WiZink, json=payload_WiZink, verify=False).json()

自动获取Bearer Token的实现方案

1. 先抓包分析token来源

打开浏览器F12开发者工具,刷新目标页面,在Network面板里筛选包含token/auth/login关键词的请求,找到网站获取Bearer Token的接口,记录下该接口的:

  • 请求URL
  • 请求方法(POST/GET等)
  • 必要请求头(比如X-Client-Id、User-Agent)
  • 请求参数(比如client_id、grant_type)

2. 模拟请求自动获取token

用requests库模拟这个token请求,拿到返回的token后替换到后续请求的Authorization头中,示例代码如下(需根据实际抓包结果修改token接口信息):

import requests

def get_wizink_token():
    # 替换为实际的token获取接口URL
    token_url = "https://www.creditopessoal.wizink.pt/xxx/auth/token"
    token_headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
        "X-Client-Id": "simWzkPt",
        # 其他抓包得到的必要头信息
    }
    # 替换为接口实际需要的参数
    token_payload = {
        "client_id": "simWzkPt",
        "grant_type": "client_credentials"
    }
    response = requests.post(token_url, headers=token_headers, data=token_payload, verify=False)
    token_data = response.json()
    # 假设返回的token字段是access_token,根据实际返回结构调整
    return token_data.get("access_token")

# 自动获取token
access_token = get_wizink_token()

# 后续请求头替换为自动获取的token
headers_WiZink['Authorization'] = f'Bearer {access_token}'

# 执行原本的请求
response_WiZink = requests.post(url_WiZink, headers=headers_WiZink, json=payload_WiZink, verify=False).json()

3. 额外注意事项

  • 如果网站需要维持会话才能获取token,改用requests.Session()来保存Cookie,避免重复处理会话信息。
  • 若token有过期时间,可在每次请求前检查token是否失效,失效则重新调用获取token的函数。
  • 爬取行为需遵守网站的使用条款,避免触发反爬机制。

内容的提问来源于stack exchange,提问作者brunosm

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 02:01:28