Truy xuất lịch sử trình duyệt Firefox bằng Python

Python cung cấp khả năng truy xuất lịch sử duyệt web từ Firefox thông qua việc xử lý cơ sở dữ liệu SQLite. Dưới đây là cách thực hiện với các thư viện tích hợp sẵn.

Các thư viện cần thiết

  • os: Duyệt hệ thống tập tin
  • sqlite3: Truy vấn cơ sở dữ liệu SQLite
  • time: Chuyển đổi định dạng thời gian

Định vị tập tin lịch sử

Firefox lưu trữ lịch sử trong tập tin places.sqlite. Hàm sau thực hiện tìm kiếm tập tin này trong ổ đĩa hệ thống.

def tim_kiem_file(thu_muc, ten_file):
    ket_qua = []
    for goc, thu_muc_con, files in os.walk(thu_muc):
        if ten_file in files:
            ket_qua.append(os.path.join(goc, ten_file))
    return ket_qua

Đọc dữ liệu lịch sử

Kết nối đến cơ sở dữ liệu và trích xuất thông tin từ bảng moz_places:

def doc_lich_su(duong_dan):
    ket_noi = sqlite3.connect(duong_dan)
    con_tro = ket_noi.cursor()
    truy_van = "SELECT url, title, visit_count, frecency, last_visit_date FROM moz_places"
    du_lieu = con_tro.execute(truy_van)
    
    muc_luc = set()
    for hang in du_lieu:
        if hang[1]:  # Kiểm tra tiêu đề tồn tại
            thoi_gian = ""
            if hang[4]:  # Chuyển đổi timestamp nếu có
                timestamp = int(hang[4]) / 1000000
                thoi_gian = time.asctime(time.localtime(timestamp))
            muc_luc.add((hang[0], hang[1], hang[2], hang[3], thoi_gian))
    ket_noi.close()
    return muc_luc

Lưu kết quả

Toàn bộ quy trình được tích hợp trong đoạn mã sau:

import os
import sqlite3
import time

def tim_kiem_file(thu_muc, ten_file):
    ket_qua = []
    for goc, thu_muc_con, files in os.walk(thu_muc):
        if ten_file in files:
            ket_qua.append(os.path.join(goc, ten_file))
    return ket_qua

def doc_lich_su(duong_dan):
    ket_noi = sqlite3.connect(duong_dan)
    con_tro = ket_noi.cursor()
    truy_van = "SELECT url, title, visit_count, frecency, last_visit_date FROM moz_places"
    du_lieu = con_tro.execute(truy_van)
    
    muc_luc = set()
    for hang in du_lieu:
        if hang[1]:
            thoi_gian = ""
            if hang[4]:
                timestamp = int(hang[4]) / 1000000
                thoi_gian = time.asctime(time.localtime(timestamp))
            muc_luc.add((hang[0], hang[1], hang[2], hang[3], thoi_gian))
    ket_noi.close()
    return muc_luc

if __name__ == "__main__":
    bat_dau = time.time()
    o_dia = os.getenv("SystemDrive")
    danh_sach_file = tim_kiem_file(o_dia, "places.sqlite")
    
    if danh_sach_file:
        tong_hop = []
        for file in danh_sach_file:
            ket_qua = doc_lich_su(file)
            tong_hop.append(ket_qua)
        
        with open("lich_su.txt", "w", encoding="utf-8") as f:
            dem = 0
            for nhom in tong_hop:
                for muc in nhom:
                    dem += 1
                    f.write(f"{muc}\n")
    else:
        print("Không tìm thấy tập tin lịch sử")
    
    print(f"Thời gian thực thi: {time.time() - bat_dau:.2f} giây")

Thẻ: python firefox SQLite browser-history data-extraction

Đăng vào ngày 1 tháng 10 lúc 08:38