当前位置:主页 > 软件编程 > Python代码 >

python解析Chrome浏览器历史浏览记录和收藏夹数据

时间:2022-07-09 09:36:52 | 栏目:Python代码 | 点击:

前言

常使用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

总结

您可能感兴趣的文章:

相关文章