栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

用Python实现的书签分类器

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Python实现的书签分类器

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

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

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

# read the original bookmarks html, filter the link and text of with open(html, 'r') as f_origin:
    lines = re.findall('
(.*?)
', 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('">(.*?)', 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 bookmarksdef 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 = 'n' 
            + 'n' 
            + 'Bookmarksn' 
            + 'Bookmarksn' 
            + '

n'     for i, item in enumerate(link_list_new):         group += 't

' + type_dict_reverse[i] + 'nt

n'         for j in item:             one = 'tt

' + j[1] + 'n'             group += one         group += 't

n'     group += '

n'     f_new.write(group)

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

import re, json# the bookmarks file exported from your browserhtml = 'bookmarks.html'# the new bookmarks file we want to gethtml_new = 'bookmarks_new.html'# init listlink_list = []
link_list_new = [[] for i in range(6)]# config file of classifiercategory_dict = json.load(open('classify.txt', 'r'))# config file of classifier typetype_dict = json.load(open('classify_type.txt', 'r'))# reverse the dict abovetype_dict_reverse = dict(zip(type_dict.values(), type_dict.keys()))

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



作者:八宝粥BBZ
链接:https://www.jianshu.com/p/7041b812e811


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/223968.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号