一直苦于瀏覽器收藏的書簽太多太亂眷昆,而每次手工整理都需要花費大量的時間。于是用Python實現(xiàn)了一個書簽分類器面哥,具體過程如下上荡。
感興趣的同學(xué)可以去Github下載完整代碼及相關(guān)配置文件(Github傳送門:bookmarks-classifier)趴樱,運(yùn)行、體驗一下;也可以繼續(xù)修改叁征、完善代碼纳账;同時,歡迎各種pull request航揉,也歡迎各種star塞祈、fork...
1.讀取書簽文件(HTML格式,由瀏覽器導(dǎo)出)帅涂,用正則表達(dá)式獲取到所有的A標(biāo)簽(即鏈接)內(nèi)容
# 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.定義書簽分類器议薪,根據(jù)上一步得到的標(biāo)簽內(nèi)容列表,進(jìn)行分類
# 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.將上一步中的分類結(jié)果導(dǎo)出為相同格式的書簽文件
# 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)來自定義分類結(jié)果斯议。