昨天我写了两篇博客,介绍:
2) 如何解析Cookies字符串、创建CookieJar对象,自动管理Cookies的添加、更新、删除
本篇顺着介绍如何在Python中使用Chrome浏览器已有的Cookies发起HTTP请求。
Chrome的Cookies文件保存路径类似于:
C:\Users\Lucas Lee\AppData\Local\Google\Chrome\User Data\Default\Cookies
其中C:\Users\Lucas Lee\AppData可通过环境变量os.environ[‘LOCALAPPDATA’]获取。
Cookies是一个Sqlite3数据库文件。
了解完上述事实,问题就非常简单了:
从数据库中查询到所需的Cookies,更新到一个CookieJar对象中。再使用这个CookieJar创建opener即可。
函数build_opener_with_chrome_cookies展示了一个基本的实现,当省略domain时,会导入所有的Cookie,建议提供domain参数。
import os
import sqlite3
import cookielib
import Cookie
import urllib2
def build_opener_with_chrome_cookies(domain=None):
cookie_file_path = os.path.join(os.environ['LOCALAPPDATA'], r'Google\Chrome\User Data\Default\Cookies')
if not os.path.exists(cookie_file_path):
raise Exception('Cookies file not exist!')
conn = sqlite3.connect(cookie_file_path)
sql = 'select host_key, name, value, path from cookies'
if domain:
sql += ' where host_key like "%{}%"'.format(domain)
cookiejar = cookielib.CookieJar() # No cookies stored yet
for row in conn.execute(sql):
cookie_item = cookielib.Cookie(
version=0, name=row[1], value=row[2],
port=None, port_specified=None,
domain=row[0], domain_specified=None, domain_initial_dot=None,
path=row[3], path_specified=None,
secure=None,
expires=None,
discard=None,
comment=None,
comment_url=None,
rest=None,
rfc2109=False,
)
cookiejar.set_cookie(cookie_item) # Apply each cookie_item to cookiejar
conn.close()
return urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) # Return opener
if __name__ == '__main__':
opener = build_opener_with_chrome_cookies(domain='192.168.1.253')
html_doc = opener.open('http://192.168.1.253').read()
import re
print 'Title:', re.search('
(.*?)', html_doc, re.IGNORECASE).group(1)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
importos
importsqlite3
importcookielib
importCookie
importurllib2
defbuild_opener_with_chrome_cookies(domain=None):
cookie_file_path=os.path.join(os.environ['LOCALAPPDATA'],r'Google\Chrome\User Data\Default\Cookies')
ifnotos.path.exists(cookie_file_path):
raiseException('Cookies file not exist!')
conn=sqlite3.connect(cookie_file_path)
sql='select host_key, name, value, path from cookies'
ifdomain:
sql+=' where host_key like "%{}%"'.format(domain)
cookiejar=cookielib.CookieJar()# No cookies stored yet
forrowinconn.execute(sql):
cookie_item=cookielib.Cookie(
version=0,name=row[1],value=row[2],
port=None,port_specified=None,
domain=row[0],domain_specified=None,domain_initial_dot=None,
path=row[3],path_specified=None,
secure=None,
expires=None,
discard=None,
comment=None,
comment_url=None,
rest=None,
rfc2109=False,
)
cookiejar.set_cookie(cookie_item)# Apply each cookie_item to cookiejar
conn.close()
returnurllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))# Return opener
if__name__=='__main__':
opener=build_opener_with_chrome_cookies(domain='192.168.1.253')
html_doc=opener.open('http://192.168.1.253').read()
importre
print'Title:',re.search('
(.*?)',html_doc,re.IGNORECASE).group(1)测试代码中,我在Chrome浏览器登陆了无线路由器,opener打开的页面同样显示处于已登陆状态。