python導(dǎo)出chrome書簽到markdown文件的實(shí)例代碼
python導(dǎo)出chrome書簽到markdown文件,主要就是解析chrome的bookmarks文件,然后拼接成markdown格式的字符串,最后輸出到文件即可。以下直接上代碼,也可以在 py-chrome-bookmarks-markdown 中直接參見源碼。
from json import loads import argparse from platform import system from re import match from os import environ from os.path import expanduser # 過濾name filter_name_list = {'My work', '書簽欄', 'websites'} html_escape_table = { "&": "&", '"': """, "'": "'", ">": ">", "<": "<", } output_file_template = """ <h3>書簽?zāi)夸?lt;/h3> {catelog} {bookmark_bar} {other} """ # 如需本地調(diào)試可注釋掉這一段 START parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description="python導(dǎo)出chrome書簽到markdown文件.") parser.add_argument("input_file", type=argparse.FileType('r', encoding='utf-8'), nargs="?", help="讀取書簽的位置,可以指定文件位置(相對(duì)路徑,絕對(duì)路徑都可以),非必填,默認(rèn)為Chrome的默認(rèn)書簽位置") parser.add_argument("output_file", type=argparse.FileType('w', encoding='utf-8'), help="讀取書簽的位置,可以指定文件位置(相對(duì)路徑,絕對(duì)路徑都可以),必填") args = parser.parse_args() if args.input_file: input_file = args.input_file else: if system() == "Darwin": input_filename = expanduser("~/Library/Application Support/Google/Chrome/Default/Bookmarks") elif system() == "Linux": input_filename = expanduser("~/.config/google-chrome/Default/Bookmarks") elif system() == "Windows": input_filename = environ["LOCALAPPDATA"] + r"\Google\Chrome\User Data\Default\Bookmarks" else: print('Your system ("{}") is not recognized. Please specify the input file manually.'.format(system())) exit(1) try: input_file = open(input_filename, 'r', encoding='utf-8') except IOError as e: if e.errno == 2: print("The bookmarks file could not be found in its default location ({}). ".format(e.filename) + "Please specify the input file manually.") exit(1) output_file = args.output_file # 如需本地調(diào)試可注釋掉這一段 END # 本地調(diào)試可以指定文件名測(cè)試 START # input_filename = 'C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default/Bookmarks' # input_file = open(input_filename, 'r', encoding='utf-8') # output_file_name = 'test2.md' # output_file = open(output_file_name, 'w', encoding='utf-8') # 本地調(diào)試可以指定文件名測(cè)試 END # 目錄 catelog = list() def html_escape(text): return ''.join(html_escape_table.get(c, c) for c in text) def html_for_node(node): # 判斷url和children即判斷是否包含在文件夾中 if 'url' in node: return html_for_url_node(node) elif 'children' in node: return html_for_parent_node(node) else: return '' def html_for_url_node(node): if not match("javascript:", node['url']): return '- [{}]({})\n'.format(node['name'], node['url']) else: return '' def html_for_parent_node(node): return '{0}\n\n{1}\n'.format(filter_catelog_name(node), ''.join([filter_name(n) for n in node['children']])) # 過濾文件夾 def filter_name(n): if n['name'] in filter_name_list: return '' else: return html_for_node(n) # 過濾目錄名 def filter_catelog_name(n): if n['name'] in filter_name_list: return '' else: catelog.append('- [{0}](#{0})\n'.format(n['name'])) return '<h4 id={0}>{0}</h4>'.format(n['name']) contents = loads(input_file.read()) input_file.close() bookmark_bar = html_for_node(contents['roots']['bookmark_bar']) other = html_for_node(contents['roots']['other']) catelog_str = ''.join(a for a in catelog) output_file.write(output_file_template.format(catelog=catelog_str, bookmark_bar=bookmark_bar, other=other))
導(dǎo)出示例: https://github.com/kent666a/kent-resources/blob/master/bookmarks.md
總結(jié)
以上所述是小編給大家介紹的python導(dǎo)出chrome書簽到markdown文件的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 獲取CSDN文章內(nèi)容并轉(zhuǎn)換為markdown文本的python
- Python3自動(dòng)生成MySQL數(shù)據(jù)字典的markdown文本的實(shí)現(xiàn)
- python使用html2text庫(kù)實(shí)現(xiàn)從HTML轉(zhuǎn)markdown的方法詳解
- 解決python Markdown模塊亂碼的問題
- 如何用Python實(shí)現(xiàn)簡(jiǎn)單的Markdown轉(zhuǎn)換器
- python 自動(dòng)化將markdown文件轉(zhuǎn)成html文件的方法
- 使用Python來開發(fā)Markdown腳本擴(kuò)展的實(shí)例分享
- python3處理word文檔實(shí)例分析
- Python word文本自動(dòng)化操作實(shí)現(xiàn)方法解析
- Python操作word文檔插入圖片和表格的實(shí)例演示
- Python實(shí)現(xiàn)Word文檔轉(zhuǎn)換Markdown的示例
相關(guān)文章
python?中的?BeautifulSoup?網(wǎng)頁(yè)使用方法解析
這篇文章主要介紹了python?中的?BeautifulSoup?網(wǎng)頁(yè)使用方法解析,文章基于python的相關(guān)資料展開詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值需要的小伙伴可以參考一下2022-04-04通用的Django注冊(cè)功能模塊實(shí)現(xiàn)方法
這篇文章主要介紹了通用的Django注冊(cè)功能模塊實(shí)現(xiàn)步驟,幫助大家更好的理解和使用django,感興趣的朋友可以了解下2021-02-02python基礎(chǔ)入門詳解(文件輸入/輸出 內(nèi)建類型 字典操作使用方法)
這篇文章主要介紹了python基礎(chǔ)入門,包括文件輸入/輸出、內(nèi)建類型、字典操作等使用方法2013-12-12python根據(jù)json數(shù)據(jù)畫疫情分布地圖的詳細(xì)代碼
這篇文章主要介紹了python根據(jù)json數(shù)據(jù)畫疫情分布地圖的詳細(xì)代碼,掌握使用pyecharts構(gòu)建基礎(chǔ)的全國(guó)地圖可視化圖表,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Python 中的字符串基礎(chǔ)與應(yīng)用小結(jié)
在Python中,字符串可以用單引號(hào)或雙引號(hào)括起來,'hello' 與 "hello" 是相同的,這篇文章主要介紹了Python 中的字符串基礎(chǔ)與應(yīng)用,需要的朋友可以參考下2023-09-09Python編程super應(yīng)用場(chǎng)景及示例解析
最近有粉絲向我咨詢super相關(guān)的問題,說網(wǎng)上搜索到的教程不夠通俗易懂,看了之后還是不太理解。所以在這里基于我自己的理解來講解一下super2021-10-10