-
Notifications
You must be signed in to change notification settings - Fork 2
/
qzonerenewcookies-selenium.py
62 lines (54 loc) · 1.75 KB
/
qzonerenewcookies-selenium.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import time
import sys
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
def connect_to_chrome():
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--disable-gpu')
browser = webdriver.Chrome(options=chrome_options)
return browser
# Log in to QQ Zone
def login(browser, my_qq):
browser.get('https://i.qq.com/')
browser.switch_to.frame("login_frame")
time.sleep(2)
try:
find = browser.find_element(By.ID, f'img_out_{my_qq}')
find.click()
time.sleep(3)
except Exception as error:
print(f'Log Error! {error}')
else:
print("Successfully Logged!")
browser.switch_to.default_content()
try:
cookies = browser.get_cookies()
cookies_dict = {cookie['name']: cookie['value'] for cookie in cookies}
with open(f"./cookies-{self_id}.json", "w") as f:
json.dump(cookies_dict, f, indent=4)
except Exception as error:
print(f'Cookies Save Error! {error}')
else:
print("Successfully Saved Cookies!")
browser.switch_to.default_content()
def read_config(file_path):
config = {}
with open(file_path, 'r') as f:
for line in f:
key, value = line.strip().split('=')
config[key.strip()] = value.strip().strip('"')
return config
def main():
# Read configuration and connect to Chrome
config = read_config('oqqwall.config')
my_qq = sys.argv[1]
# Connect to the Chrome instance running in the daemon
browser = connect_to_chrome()
# Log in and save cookies
login(browser, my_qq)
browser.quit()
if __name__ == '__main__':
main()