Playwright模拟登陆

由于时效问题,该文某些代码、技术可能已经过期,请注意!!!本文最后更新于:14 天前

playwright

模拟登陆获取token等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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("your_email@example.com", "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)

response返回的access token可能没有权限访问,需要使用request 获取权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
async def handle_request(request, container):
if 'x-api-key' in request.headers:
container['x_api_key'] = request.headers['x-api-key']

if 'authorization' in request.headers:
container['authorization'] = request.headers['authorization']

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

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

# 注册响应和请求事件监听器
# page.on("response", lambda response: handle_response(response, tokens_container))
page.on("request", lambda request: handle_request(request, 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 container.get('authorization'), container.get('x_api_key')

def fetch_requirements(headers):
url = 'https://api24.commonapp.org/datacatalog/members/requirements'
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()


async def main():
email = 'xxx'
password = 'xxx'

authorization, x_api_key = await login_and_get_tokens(email, password)
print(authorization)
print(x_api_key)
if not any([authorization, x_api_key]):
print("Failed to retrieve authorization.")
return

headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Origin': 'https://apply.commonapp.org',
'Referer': 'https://apply.commonapp.org/',
'content-type': 'application/json',
'x-api-key': x_api_key,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Authorization': authorization
}

requirements_data = fetch_requirements(headers)

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!