将Chrome历史记录文件中的日期时间字段转换为可读格式
我正在写一个脚本,用来收集用户的浏览器历史记录,并带有时间戳(这是在教育环境下进行的)。Firefox 3 的历史记录保存在一个 sqlite 文件里,时间戳是以 UNIX 时间格式存储的……通过 Python 中的 SQL 命令获取这些数据并转换成可读的格式其实很简单:
sql_select = """ SELECT datetime(moz_historyvisits.visit_date/1000000,'unixepoch','localtime'),
moz_places.url
FROM moz_places, moz_historyvisits
WHERE moz_places.id = moz_historyvisits.place_id
"""
get_hist = list(cursor.execute (sql_select))
Chrome 也把历史记录存储在一个 sqlite 文件中……但是它的历史时间戳似乎是从 1601 年 1 月 1 日午夜 UTC 开始的微秒数……
那么,如何把这个时间戳转换成像 Firefox 示例中那样的可读格式(比如 2010-01-23 11:22:09)呢?我正在用 Python 2.5.x(这是 OS X 10.5 上的版本),并且导入了 sqlite3 模块……
4 个回答
4
这是一种更符合Python风格且更节省内存的方法来实现你所描述的功能(顺便说一下,感谢你提供的初始代码!):
#!/usr/bin/env python
import os
import datetime
import sqlite3
import opster
from itertools import izip
SQL_TIME = 'SELECT time FROM info'
SQL_URL = 'SELECT c0url FROM pages_content'
def date_from_webkit(webkit_timestamp):
epoch_start = datetime.datetime(1601,1,1)
delta = datetime.timedelta(microseconds=int(webkit_timestamp))
return epoch_start + delta
@opster.command()
def import_history(*paths):
for path in paths:
assert os.path.exists(path)
c = sqlite3.connect(path)
times = (row[0] for row in c.execute(SQL_TIME))
urls = (row[0] for row in c.execute(SQL_URL))
for timestamp, url in izip(times, urls):
date_time = date_from_webkit(timestamp)
print date_time, url
c.close()
if __name__=='__main__':
opster.dispatch()
这个脚本可以这样使用:
$ ./chrome-tools.py import-history ~/.config/chromium/Default/History* > history.txt
当然,你可以不使用Opster,但我觉得它还是挺方便的 :-)
16
试试这个:
sql_select = """ SELECT datetime(last_visit_time/1000000-11644473600,'unixepoch','localtime'),
url
FROM urls
ORDER BY last_visit_time DESC
"""
get_hist = list(cursor.execute (sql_select))
或者类似这样的东西
对我来说似乎有效。
1
这可能不是世界上最符合Python风格的代码,但这是一个解决方案:通过调整时区(这里是东部标准时间)来“作弊”,方法如下:
utctime = datetime.datetime(1601,1,1) + datetime.timedelta(microseconds = ms, hours =-5)
这是一个函数:它假设Chrome的历史记录文件已经从另一个账户复制到了/Users/someuser/Documents/tmp/Chrome/History这个路径下。
def getcr():
connection = sqlite3.connect('/Users/someuser/Documents/tmp/Chrome/History')
cursor = connection.cursor()
get_time = list(cursor.execute("""SELECT last_visit_time FROM urls"""))
get_url = list(cursor.execute("""SELECT url from urls"""))
stripped_time = []
crf = open ('/Users/someuser/Documents/tmp/cr/cr_hist.txt','w' )
itr = iter(get_time)
itr2 = iter(get_url)
while True:
try:
newdate = str(itr.next())
stripped1 = newdate.strip(' (),L')
ms = int(stripped1)
utctime = datetime.datetime(1601,1,1) + datetime.timedelta(microseconds = ms, hours =-5)
stripped_time.append(str(utctime))
newurl = str(itr2.next())
stripped_url = newurl.strip(' ()')
stripped_time.append(str(stripped_url))
crf.write('\n')
crf.write(str(utctime))
crf.write('\n')
crf.write(str(newurl))
crf.write('\n')
crf.write('\n')
crf.write('********* Next Entry *********')
crf.write('\n')
except StopIteration:
break
crf.close()
shutil.copy('/Users/someuser/Documents/tmp/cr/cr_hist.txt' , '/Users/parent/Documents/Chrome_History_Logs')
os.rename('/Users/someuser/Documents/Chrome_History_Logs/cr_hist.txt','/Users/someuser/Documents/Chrome_History_Logs/%s.txt' % formatdate)