-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_cookies.py
104 lines (81 loc) · 2.11 KB
/
get_cookies.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
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
104
# -*- coding: utf-8 -*-
from time import sleep
from selenium import webdriver
import json
options = webdriver.ChromeOptions()
options.add_argument(
'--user-data-dir=C:/Users/Username/AppData/Local/Google/Chrome/User Data')
driver = webdriver.Chrome(
executable_path='C:\\chromedriver.exe', # 指向你自己下载的chromedriver的目录
options=options)
def load_pt_sites():
pt_sites = {}
try:
with open('pt_sites.json', 'r') as pt_file:
pt_sites = json.load(pt_file)
except BaseException:
print("Json load failed")
finally:
return pt_sites
# initial
hudbt = {
'abbr': 'hudbt',
'domain': 'https://hudbt.hust.edu.cn',
'index': 'https://hudbt.hust.edu.cn/index.php',
'cookie': []
}
npupt = {
'abbr': 'npupt',
'domain': 'https://npupt.com',
'index': 'https://npupt.com/index.php',
'cookie': []
}
stju = {
'abbr': 'stju',
'domain': 'https://pt.sjtu.edu.cn',
'index': 'https://pt.sjtu.edu.cn/index.php',
'cookie': []
}
tjupt = {
'abbr': 'tjupt',
'domain': 'https://www.tjupt.org',
'index': 'https://www.tjupt.org/index.php',
'cookie': []
}
byr = {
'abbr': 'byr',
'domain': 'https://bt.byr.cn',
'index': 'https://bt.byr.cn/index.php',
'cookie': []
}
nwsuaf6 = {
'abbr': 'nwsuaf6',
'domain': 'https://pt.nwsuaf6.edu.cn',
'index': 'https://pt.nwsuaf6.edu.cn/index.php',
'cookie': []
}
pt_sites = {}
pt_sites['byr'] = byr
pt_sites['hudbt'] = hudbt
pt_sites['npupt'] = npupt
pt_sites['stju'] = stju
pt_sites['tjupt'] = tjupt
pt_sites['nwsuaf6'] = nwsuaf6
def get_cookie(url):
driver.get(url)
cookies_0 = driver.get_cookies()
cookies_1 = {}
for item in cookies_0:
cookies_1[item['name']] = item['value']
return cookies_1
def save_site_info(pt_sites):
cookies_file = 'pt_sites.json'
with open(cookies_file, 'w') as f:
json.dump(pt_sites, f)
if __name__ == "__main__":
for site in pt_sites:
url = pt_sites[site]['index']
pt_sites[site]['cookie'] = get_cookie(url)
sleep(5)
driver.quit()
save_site_info(pt_sites)