Google Chrome的互联网历史脚本

4 投票
5 回答
12872 浏览
提问于 2025-04-19 07:11

我并不是在寻找“最好”或最有效的脚本来完成这个任务。不过,我想知道有没有一种脚本可以从,比如说,谷歌浏览器中提取一天的上网历史,并把它记录到一个txt文件里。我更希望这个脚本是用Python或MATLAB写的。

如果你们有其他方法,可以用这两种语言利用谷歌浏览器本地存储的历史数据,我也非常乐意听听。

如果有人能帮我解决这个问题,我会非常感激!

5 个回答

0

这并不是你想要的东西。不过,使用这个你可以根据自己的需要来操作数据库表。

import os
import sqlite3

def Find_path():
    User_profile = os.environ.get("USERPROFILE")
    History_path = User_profile + r"\\AppData\Local\Google\Chrome\User Data\Default\History" #Usually this is where the chrome history file is located, change it if you need to.
    return History_path

def Main():
    data_base = Find_path()            
    con = sqlite3.connect(data_base) #Connect to the database
    c = con.cursor()
    c.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name") #Change this to your prefered query
    print(c.fetchall())
if __name__ == '__main__':
    Main()
0

这是另一个例子:

import csv, sqlite3, os
from datetime import datetime, timedelta

connection = sqlite3.connect(os.getenv("APPDATA") + "\..\Local\Google\Chrome\User Data\Default\history")
connection.text_factory = str
cur = connection.cursor()
output_file = open('chrome_history.csv', 'wb')
csv_writer = csv.writer(output_file)
headers = ('URL', 'Title', 'Visit Count', 'Date (GMT)')
csv_writer.writerow(headers)
epoch = datetime(1601, 1, 1)
for row in (cur.execute('select url, title, visit_count, last_visit_time from urls')):
    row = list(row)
    url_time = epoch + timedelta(microseconds=row[3])
    row[3] = url_time
    csv_writer.writerow(row)
0

我没有使用sqlite3/sqlite,而是用了一个谷歌浏览器的扩展程序“导出历史记录”,把所有的历史记录导出成一个CSV文件,然后再把这个CSV文件加载到MATLAB的单元格里。

导出历史记录

我的代码是:

file_o = ['history.csv'];
fid = fopen(file_o, 'rt');
fmt = [repmat('%s', 1, 6) '%*[^\n]'];
C = textscan(fid,fmt,'Delimiter',',','CollectOutput',true);
C_unpacked = C{:}; 
C_urls = C_unpacked(1:4199, 5);
4

接着m170897017说的内容:

那个文件是一个sqlite3数据库,所以直接用repr()去查看里面的内容是没有什么意义的。

你需要打开这个sqlite数据库,然后用SQL语句去查询,才能把数据提取出来。在Python中,可以使用标准库里的sqlite3库来实现这个操作。

这里有一个相关的SuperUser问题,里面展示了一些用来获取网址和时间戳的SQL语句:https://superuser.com/a/694283

5

根据我的理解,这个事情看起来很简单。我不确定这是不是你想要的。Chrome浏览器的上网历史记录会保存在一个特定的路径下。以Windows 7为例,它的存储路径是:C:\Users\[用户名]\AppData\Local\Google\Chrome\User Data\Default\History

在Python中:

f = open('C:\Users\[username]\AppData\Local\Google\Chrome\User Data\Default\History', 'rb')
data = f.read()
f.close()
f = open('your_expected_file_path', 'w')
f.write(repr(data))
f.close()

撰写回答