在Scrapy中使用Chrome中的cookie

 

from cookielib import CookieJar as _CookieJar, DefaultCookiePolicy

from scrapy.utils.httpobj import urlparse_cached

def sqlite2cookie():#filename
   from cStringIO import StringIO
   #from pysqlite2 import dbapi2 as sqlite
   import sqlite3
   import cookielib
 
 ## but we can make sqlite3 always return bytestrings ...
 # Cookies file come from C:\Users\JiangHonglei\AppData\Local\Google\Chrome\User Data\Default\Cookies
   con = sqlite3.connect(r'Cookies') 
   con.text_factory = str
   
   cur = con.cursor()
   #cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies")
   cur.execute("select host_key, path, secure, expires_utc, name, value from cookies")
   
   ftstr = ["FALSE","TRUE"]
 
   s = StringIO()
   s.write("""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
""")
   for item in cur.fetchall():
      try:
         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]))
      except UnicodeError:
            continue
   s.seek(0)
 
   cookie_jar = cookielib.MozillaCookieJar()
   cookie_jar._really_load(s, '', True, True)
   return cookie_jar

你可能感兴趣的:(scrapy,chrome,cookie,sqlite3,CookieJar)