使用脚本收集带有时间戳的用户浏览器历史记录(教育设置)。 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模块....
答案 0 :(得分:14)
试试这个:
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 :(得分:4)
这是一种更加pythonic和内存友好的方式来做你所描述的(顺便说一句,感谢初始代码!):
#!/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可以被抛弃,但对我来说似乎很方便: - )
答案 2 :(得分:1)
sqlite
模块返回datetime字段的datetime
个对象,这些字段具有用于打印名为strftime
的可读字符串的格式方法。
拥有记录集后,您可以执行以下操作:
for record in get_hist:
date_string = record[0].strftime("%Y-%m-%d %H:%M:%S")
url = record[1]
答案 3 :(得分:1)
这可能不是世界上最恐怖的代码,但这里有一个解决方案:通过这样做来调整时区(EST):
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)