main.py
3.79 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
from utils.base_social_media import set_init_script
from utils.files_times import get_absolute_path
from utils.log import kuaishou_logger
from playwright.async_api import Playwright, async_playwright
async def toutiao_setup(account_file, handle=False, auto=False):
print(account_file)
account_file = get_absolute_path(account_file, "wyh_uploader")
if not os.path.exists(account_file):
if not handle:
return False
kuaishou_logger.info('[+] cookie文件不存在或已失效,即将自动打开浏览器,请扫码登录,登陆后会自动生成cookie文件')
if not auto:
await get_toutiao_cookie(account_file)
else:
await get_toutiao_cookie_auto(account_file)
else:
await open_toutiao_main_page(account_file)
return True
async def open_toutiao_main_page(account_file):
async with async_playwright() as playwright:
options = {
'args': [
'--lang en-GB'
],
'headless': False, # Set headless option here
}
# Make sure to run headed.
browser = await playwright.chromium.launch(**options)
# Setup context however you like.
context = await browser.new_context(storage_state=account_file) # Pass any options
context = await set_init_script(context)
# Pause the page, and start recording manually.
page = await context.new_page()
await page.goto("https://mp.toutiao.com")
await page.pause()
browser.close()
async def get_toutiao_cookie_auto(account_file):
print("get_toutiao_cookie")
async with async_playwright() as playwright:
options = {
'args': [
'--lang en-GB'
],
'headless': False, # Set headless option here
}
# Make sure to run headed.
browser = await playwright.chromium.launch(**options)
# Setup context however you like.
context = await browser.new_context() # Pass any options
context = await set_init_script(context)
# Pause the page, and start recording manually.
page = await context.new_page()
await page.goto("https://mp.toutiao.com")
# 自动登陆
await page.wait_for_timeout(2000)
await page.get_by_text('密码登录').click()
await page.wait_for_timeout(2000)
await page.get_by_placeholder("手机号/邮箱").fill('18610534668')
print("输入账号成功")
await page.wait_for_timeout(1000)
await page.get_by_placeholder('密码').fill("Liuyihong1023@")
await page.wait_for_timeout(2000)
await page.locator("//*[@class='web-login-confirm-info__checkbox']").click()
await page.wait_for_timeout(2000)
await page.locator("//*[@class='web-login-button']").click()
await page.wait_for_timeout(2000)
# 点击调试器的继续,保存cookie
await context.storage_state(path=account_file)
async def get_toutiao_cookie(account_file):
print("get_toutiao_cookie")
async with async_playwright() as playwright:
options = {
'args': [
'--lang en-GB'
],
'headless': False, # Set headless option here
}
# Make sure to run headed.
browser = await playwright.chromium.launch(**options)
# Setup context however you like.
context = await browser.new_context() # Pass any options
context = await set_init_script(context)
# Pause the page, and start recording manually.
page = await context.new_page()
await page.goto("https://mp.toutiao.com")
# 手动授权登录
await page.pause()
# 点击调试器的继续,保存cookie
await context.storage_state(path=account_file)