用Python实现的书签分类器

一直苦于浏览器收藏的书签太多太乱,而每次手工整理都需要花费大量的时间。于是用Python实现了一个书签分类器,具体过程如下。

感兴趣的同学可以去Github下载完整代码及相关配置文件(Github传送门:bookmarks-classifier),运行、体验一下;也可以继续修改、完善代码;同时,欢迎各种pull request,也欢迎各种star、fork...

1.读取书签文件(HTML格式,由浏览器导出),用正则表达式获取到所有的A标签(即链接)内容

# read the original bookmarks html, filter the link and text of <a>
with open(html, 'r') as f_origin:
    lines = re.findall('<DT>(.*?)<DT>', f_origin.read(), re.S)
    print('Total:' + str(len(lines)))
    for line in lines:
        domain = re.findall('://[a-zA-Z0-9]*\.(.*?)\.', line, re.S)
        link = re.findall('HREF="(.*?)"', line, re.S)
        text = re.findall('">(.*?)</A>', line, re.S)
        if len(domain) > 0 and len(link) > 0 and len(text) > 0:
            link_item = (domain[0], link[0], text[0])
            link_list.append(link_item)
    print(link_list)
    print('Filter:' + str(len(link_list)))
    classify(link_list)

2.定义书签分类器,根据上一步得到的标签内容列表,进行分类

# the classifier of bookmarks
def classify(list):
    for domain, link, text in list:
        if domain not in category_dict:
            cate = 'other'
        else:
            cate = category_dict[domain]
        link_item_new = (link, text)
        link_list_new[type_dict[cate]].append(link_item_new)
    print(link_list_new)
    print('classify:' + str(len(link_list_new)))

3.将上一步中的分类结果导出为相同格式的书签文件

# write the results to a new bookmarks html
with open(html_new, 'w') as f_new:
    group = '<!DOCTYPE NETSCAPE-Bookmark-file-1>\n' \
            + '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n' \
            + '<TITLE>Bookmarks</TITLE>\n' \
            + '<H1>Bookmarks</H1>\n' \
            + '<DL><p>\n'
    for i, item in enumerate(link_list_new):
        group += '\t<DT><H3 ADD_DATE="" LAST_MODIFIED="">' + type_dict_reverse[i] + '</H3>\n\t<DL><p>\n'
        for j in item:
            one = '\t\t<DT><A HREF="' + j[0] + '" ADD_DATE="" ICON="">' + j[1] + '</A>\n'
            group += one
        group += '\t</DL><p>\n'
    group += '</DL><p>\n'
    f_new.write(group)

4.使用的库及变量的初始化

import re, json

# the bookmarks file exported from your browser
html = 'bookmarks.html'
# the new bookmarks file we want to get
html_new = 'bookmarks_new.html'
# init list
link_list = []
link_list_new = [[] for i in range(6)]
# config file of classifier
category_dict = json.load(open('classify.txt', 'r'))
# config file of classifier type
type_dict = json.load(open('classify_type.txt', 'r'))
# reverse the dict above
type_dict_reverse = dict(zip(type_dict.values(), type_dict.keys()))

附:读者可以通过修改代码库中的配置文件(classify.txt、classify_type.txt)来自定义分类结果。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,046评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,410评论 19 139
  • 我很喜欢夜读,在24小时的三联选一本自己喜欢的书彻夜读完。 如果要在夏日里挑出一件与之媲美的事,恐怕只有冲完澡后吞...
    Fish_琪阅读 1,009评论 0 3
  • 尘世万千,浮华隐现 老剑不言,岁月缄声 湖畔一苇停扁舟 江山万里挂风流 好友两三,侠义飘摇 遍数恩怨情仇 几回对错...
    坏男人一生的承诺阅读 2,921评论 0 0
  • 你 我 好久不见
    简茧笺阅读 250评论 0 0

友情链接更多精彩内容