在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
上一篇: 参数绑定
下一篇: P5028 Annihilate(SA)