python解析Chrome瀏覽器歷史瀏覽記錄和收藏夾數據

前言

常使用chrome瀏覽器作為自己的默認瀏覽器,也喜歡使用瀏覽器來收藏自己的喜歡的有用的鏈接,自己也做瞭一個記錄筆記的小腳本,想擴展收錄chrome瀏覽器收藏夾的內容,,下面,,使用python提取chrome瀏覽器的歷史記錄,以及收藏夾。

(一)查詢chrome數據緩存地址

1.打開 chrome瀏覽器,輸入 chrome://version,進入瀏覽器版本信息頁面 2.復制頁面下圖,劃線地址

請添加圖片描述

(二)提取收藏夾數據

1.文件路徑

上面我的chrome瀏覽器的緩存路徑是:
C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default
瀏覽器的收藏夾的數據,記錄在Bookmarks文件裡面
Bookmark文件的內容格式是json

請添加圖片描述

2.解析代碼

解析代碼為

import os
import json
#chrome data path
path = "C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default"
#chrome browser bookmark
class BookMark:
    
    def __init__(self,chromePath=path):
        #chromepath
        self.chromePath = chromePath
        #parse bookmarks
        with open(os.path.join(path,'Bookmarks'),encoding='utf-8') as f:
            bookmarks = json.loads(f.read())
        self.bookmarks = bookmarks
        #folders
        self.folders = self.get_folders()
        
    def get_folders(self):
        #folders
        names = [
            (i,self.bookmarks['roots'][i]['name']) 
            for i in self.bookmarks['roots']
                 ]
        return names
    
    def get_folder_data(self,folder=0):
        return self.bookmarks['roots'][self.folders[folder][0]]['children']
        
    def set_chrome_path(self,chromePath):
        self.chromePath = chromePath
        
    def refresh(self):
        'update chrome data from chrome path'
        #parse bookmarks
        with open(os.path.join(path,'Bookmarks'),encoding='utf-8') as f:
            bookmarks = json.loads(f.read())
        self.bookmarks = bookmarks

(三)查看瀏覽歷史數據

1.文件路徑

歷史數據,存儲在下面的History文件裡面,內容格式是sqlite的數據庫文件,可以直接使用sqlite3來解析,當然也可以使用DB Browser for SQLite來圖形化界面顯示History sqlite數據文件。

請添加圖片描述

2.解析代碼

import os
import sqlite3

#chrome data path
path = "C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default"

#History
class History:
    def __init__(self,chromePath=path):
        self.chromePath = chromePath
        
    def connect(self):
        self.conn = sqlite3.connect(os.path.join(self.chromePath,"History"))
        self.cousor = self.conn.cursor()
        
    def close(self):
        self.conn.close()
        
    def get_history(self):
        cursor = self.conn.execute("SELECT id,url,title,visit_count  from urls")
        rows = []
        for _id,url,title,visit_count in cursor:
            row = {}
            row['id'] = _id
            row['url'] = url
            row['title'] = title
            row['visit_count'] = visit_count
            rows.append(row)
        return rows

(四)完整代碼&測試代碼

import os
import sqlite3

#chrome data path
path = "C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default"

#History
class History:
    def __init__(self,chromePath=path):
        self.chromePath = chromePath
        
    def connect(self):
        self.conn = sqlite3.connect(os.path.join(self.chromePath,"History"))
        self.cousor = self.conn.cursor()
        
    def close(self):
        self.conn.close()
        
    def get_history(self):
        cursor = self.conn.execute("SELECT id,url,title,visit_count  from urls")
        rows = []
        for _id,url,title,visit_count in cursor:
            row = {}
            row['id'] = _id
            row['url'] = url
            row['title'] = title
            row['visit_count'] = visit_count
            rows.append(row)
        return rows

總結

到此這篇關於python解析Chrome瀏覽器歷史瀏覽記錄和收藏夾數據的文章就介紹到這瞭,更多相關python解析Chrome瀏覽器內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: