使用Playwright模拟登陆

import asyncio
from playwright.async_api import async_playwright
​
## jupyter中需要导入下面的包
import nest_asyncio
nest_asyncio.apply()
​

async def handle_response(response, tokens_container):
    if response.url == 'https://cognito-idp.us-west-2.amazonaws.com/':  
        json_data = await response.json()
        if 'AuthenticationResult' in json_data:
            tokens_container['tokens'] = {
                'access_token': json_data['AuthenticationResult']['AccessToken'],
                'refresh_token': json_data['AuthenticationResult']['RefreshToken']
            }

async def handle_request(request, x_api_key_container):
    if 'x-api-key' in request.headers:
        x_api_key_container['x_api_key'] = request.headers['x-api-key']

async def login_and_get_tokens(email, password):
    tokens_container = {}
    x_api_key_container = {}

    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()

        # 注册响应和请求事件监听器
        page.on("response", lambda response: handle_response(response, tokens_container))
        page.on("request", lambda request: handle_request(request, x_api_key_container))
        
        # 导航到指定URL并登录
        await page.goto("https://apply.commonapp.org/login")
        await page.fill('input[name="email"]', email)
        await page.fill('input[name="password"]', password)
        await page.click('button[type="submit"]')
        await page.wait_for_load_state("networkidle")

        await browser.close()
        return tokens_container.get('tokens'), x_api_key_container.get('x_api_key')

# 示例调用
tokens, x_api_key = asyncio.run(login_and_get_tokens("[email protected]", "your_password"))

# 检查是否未成功获取 tokens 或 x-api-key
if not any([tokens, x_api_key]):
    print("Failed to retrieve tokens or x-api-key.")
else:
    print("Tokens:", tokens)
    print("x-api-key:", x_api_key)

handle_response 主要用来抓包登陆过程中的请求和响应,可根据需求自行设计

猜你喜欢

转载自blog.csdn.net/weixin_42666760/article/details/141356401