-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webrowser_config.py
48 lines (36 loc) · 1.53 KB
/
webrowser_config.py
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
from __future__ import annotations
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from utils.config import load_config
class ChromeDriverConfig:
def __init__(self):
self.args: dict[str, str] = load_config("./configs/chrome.yaml")
self.option = webdriver.ChromeOptions()
self.option.add_argument(r"user-data-dir=./User Data") # 浏览器路径
# 指定Chrome和ChromeDriver的路径
self.chrome_path = self.args["chrome"]
self.chrome_driver_path = self.args["chrome_driver"]
self.option.binary_location = self.chrome_path
# 使用Service指定ChromeDriver的路径
self.service = Service(self.chrome_driver_path)
if __name__ == "__main__":
chrome_config = ChromeDriverConfig()
# 初始化driver
driver = webdriver.Chrome(service=chrome_config.service, options=chrome_config.option)
driver.get(chrome_config.args["target_url"])
sleep(3)
# 获取 cookies
cookies = driver.get_cookies()
# 查找 SESSDATA cookie
sessdata = ""
for cookie in cookies:
if cookie["name"] == "SESSDATA":
sessdata = cookie["value"]
print(cookie["name"] + ": ", sessdata)
if sessdata:
print("复制你的SESSDATA,然后填写到./configs/args.yaml的SESSDATA中,写在双引号中")
else:
print("第一次打开浏览器,需要先登陆bilibili账号,然后再次运行程序")
print("可以关闭该窗口了....")
sleep(100000)