夏日幽灵 サマーゴースト (2021)
126 字
1 分钟
python-selenium
获取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待更新