import json import os # 获取 Chrome 书签文件路径 if os.name == "nt": bookmarks_path = os.path.expanduser("~\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks") elif os.name == "posix": bookmarks_path = os.path.expanduser("~/Library/Application Support/Google/Chrome/Default/Bookmarks") else: raise NotImplementedError("不支持的操作系统") # 读取 Chrome 书签文件 with open(bookmarks_path, "r", encoding="utf-8") as f: bookmarks = json.load(f) # 提取书签的标题和网址 def extract_bookmarks(bookmarks_dict, bookmarks_list): for item in bookmarks_dict["children"]: if "children" in item: extract_bookmarks(item, bookmarks_list) elif "url" in item: title = item["name"] url = item["url"] bookmarks_list.append({"title": title, "url": url}) bookmarks_list = [] extract_bookmarks(bookmarks["roots"]["bookmark_bar"], bookmarks_list) # 输出标题和网址 for bookmark in bookmarks_list: print("标题:", bookmark["title"]) print("网址:", bookmark["url"]) print("-" * 40)