This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgetcookie.py
59 lines (55 loc) · 2.43 KB
/
getcookie.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
from selenium import webdriver
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
def getWeb():
'''配置web浏览器'''
options = webdriver.ChromeOptions()
mobile_emulation = {"deviceName": "Galaxy S5"}
capabilities = DesiredCapabilities.CHROME
capabilities['loggingPrefs'] = {'browser': 'ALL'}
# options = webdriver.ChromeOptions()
options.add_experimental_option("mobileEmulation", mobile_emulation)
options.add_experimental_option("excludeSwitches", ['enable-automation'])
# 修改windows.navigator.webdriver,防机器人识别机制,selenium自动登陆判别机制desired_capabilities=capabilities,
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 更换头部
options.add_argument(
'user-agent=mozilla/5.0 (iphone; cpu iphone os 5_1_1 like mac os x) applewebkit/534.46 (khtml, like gecko) mobile/9b206 micromessenger/5.0')
# 禁用图片
# 禁止图片和css加载
# "profile.managed_default_content_settings.images": 2,
prefs = {'permissions.default.stylesheet': 2}
options.add_experimental_option("prefs", prefs)
# #添加代理
# ip,port = '127.0.0.1','8080'
# options.add_argument(('--proxy-server=http://{}:{}'.format(ip,port)))#有的博客写的是'--proxy-server=http://',就目前我的电脑来看的话需要把http://去掉就可以用,他会自己加的
# options.add_argument('-headless') # 无头参数
# options.headless = True
web = webdriver.Chrome(options=options)
url = "https://work.jluzh.edu.cn/default/work/jlzh/jkxxtb/jkxxcj.jsp"
web.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
web.set_page_load_timeout(30)
web.set_script_timeout(30) # 这两种设置都进行才有效
web.get(url)
# web.add_cookie({'name': 'JSESSIONID','path': '/','value': cookies})
return web
def get_cookie(username, password):
web = getWeb()
time.sleep(2)
user = web.find_element_by_id("username")
user.send_keys("{}".format(username))
passwd = web.find_element_by_id("password")
passwd.send_keys("{}".format(password))
btn = web.find_element_by_id("passbutton")
btn.click()
time.sleep(2)
print(web.get_cookies())
cookie = web.get_cookies()[0]['value']
web.quit()
return cookie