Chromeのブックマーク棚卸し

使い捨てスクリプトを貼っておきます。

  1. Cmd+Option+Bでブックマークマネージャーを表示する。
  2. 画面右上の3点リーダーから「エクスポート」を選択し、「bookmarks_2025_12_30.html」ファイルをダウンロードする。
  3. ダウンロードしたブックマークファイルと同じフォルダに以下のPythonのスクリプトを保存する。
import sys
import urllib.request
import os
import re

def check_url(url):
    """URLの生存確認を行う"""
    try:
        req = urllib.request.Request(
            url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
        )
        # タイムアウトは5秒に設定
        with urllib.request.urlopen(req, timeout=5) as response:
            return response.status < 400
    except:
        return False

def process_bookmarks(input_file):
    if not os.path.exists(input_file):
        print(f"Error: {input_file} not found.")
        return

    # ファイル名と拡張子を分割して出力用ファイル名を作成
    # 例: bookmarks_2025_12_30.html -> base="bookmarks_2025_12_30", ext=".html"
    base, ext = os.path.splitext(input_file)
    ok_file = f"{base}_ok{ext}"
    ng_file = f"{base}_ng{ext}"

    # HREF属性を抽出する正規表現
    href_pattern = re.compile(r'HREF="([^"]+)"', re.IGNORECASE)

    print(f"処理を開始します: {input_file}")

    with open(input_file, "r", encoding="utf-8") as f_in, \
         open(ok_file, "w", encoding="utf-8") as f_ok, \
         open(ng_file, "w", encoding="utf-8") as f_ng:

        for line in f_in:
            # リンク(Aタグ)が含まれる行か判定
            match = href_pattern.search(line)
            
            if match:
                url = match.group(1)
                # HTTP(S)リンクのみチェック
                if url.startswith("http"):
                    print(f"Checking: {url} ... ", end="", flush=True)
                    if check_url(url):
                        print("OK")
                        f_ok.write(line)
                    else:
                        print("NG")
                        f_ng.write(line)
                else:
                    # httpから始まらない特殊なリンク(chrome://等)は両方に出力
                    f_ok.write(line)
                    f_ng.write(line)
            else:
                # <DT><H3>などのフォルダ行、<DL>, <p>、インデント、
                # ヘッダー・フッターなどはそのまま両方のファイルに出力
                f_ok.write(line)
                f_ng.write(line)

    print(f"\n完了しました。")
    print(f"保存先(OK): {ok_file}")
    print(f"保存先(NG): {ng_file}")

if __name__ == "__main__":
    if len(sys.argv) > 1:
        process_bookmarks(sys.argv[1])
    else:
        print("Usage: python3 main.py bookmarks_2025_12_30.html")
  1. Pythonのスクリプトを実行する。
python3 main.py bookmarks_2025_12_30.html

 →以下のファイルが出力される。

  • bookmarks_2025_12_30_ok.html
  • bookmarks_2025_12_30_ng.html
  1. ngのファイルをブラウザで開き、リンクが切れているか確認する。結構間違っている、、

その他

ブックマークのチェック処理を実装しようとしたら、HTMLファイルのクセが強すぎてパースエラー。

Netscape Bookmark File Format

Throughout this file format definition, {date} is a decimal integer that represents the number of seconds elapsed since midnight January 1, 1970.

自力で実装するのも大変だし、かといって専用ライブラリを入れるほどでもない。そもそも個人情報だけどGoogle様には筒抜けなんだからGemini先生にファイル投げて『リンク切れチェックしといて』で済ませようとしたら、それは無理とのこと。

結局、Gemini先生にPythonコードを書いてもらって自分で実行する形に。年末年始のブックマーク大掃除用、使い捨てスクリプトです。

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です