from selenium import webdriver
import sqlite3
import os
from win32.win32crypt import CryptUnprotectData
def getcookiefromchrome(host='portal.chinanetcenter.com'):
cookiepath = os.environ['LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies"
sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host
with sqlite3.connect(cookiepath) as conn:
cu = conn.cursor()
cookies = {name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in
cu.execute(sql).fetchall()}
return cookies
sicookies=getcookiefromchrome("si.chinanetcenter.com")
cacookies=getcookiefromchrome(".chinanetcenter.com")
chrome_options = webdriver.ChromeOptions()
path=r"user-data-dir={}".format(os.environ['LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default")
chrome_options.add_argument(path)
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)
cookie_dict1 = {
"domain": ".chinanetcenter.com", # 火狐浏览器不用填写,谷歌要需要
'name': "versionSwitch",
'value': cacookies["versionSwitch"],
'path': '/',
'httpOnly': False,
'Secure': False}
cookie_dict2 = {
"domain": ".chinanetcenter.com", # 火狐浏览器不用填写,谷歌要需要
'name': "customerType",
'value': cacookies["customerType"],
'path': '/',
'httpOnly': False,
'Secure': False}
cookie_dict3 = {
"domain": ".chinanetcenter.com", # 火狐浏览器不用填写,谷歌要需要
'name': "timeZoneSetting",
'value': cacookies["timeZoneSetting"],
'path': '/',
'httpOnly': False,
'Secure': False}
cookie_dict4 = {
"domain": ".chinanetcenter.com", # 火狐浏览器不用填写,谷歌要需要
'name': "__lcl",
'value': cacookies["__lcl"],
'path': '/',
'httpOnly': False,
'Secure': False}
cookie_dict5 = {
"domain": "si.chinanetcenter.com", # 火狐浏览器不用填写,谷歌要需要
'name': "route",
'value': sicookies["route"],
'path': '/',
'httpOnly': False,
'Secure': False}
cookie_dict6 = {
"domain": "si.chinanetcenter.com", # 火狐浏览器不用填写,谷歌要需要
'name': "SESSION",
'value': sicookies["SESSION"],
'path': '/',
'httpOnly': True,
'Secure': False}
driver.get('https://portal.chinanetcenter.com/cas/login?service=https%3A%2F%2Fsi.chinanetcenter.com%2Fr_sec_login&appcode=serviceinsight')
driver.add_cookie(cookie_dict1)
driver.add_cookie(cookie_dict2)
driver.add_cookie(cookie_dict3)
driver.add_cookie(cookie_dict4)
driver.add_cookie(cookie_dict5)
driver.add_cookie(cookie_dict6)
driver.get("https://si.chinanetcenter.com/v2/index/#/")
print(driver.page_source)