python读取chrome/firefox本地缓存cookie
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python读取chrome/firefox本地缓存cookie,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1854字,纯文字阅读大概需要3分钟。
内容图文
# chrome
def get_cookie_from_chrome(hosts_list=None):
cookiepath=os.environ['LOCALAPPDATA']+r"\Google\Chrome\User Data\Default\Cookies"
sql = "select host_key,name,encrypted_value from cookies "
if hosts_list:
sql_where = " where "
for hosts in hosts_list:
sql_where += " host_key = '{}' or ".format(hosts)
sql_where = sql_where[: len(sql_where) - len(' or ')]
sql += sql_where
with sqlite3.connect(cookiepath) as conn:
cu=conn.cursor()
cookies_arr = []
for host_key, name, encrypted_value in cu.execute(sql).fetchall():
if name == 'miniDialog':
continue
cookies_arr.append('{}={}'.format(name, CryptUnprotectData(encrypted_value)[1].decode()))
if cookies_arr:
return '; '.join(cookies_arr)
# firefox
def get_firfox_cookie_path():
cookiepath_common = os.environ['APPDATA'] + r"\Mozilla\Firefox\Profiles"
folds_arr = os.listdir(cookiepath_common)
folds_end = [os.path.splitext(file)[-1][1:] for file in folds_arr]
if 'default-release' in folds_end:
cookie_fold_index = folds_end.index('default-release')
else:
cookie_fold_index = folds_end.index('default')
cookie_fold = folds_arr[cookie_fold_index]
cookie_path = os.path.join(cookiepath_common, cookie_fold)
return os.path.join(cookie_path, 'cookies.sqlite')
def get_cookie_from_firfox(hosts_list=None):
cookie_file = get_firfox_cookie_path()
with sqlite3.connect(cookie_file) as conn:
cur=conn.cursor()
# sql = "select sql from sqlite_master where type = 'table';"
sql = "select baseDomain, name, value from moz_cookies"
# sql = "pragma table_info('moz_cookies');"
if hosts_list:
sql_where = " where "
for hosts in hosts_list:
sql_where += " baseDomain = '{}' or ".format(hosts)
sql_where = sql_where[: len(sql_where) - len(' or ')]
sql += sql_where
cookie_arr = []
for baseDomain, name, value in cur.execute(sql).fetchall():
if name == 'miniDialog':
continue
cookie_arr.append('{}={}'.format(name, value))
if cookie_arr:
return '; '.join(cookie_arr)
内容总结
以上是互联网集市为您收集整理的python读取chrome/firefox本地缓存cookie全部内容,希望文章能够帮你解决python读取chrome/firefox本地缓存cookie所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。