我正在尝试使用sqlite3
浏览我的chrome数据库,找到我输入的最新网页,并检查它们是否需要密码。到目前为止,我实现的是:
('2018-07-30 07:28:56', '1970-01-01 00:00:30', 'strftime (DateTime) - APIdock')
('2018-07-30 07:28:55', '1970-01-01 00:00:31', 'python date time get the current time but with seconds and hour and minute - Stack Overflow')
上面的结果是运行代码时得到的结果(发布在最后)。现在我有两个问题:
'1970-01-01 00:00:31'
中有visit_duration
而不是00:00:31
?import os
import sqlite3
import pandas as pd
def mkdir(name):
if not os.path.exists(name):
os.makedirs(name)
def execute_and_fetch(db, statement):
cursor = db.cursor()
cursor.execute(statement)
return cursor.fetchall()
def get_user_history_path():
#path to user's history database (Chrome)
data_path = os.path.expanduser('~') + r"\AppData\Local\Google\Chrome\User Data\Default"
history_db = os.path.join(data_path, 'history')
return history_db
def dump_all_tables_to_csv(db_path):
statement = "SELECT name FROM sqlite_master WHERE type='table';"
db = sqlite3.connect(db_path)
dir_name = "tables_csv_format"
mkdir(dir_name)
os.chdir(dir_name)
for table_name in execute_and_fetch(db, statement):
table_name = table_name[0]
table = pd.read_sql_query("SELECT * from %s" % table_name, db)
table.to_csv(table_name + '.csv', index_label='index')
def get_last_urls_visited(db_path, num = 2):
statement = """
SELECT
datetime(visit_time / 1000000 + (strftime('%s', '1601-01-01')), 'unixepoch'),
datetime(visit_duration / 1000000 + (strftime('%H:%M:%S')), 'unixepoch'),
urls.title
FROM visits, urls
WHERE urls.id = visits.url
ORDER BY visit_time DESC
LIMIT {};
""".format(num)
db = sqlite3.connect(db_path)
results = execute_and_fetch(db, statement)
print(*results, sep = "\n")
def main():
google_chrome_db = get_user_history_path()
# Done once, to save the tables for convenience
# dump_all_tables_to_csv(google_chrome_db)
get_last_urls_visited(google_chrome_db)
main()