欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在Scrapy中使用Chrome中的cookie

程序员文章站 2022-05-11 23:18:17
...

 

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

转载于:https://my.oschina.net/cppblog/blog/12092