python - 有没有办法在不使用 Python 本地数据库的情况下获取 Chrome 历史记录和书签?

标签 python google-chrome

我正在尝试创建一个能够检索 Chrome 历史记录和用户书签的 Python 程序;然而,在 C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default\History 中使用本地存储的数据库时我遇到的问题是,如果 Chrome 当前处于打开状态,我会收到一条错误消息:

Traceback (most recent call last):
  File "C:/Users/*****/PycharmProjects/*******/chromer_history_reader.py", line 10, in <module>
    cursor.execute(select_statement)
sqlite3.OperationalError: database is locked

有解决办法吗?

我已经尝试研究使用 Chrome API 获取历史记录的方法 (https://developers.chrome.com/extensions/history),但似乎我只能通过 JavaScript 和 Chrome 扩展来使用 API。

import os
import sqlite3
from pprint import pprint

history_db = os.getenv("APPDATA") + "\\..\\Local\\Google\\Chrome\\User Data\\Default\\History"

c = sqlite3.connect(history_db)
cursor = c.cursor()
select_statement = "SELECT urls.url, urls.visit_count FROM urls, visits WHERE urls.id = visits.url;"
cursor.execute(select_statement)

results = cursor.fetchall()

pprint(results)

我希望能够看到用户的 chrome 历史记录/书签,即使 chrome 当前处于打开状态。

最佳答案

您可以使用该程序在计算机上查看原始历史文件,方法是使用以下代码将您的历史文件复制到计算机上不在 Chrome 保存文件夹中的其他位置

import shutil

shutil.copy2(os.getenv("APPDATA") + "\\..\\Local\\Google\\Chrome\\User Data\\Default\\History", "NEW FILE LOCATION\\HISTORY")

history_db = "NEW FILE LOCATION\\HISTORY"

关于python - 有没有办法在不使用 Python 本地数据库的情况下获取 Chrome 历史记录和书签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55962510/

相关文章:

python - 如何在 Chrome 驱动程序中使用 Selenium 重用预加载的图像?

Javascript window.scrollTo 在不同浏览器上的行为

python - 尝试通过 Google Picasa API 添加照片或相册时无法进行身份验证

python - 自定义 Python 字符映射编解码器

javascript - 创建 iframe 然后修改其内容

javascript - chrome xml 下载警告

python - Pymongo 在子文档中通过 _id 查找

python - python中的套接字编程 - 时间戳

python - 使用 webdriver 滚动到元素?

google-chrome - 在不打开新标签页的情况下强制从 Google Chrome 下载图片