1. 程式人生 > 程式設計 >使用Python解析Chrome瀏覽器書籤的示例

使用Python解析Chrome瀏覽器書籤的示例

Chrome 瀏覽器的書籤如果可以匯出,並轉換為我們需要的格式時,我們就可以編寫各種外掛來配合書籤的使用。

答案顯然是可以的,接下來我們以 Python 為例寫一個遍歷列印書籤的例子

書籤地址

先來說下獲取書籤的方法

Chrome 瀏覽器的書籤存放位置在各個平臺的區別

  • Mac
~/Library/Application Support/Google/Chrome/Default/Bookmarks
  • Linux
~/.config/google-chrome/Default/Bookmarks
  • Windows
%LOCALAPPDATA%"\Google\Chrome\User Data\Default\Bookmarks"

書籤結構

書籤內容為 JSON 格式,結構如下

{
  "checksum":"b196f618a9166d56dc6c98cfe9a98d45","roots":{
    "bookmark_bar":{
      "children":[
        {
          "date_added":"13246172853099058","guid":"83431411-157f-45f8-a9a4-d9af26c71bce","id":"1944","name":"blog local 溫欣爸比的部落格","type":"url","url":"http://localhost:4000/"
        },{
          "children":[
            {
              "date_added":"13246172853099058","url":"http://localhost:4000/"
            }
          ],"date_added":"13246172844427649","date_modified":"13246172865895702","guid":"6aa4ecce-a220-4689-9239-7df10965748b","id":"1943","name":"Blog","type":"folder"
        }
      ],"date_added":"13242060909278534","date_modified":"13246172853099058","guid":"00000000-0000-4000-a000-000000000002","id":"1","name":"書籤欄","type":"folder"
    },"other":{
      "children":[

      ],"date_added":"13242060909278616","date_modified":"0","guid":"00000000-0000-4000-a000-000000000003","id":"2","name":"其他書籤","synced":{
      "children":[

      ],"date_added":"13242060909278621","guid":"00000000-0000-4000-a000-000000000004","id":"3","name":"移動裝置書籤","type":"folder"
    }
  },"sync_metadata":"","version":1
}

清晰了這個結構在寫程式碼就很簡單了,以書籤欄為例,只需要將 data['roots']['bookmark_bar']['children'] 進行迴圈遍歷即可,程式碼詳情可見 demo

完整demo

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])
# Description: 列印不換行進度條
# 預覽 https://raw.githubusercontent.com/wxnacy/image/master/blog/python_progress.gif

import time


def get_progress(progress,total):
  '''獲取進度條'''
  progress_ratio = progress / total
  progress_len = 20
  progress_num = int(progress_ratio * 20)
  pro_text = '[{:-<20s}] {:.2f}% {} / {}'.format(
    '=' * progress_num,progress_ratio * 100,progress,total)
  return pro_text

def print_progress(total):
  '''模擬列印進度條'''
  progress = 0
  step = 30
  while progress < total:
    time.sleep(1)
    b = progress
    e = b + step
    progress += step
    end = '\r'
    if progress >= total:
      end = '\n'
      progress = total
    print(get_progress(progress,total),end = end)

if __name__ == "__main__":
  print_progress(100)

以上就是使用Python解析Chrome瀏覽器書籤的示例的詳細內容,更多關於Python解析Chrome瀏覽器書籤的資料請關注我們其它相關文章!