用python一键清理收藏夹
失效的书签们
当我们面对成百上千的书签和收藏夹的时候,我们总会头疼不已
尤其是昨天还在更新的程序设计博客,今天就挂了永不更新。或者是昨天看的起劲的电影网站,今天直接404。失效页面这么多,每次我打开才知道失效了,并且需要手动删除,这能是一个程序员干的事情吗?可是无论是Google浏览器还是国内浏览器,最多也就提供一个对于收藏夹的备份服务,那只能python走起了。
python支持的收藏夹文件格式
对于收藏夹提供的支持很少,主要还是因为收藏夹藏在浏览器里面,我们只能手动导出htm文件进行管理
内容比较简单,对前端没什么了解的我,也可以很明显看出其中的树形结构和内在逻辑。
固定格式 网址 固定格式 页面名 固定格式
很简单的想到了正则匹配,其中有两个子串。
提取出来再挨个访问,看看哪个失效了,就删除,就能活得清理后的收藏夹了。
读取收藏夹文件
path = "C:\\Users\\XU\\Desktop"
fname = "bookmarks.html"
os.chdir(path)
bookmarks_f = open(fname, "r+" ,encoding='UTF-8')
booklists = bookmarks_f.readlines()
bookmarks_f.close()
因为对于前端的不熟悉,这个导出的收藏夹可以抽象的分成
- 结构代码
- 保存网页书签的关键代码
其中结构代码我们不能动,要原封不动的保留,而保存网页书签的关键代码,我们要提取内容并且进行判断保留和删除。
所以这里采用readlines函数,每行读取,单独判断。
正则匹配
pattern = r'HREF="(.*?)" .*?>(.*?)</A>'
while len(booklists)>0:
bookmark = booklists.pop(0)
detail = re.search(pattern, bookmark)
如果是关键代码:提取出的子串在 detail.group(1) 和 detail.group(2) 里面
而如果是结构代码:detail == None
访问页面
import requests
r = requests.get(detail.group(1),timeout=500)
编代码尝试之后发现会有这四种情况
- r.status_code == requests.codes.ok
-
r.status_code==404
-
r.status_code!=404 && 无法访问 (可能是屏蔽爬虫,建议保留)
-
requests.exceptions.ConnectionError
类似知乎、简书基本都反爬了,所以简单的get还不能有效访问,细节不值得大费周章,直接保留就好。
而error,直接用try抛出异常就好,不然程序会停止运行。
添加逻辑后:
while len(booklists)>0:
bookmark = booklists.pop(0)
detail = re.search(pattern, bookmark)
if detail:
#print(detail.group(1) +"----"+ detail.group(2))
try:
#访问
r = requests.get(detail.group(1),timeout=500)
#如果可则添加
if r.status_code == requests.codes.ok:
new_lists.append(bookmark)
print( "ok------ 保留:"+ detail.group(1)+" "+ detail.group(2))
else:
if(r.status_code==404):
print("不可访问 删除:"+ detail.group(1)+" "+ detail.group(2) +'错误码 '+str(r.status_code))
else:
print("其他原因 保留:"+ detail.group(1)+" "+ detail.group(2) +'错误码 '+str(r.status_code))
new_lists.append(bookmark)
except:
print( "不可访问 删除:"+ detail.group(1)+" "+ detail.group(2))
#new_lists.append(bookmark)
else:#没匹配到是结构语句
new_lists.append(bookmark)
程序执行情况!
导出htm
bookmarks_f = open('new_'+fname, "w+" ,encoding='UTF-8')
bookmarks_f.writelines(new_lists)
bookmarks_f.close()
导入浏览器
实际应用于我的浏览器
确实有很多电影网都失效了,通过python能够一键清理其中无法访问的书签。Python让计算机更高效~
最后本文完整代码已上传分享,可以在公众号「小詹学Python」后台回复关键词「20190908」获取。
⬆️长按 2 秒关注「 小詹学Python 」
Varfor Online Dating ar battre an traditionell dating Dating en magma grunt kapitel 9 dejtingsajt i Madrid Spanien scammers pa Badoo dejtingsajt Patrick Fugit dating historia http://aclassynerd.com/13-r-gammal-dating-22-r-gammal.html Dating telefonnummer Storbritannien
However, for women going through menopause, this sudden drop in desire for
sexual activity or intimacy can be troubling. https://saleviabuy.com viagra generic over the counter
Treatment is divided into two phases. https://genqpviag.com do generic viagra pills work
COX-2 is also involved in thermoregulation and the pain response to injury.
viagra online cheap price
online generic viagra reviews
viagra for hair es necesario receta para la viagra boyfriend secretly taking viagra
order viagra online canada viagra professional review viagra
ejakulation bei viagra buybuyviamen.com lampara viagra
top-viagra-store.com purevigra.com does viagra keep you erect after ejaculation
This is really interesting, You are a very skilled blogger.
I’ve joined your feed and look forward to seeking more
of your fantastic post. Also, I have shared your site in my social
networks!
Here is my blog post – 더킹카지노
Excellent blog you have got here.. It’s difficult to find good quality
writing like yours these days. I honestly appreciate people
like you! Take care!!
My page 카지노사이트
generic viagra discount paradiseviagira.com viagra dapoxetin
viagra v6 can diabetics take viagra email virus link to viagra
Order viagra online canada mastercard https://mygoviagar.com/ cheap viagra pills free shipping
Viagra sale https://lightvigra.com viagra price in malaysia
Viagra india hopeviagrin.com otc generic viagra
Where to buy female viagra pill https://miraclevigra.com how to purchase viagra pills
viagra tablet online india generic viagra no prescription viagra online lowest price
buy viagra online mastercard viagra professional pfizer viagra pills no prescription
get viagra free samples cuando vence patente viagra cari viagra
vaigra female viagra gel viagra for female for sale
where can you buy viagra over the counter viagra generic india viagra 100mg pills generic
man overdosed viagra natural viagra around the house dia diem ban viagra
research paper zika virus research proposal psychology essays
a personal statement college essays for sale guidelines in writing an essay
how i can get viagra in norway supreme suppliers viagra viagra 50mg price
writing a thesis statement for research paper probability homework help order term paper
buy essay paper expository essay assignment maths homework help online
essay on helping the poor writing a research paper why do you want to go to college essay
Buy essay https://buyessayhelpbtg.com buy
essays online reviews
do all colleges require essays assignments writing services chemistry homework help online
essay writing on child labour algebra 1 homework 5 cpm homework help
writing essays for fun historical research proposal writing resume cover letter
cialis tablets australia generic cialis india cialis
buy viagra super force with mastercard order viagra online australia buy viagra in canada
generic viagra australia buy viagra without a perscription brand name viagra
viagra professional viagra cialis viagra mit pay pal bezahlen
cialis for daily use side effects cialis black 800 to buy in the uk one pill free cialis online
i want to buy cialis in australia brand cialis no prescription cialis with dapoxetine from canada
viagra with priligy buy viagra australia online no prescription ggeneric viagra for sale
Hi there, this weekend is good in support of me, for the reason that this moment i am reading this impressive informative paragraph here at
my residence.
my web site – sex1taem
I enjoy what you guys are up too. This sort of clever work and exposure!
Keep up the superb works guys I’ve included you guys to my personal blogroll.
Feel free to visit my homepage: ladki
brand viagra viagra 10 pills ВЈ20 mustang viagra
Pretty part of content. I just stumbled upon your
blog and in accession capital to say that I acquire in fact enjoyed account your blog posts.
Anyway I’ll be subscribing to your augment and even I achievement
you get admission to constantly quickly.
Look into my blog … ki
Pretty! This was a really wonderful article. Many thanks for supplying these details.
Feel free to visit my homepage; hd1teen
Right now it looks like Movable Type is the top blogging
platform available right now. (from what I’ve read) Is that what you’re using on your
blog?
Feel free to visit my blog post :: sex
Wow! At last I got a weblog from where I know how to
in fact obtain useful information concerning my study and knowledge.
Here is my website – bangla
It is the best time to make some plans for the future and it is time
to be happy. I have read this post and if I could I want to suggest you some interesting things
or advice. Perhaps you can write next articles referring to this article.
I want to read more things about it!
Here is my web-site ladaki
Awesome article.
My site :: girl
I don’t know whether it’s just me or if everyone else encountering problems with your
website. It looks like some of the written text within your posts are
running off the screen. Can somebody else please comment and let me know if this is happening to them as well?
This could be a issue with my internet browser because I’ve had this
happen previously. Appreciate it
Stop by my homepage: bareback14
After checking out a few of the articles on your website, I seriously appreciate your technique of blogging.
I saved as a favorite it to my bookmark site list and will be
checking back soon. Take a look at my web site as well and let me know what you think.
Here is my website: ki
This is my first time go to see at here and i am truly impressed to read everthing at single place.
Here is my webpage; vdeo14
Oh my goodness! Amazing article dude! Thanks, However I am going through problems with
your RSS. I don’t understand why I can’t join it.
Is there anybody else having the same RSS problems?
Anyone who knows the solution will you kindly respond?
Thanks!!
My web site :: sex1staimsx1stb1stblad1stbrowser1stclasstube1stcom1stde1stdp1stim1stime1stime
Having read this I believed it was really enlightening.
I appreciate you finding the time and energy to put
this short article together. I once again find myself spending a lot of
time both reading and commenting. But so what, it was still worth it!
my blog post – 20
viagra shipping overnight can i buy viagra without a prescription where to buy generic viagra in canada
Keep on writing, great job!
Feel free to surf to my web site … xxx (zhel.city)
This is a really good tip especially to those new to
the blogosphere. Brief but very precise information… Thank you for
sharing this one. A must read article!
My webpage … xxx – ru.xhamster.com,
Quality articles or reviews is the important to invite the people to visit the website,
that’s what this web page is providing.
My web site; คาสิโน
canada rx pharmacy canadian pharmacy cialis mens ed pills
cheap rx cheap erectile dysfunction pills online rx pharmacy
online drugstore pharmacy http://pharmacy-onlineasxs.com/ best canadian online pharmacy
best drugstore primer best drugstore blush discount pharmacy
pharmacy express online drug store best drugstore primer