main.py
3.08 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
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 wyh_setup(account_file, handle=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文件')
await get_wyh_cookie(account_file)
else:
await open_wyh_main_page(account_file)
return True
async def open_wyh_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://baijiahao.baidu.com/builder/theme/bjh/login")
board = page.locator("//*[@class='homeV4__board__card__data__value']")
# html_content = await board.inner_html()
# print(html_content)
content = await board.nth(0).inner_text() #总粉丝数
print(content)
content = await board.nth(1).inner_text() #总阅读数
print(content)
content = await board.nth(2).inner_text() #总收益
print(content)
await page.get_by_text("粉丝数据").click()
await page.wait_for_timeout(5000)
await page.get_by_text("内容数据").click()
await page.wait_for_timeout(5000)
await page.get_by_text("收益数据").click()
await page.wait_for_timeout(5000)
browser.close()
async def get_wyh_cookie(account_file):
print("get_wyh_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://baijiahao.baidu.com/builder/theme/bjh/login")
# await page.pause()
# 点击调试器的继续,保存cookie
# await context.storage_state(path=account_file)
# 自动登陆
await page.wait_for_timeout(20000)
await page.get_by_text('注册/登录百家号').click()
await page.pause()