1 import os
2 import sqlite3
3 import requests
4 from win32.win32crypt import CryptUnprotectData
5
6 def getchromecookie(host):
7 cookiepath = os.environ['LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies"
8 with sqlite3.connect(cookiepath) as conn:
9 sqlite = "select host_key,name,encrypted_value from cookies where host_key='%s'"%host
10 cu = conn.cursor()
11 '''自己写的'''
12 cursor = cu.execute(sqlite)
13 # for row in cursor:
14 # cookies = {row[1]:CryptUnprotectData(row[2])}
15 # print(row)
16 # print(cookies)
17 '''网上找的'''
18 '''Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。'''
19 '''Python decode() 方法以 encoding 指定的编码格式解码字符串。默认编码为字符串编码。'''
20 # cookies = {name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in
21 # cu.execute(sqlite).fetchall()}
22 # print(cookies)
23 '''再改一下'''
24 for host_key,name,encrypted_value in cursor:
25 cookies = {name:CryptUnprotectData(encrypted_value)[1].decode()}
26 return cookies
27 host = 'sp.kaixintuba.com'
28 # print(getchromecookie(host))
29 url = 'http://sp.kaixintuba.com/manager/vm_offline.aspx?_r=636014316'
30 r = requests.get(url,cookies = getchromecookie(host))
31 print(r.text)