版本:python 3.9
库:request,re
# 过滤失效的谷歌浏览器书签
import re
import requests
# 按 Shift+F10 执行或将其替换为您的代码。
# 按 双击 Shift 在所有地方搜索类、文件、工具窗口、操作和设置。
def main_start():
new_file = open('./整理后的书签文件.html', mode='w', encoding='utf-8')
new_file2 = open('./失效的书签文件.bed.html', mode='w', encoding='utf-8')
# 字符匹配
preg_match = re.compile('A HREF=".*ADD_DATE')
preg_match_github = re.compile('github')
filter_html = ''
bed_html = ''
with open('./谷歌浏览器导出的书签文件', encoding='utf-8') as bookmark:
num = 0
for item in bookmark.readlines():
if num == 10:
break
str_match = preg_match.search(item)
if str_match != None:
group_str = str_match.group()
group_str = group_str.replace('A HREF="', '')
group_str = group_str.replace('" ADD_DATE', '')
print(f' 开始请求:{group_str}')
try:
github_search = preg_match_github.search(group_str)
if github_search != None:
filter_html += item + '\n'
else:
rr = requests.get(group_str, timeout=5)
if rr.status_code == 404:
bed_html += group_str + '\n'
print(f'{group_str} 这个网址失效啦!')
else:
filter_html += item + '\n'
print(f' 成功响应:{group_str}')
except:
bed_html += group_str + '\n'
print(f'except:{group_str} 这个网址失效啦!')
else:
filter_html += item + '\n'
new_file.write(filter_html)
new_file2.write(bed_html)
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
main_start()