Python实现Chrome浏览器书签自动备份

import os
import json

# 设置 Chrome 书签文件的路径
chrome_path = r"C:\MyChromeDevUserData\Default\Bookmarks"

# 读取 Chrome 书签文件
with open(chrome_path, encoding="utf-8") as f:
    bookmarks_data = json.load(f)["roots"]["bookmark_bar"]

# 生成 HTML 文件
html_output = '<!DOCTYPE NETSCAPE-Bookmark-file-1>\n'
html_output += '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n'
html_output += '<TITLE>Chrome Bookmarks</TITLE>\n'
html_output += '<H1>Chrome Bookmarks</H1>\n<DL><p>\n'

# 处理书签数据,生成 HTML 标记
def process_bookmarks(bookmarks_list):
    bookmarks_output = ""
    for bookmark in bookmarks_list:
        if "children" in bookmark:
            bookmarks_output += "<DT><H3>{0}</H3>\n".format(bookmark["name"])
            bookmarks_output += "<DL><p>\n"
            bookmarks_output += process_bookmarks(bookmark["children"])
            bookmarks_output += "</DL><p>\n"
        else:
            bookmarks_output += '<DT><A HREF="{0}" ADD_DATE="{1}">{2}</A>\n'.format(
                bookmark["url"], int(bookmark["date_added"])/1000000, bookmark["name"]
            )
    return bookmarks_output

html_output += process_bookmarks(bookmarks_data["children"])
html_output += "</DL><p>\n"

# 将生成的 HTML 写入文件
with open("chrome_bookmarks.html", "w", encoding="utf-8") as file:
    file.write(html_output)

通过以上ptyhon代码可自动导出浏览器书签,可用于新浏览器导入。

注意:修改代码中chrome_path = r"C:\MyChromeDevUserData\Default\Bookmarks"为自己电脑chrome浏览器用户数据路径。

Tip:可通过命令pyinstaller -F py脚本文件,生成exe,双击执行导出备份书签。

可设置计算机任务计划,每日指定时间自动执行该exe,达到自动备份书签效果。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值