私の備忘録がないわね...私の...

画像処理とかプログラミングのお話。

ChromeのブックマークをPythonで扱う

環境

目的

Bookmark のタイトルと URL を Markdown 形式で記述する.

PC 内に保存された JSON ファイルを用いる方法

import json

path = 'C:\\Users\\{ユーザー名}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks' # windows

with open(path, encoding='utf-8') as f:
    js_dict = json.load(f)

bookmark_list = js_dict['roots']['bookmark_bar']['children']
for bookmark in bookmark_list:
    if bookmark['type'] == 'url':
        bookmark_name = bookmark['name']
        url = bookmark['url']
        print(f'[{bookmark_name}]({url})')
        print()

エキスポートされた HTML ファイルを使う方法

pip3 install beautifulsoup4
from bs4 import BeautifulSoup

path = '/Users/kumano/Documents/bookmarks_2022_06_11.html'

with open(path, encoding='utf-8') as f:
    contents = f.read()

soup = BeautifulSoup(contents, features='html.parser')
link_list = soup.find_all('a')
for link in link_list:
    url = link.get('href')
    title = link.string
    print(f'[{title}]({url})')
    print()

結果

[Why ReLU Networks Yield High-Confidence Predictions Far Away From the Training Data and How to Mitigate the Problem](https://openaccess.thecvf.com/content_CVPR_2019/papers/Hein_Why_ReLU_Networks_Yield_High-Confidence_Predictions_Far_Away_From_the_CVPR_2019_paper.pdf)

[Feature Denoising for Improving Adversarial Robustness](https://openaccess.thecvf.com/content_CVPR_2019/papers/Xie_Feature_Denoising_for_Improving_Adversarial_Robustness_CVPR_2019_paper.pdf)