-
Notifications
You must be signed in to change notification settings - Fork 348
/
add_cookie.py
52 lines (48 loc) · 1.85 KB
/
add_cookie.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
import re
from utils.config import get_config
from utils.selenium_browser import get_browser
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import WebDriverException
if __name__ == '__main__':
"""
用于获取手机端cookie
"""
browser = get_browser(get_config())
browser.get("https://plogin.m.jd.com/login/login")
try:
wait = WebDriverWait(browser, 135)
print("请在网页端通过手机号码登录")
wait.until(EC.presence_of_element_located((By.ID, 'msShortcutMenu')))
browser.get("https://home.m.jd.com/myJd/newhome.action")
username = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'my_header_name'))).text
pt_key, pt_pin, cookie = "", "", ""
for _ in browser.get_cookies():
if _["name"] == "pt_key":
pt_key = _["value"]
if _["name"] == "pt_pin":
pt_pin = _["value"]
if pt_key and pt_pin:
break
cookie = "pt_key=" + pt_key + ";pt_pin=" + pt_pin + ";"
print("获取的cookie是:" + cookie)
new_lines = []
rf = open("config.yaml", 'r', encoding='utf-8')
line = rf.readline()
while line:
if "cookie:" in line:
lineReg = re.compile(r'cookie: \"(.*?)\"')
line = lineReg.sub('cookie: \"%s\"' % cookie, line)
new_lines.append(line)
line = rf.readline()
rf.close()
wf = open("config.yaml", 'w', encoding='utf-8')
for line in new_lines:
wf.write(line)
wf.close()
print("成功添加", username)
except WebDriverException:
print("添加失败")
finally:
browser.close()