python导出chrome书签到markdown文件的实例代码
作者:深蓝浅蓝的天 关注 时间:2022-08-26 00:49:36
python导出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>书签目录</h3>
{catelog}
{bookmark_bar}
{other}
"""
# 如需本地调试可注释掉这一段 START
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description="python导出chrome书签到markdown文件.")
parser.add_argument("input_file", type=argparse.FileType('r', encoding='utf-8'), nargs="?",
help="读取书签的位置,可以指定文件位置(相对路径,绝对路径都可以),非必填,默认为Chrome的默认书签位置")
parser.add_argument("output_file", type=argparse.FileType('w', encoding='utf-8'),
help="读取书签的位置,可以指定文件位置(相对路径,绝对路径都可以),必填")
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
# 如需本地调试可注释掉这一段 END
# 本地调试可以指定文件名测试 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')
# 本地调试可以指定文件名测试 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))
导出示例: https://github.com/kent666a/kent-resources/blob/master/bookmarks.md
总结
以上所述是小编给大家介绍的python导出chrome书签到markdown文件的实例代码网站的支持!
来源:https://www.jianshu.com/p/b4c1ad6d9399?utm_source=tuicool&utm_medium=referral
标签:python,markdown,chrome
0
投稿
猜你喜欢
Django 限制访问频率的思路详解
2021-08-17 16:52:57
详解Python验证码识别
2023-11-12 23:46:40
CSS属性behavior的语法及介绍
2010-01-13 12:40:00
如何使用Pytorch搭建模型
2022-07-18 10:34:27
javascript结合ajax读取txt文件内容
2024-04-30 10:14:58
OpenCV清除小面积连通域的实现方法
2023-11-16 03:58:25
Python3.6使用tesseract-ocr的正确方法
2023-10-24 20:46:28
chatGPT之Python API启用上下文管理案例详解
2022-09-21 13:51:43
对网页切片算法问题的研究
2008-06-05 12:41:00
flask中使用SQLAlchemy进行辅助开发的代码
2021-09-10 07:46:43
python mysqldb连接数据库
2024-01-15 17:52:56
Python使用面向对象方式创建线程实现12306售票系统
2021-01-22 09:13:36
在Python中使用元类的教程
2021-04-12 05:09:34
linux 后台日志 mysql 错误异常的解释(推荐)
2024-01-26 06:01:42
深入浅析python3 依赖倒置原则(示例代码)
2021-07-30 02:27:33
python requests模拟登陆github的实现方法
2022-10-05 21:37:48
MySQL百万级高并发网站实战攻略
2009-03-25 15:49:00
python scipy.misc.imsave()函数的用法说明
2022-11-01 13:04:24
ASP sql:rs.open语句详细说明
2008-03-17 11:30:00
详解Python下Flask-ApScheduler快速指南
2023-12-25 07:37:34