帮你转换下代码高亮
[Python] 纯文本查看 复制代码 #获取cookie
def cookie_get_Chrome(url):
# 进入浏览器设置
global wd
wd = webdriver.Chrome(chrome_options=chrome_options)
wd.get(url)
time.sleep(0.5)
acc_pass_keys()
while(True):
time.sleep(3)
if tip_sleep() == True:
break
print('获取Cookie中,请等待……')
time.sleep(0.5) # 程序走到这里时暂停,登陆网页后得到cookie
print('cookie:-->start'.center(130, '*'))
dictCookies = wd.get_cookies()
jsonCookies = json.dumps(dictCookies)
print(jsonCookies)
with open('cookie.txt', 'w') as f:
f.write(jsonCookies)
print('cookie:-->End'.center(130, '*'))
time.sleep(1)
wd.quit()
# 添加cookie
def cookie_add_Chrome(url):
global wd,chrome_options
# 设置中文
wd.quit()
op = webdriver.ChromeOptions()
# op.add_argument('lang=zh_CN.UTF-8')
op.add_experimental_option('prefs', prefs)# 禁用图片
op.add_argument('--headless')#后台运行
wd = webdriver.Chrome(chrome_options=op)
wd.get(url)
wd.delete_all_cookies() #清理cookies缓存
with open('cookie.txt', 'r', encoding='utf8') as f:
listCookies = json.loads(f.read())
print('%%%%%%%%%%%%%%%%%', listCookies)
for cookie in listCookies:
if 'expiry' in cookie:
del cookie['expiry']
wd.add_cookie(cookie)
wd.get(url)
# 读取完cookie刷新页面
wd.refresh()
wd.get(url) |