126 字
1 分钟
python-selenium
2025-03-20
无标签

获取token#

def get_token():

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    import json
    import time
    import urllib.parse

    caps = DesiredCapabilities.CHROME
    caps["goog:loggingPrefs"] = {"performance": "ALL"}

    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    driver = webdriver.Chrome(
        options=options
    )

    target_url = "https://vp.fact.qq.com/home"
    api_pattern = "/api/config/initial"

    driver.get(target_url)

    # 等待并捕获网络请求(最多等待10秒)
    start_time = time.time()
    timeout = 10
    token = None

    while time.time() - start_time < timeout:
        logs = driver.get_log("performance")
        for log in logs:
            message = json.loads(log["message"])["message"]
            if message.get("method") == "Network.requestWillBeSent":
                request = message.get("params", {}).get("request", {})
                url = request.get("url", "")
                if api_pattern in url:
                    parsed_url = urllib.parse.urlparse(url)
                    token = urllib.parse.parse_qs(parsed_url.query).get("token", [None])[0]
                    if token:
                        print("捕获到 Token:", token)
                        driver.quit()  # 退出浏览器
                    return token
NOTE

待更新

python-selenium
https://bangwu.top/posts/python-selenium/
作者
棒无
发布于
2025-03-20
许可协议
CC BY-NC-SA 4.0