码迷,mamicode.com
首页 > 其他好文 > 详细

如何在采集程序中共享火狐的Cookie?

时间:2015-10-21 12:44:48      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

原文作者:西安鲲之鹏

原文链接:http://www.site-digger.com/html/articles/20120531/36.html

 

对于需要登录后才能进行的采集,采用共享火狐浏览器Cookie的方案好处是:不用自己在再写登录过程,直接在火狐中进行登录即可。

火狐的Cookie存储在哪儿?

  • 临时性Cookie,即关闭浏览器后就会过期的Cookie存储在sessionstore.js中,格式为JSON,结构如下:
  • 技术分享
  • 永久性Cookie存放在cookies.sqlite中,格式为SQLite数据库,结构如下:
  • 技术分享

 这两个文件的路径在不同的操作系统(不同用户)下是不一样的,比如Win7上的路径是:C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\d4371ep6.default。

如何使用这些Cookie数据?

读取cookies.sqlite和sessionstore.js将其转换为统一的、我们的采集程序可用的Cookie格式。

我们的采集程序采用Python编写,Python的cookielib.MozillaCookieJar()可识别的Cookie格式为:

  1. # Netscape HTTP Cookie File  
  2. # http://www.netscape.com/newsref/std/cookie_spec.html  
  3. # This file was generated by libcurl! Edit at your own risk.  
  4.    
  5. www.example.com  FALSE /  FALSE 1338534278  cookiename value  
  6.   
  7. 前面几行为注释行(以#开头),后面为Cookie数据,每行为一项,元素之间采用制表符\t分隔。  
  8. 各部分的含义如下:  
  9.   
  10. domain - 可被使用的域;  
  11. flag - 是否允许所有子域使用该项,可根据domain是否已点开头来判断;  
  12. path - 可被使用的路径;  
  13. secure - 是否需要安全连接(HTTPS);  
  14. expiration - 过期时间,Unix时间戳。  
  15. name - Cookie项名;  
  16. value - Cookie项对应的值;  

我们的实现方案如下:

  1. import os  
  2. import cookielib  
  3. import urllib2  
  4. from pysqlite2 import dbapi2 as sqlite  
  5.   
  6. def firefox_cookie(ffc_path=r‘C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\d4371ep6.default‘):  
  7.          
  8.         con = sqlite.connect(os.path.join(ffc_path, ‘cookies.sqlite‘))  
  9.         cur = con.cursor()  
  10.         cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies")  
  11.         ftstr = [‘FALSE‘‘TRUE‘]  
  12.         s = StringIO()  
  13.         s.write(‘# Netscape HTTP Cookie File\n‘)  
  14.         s.write(‘# http://www.netscape.com/newsref/std/cookie_spec.html\n‘)  
  15.         s.write(‘# This is a generated file!  Do not edit\n‘)  
  16.         for item in cur.fetchall():  
  17.             s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (item[0], ftstr[item[0].startswith(‘.‘)], item[1], ftstr[item[2]], item[3], item[4], item[5]))  
  18.         js_path = os.path.join(ffc_path, ‘sessionstore.js‘)  
  19.         if os.path.exists(js_path):  
  20.             try:  
  21.                 js_data = json.loads(open(js_path, ‘rb‘).read())  
  22.             except Exception, e:  
  23.                 print str(e)  
  24.             else:  
  25.                 if ‘windows‘ in js_data:  
  26.                     for window in js_data[‘windows‘]:  
  27.                         for cookie in window[‘cookies‘]:  
  28.                             s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (cookie.get(‘host‘‘‘), ftstr[cookie.get(‘host‘‘‘).startswith(‘.‘)], \  
  29.                                      cookie.get(‘path‘‘‘), False, str(int(time.time()) + 3600*24*7), cookie.get(‘name‘‘‘), cookie.get(‘value‘‘‘)))  
  30.       
  31.           
  32.         s.seek(0)  
  33.         cookie_jar = cookielib.MozillaCookieJar()  
  34.         cookie_jar._really_load(s, ‘‘TrueTrue)  
  35.         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))  
  36.         return self.opener  
特别说明:该文章为西安鲲之鹏的原创文章 ,你除了可以发表评论外,还可以转载到你的网站或博客,但是请保留源地址,谢谢!!(尊重他人劳动,你我共同努力)

如何在采集程序中共享火狐的Cookie?

标签:

原文地址:http://my.oschina.net/webscraping/blog/519795

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!