Python教程网
--左手Python,右手AI!

骚操作!用python一键清理收藏夹

用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 」

赞(23)
未经允许不得转载:Python教程网 » 骚操作!用python一键清理收藏夹
分享到: 更多 (0)

评论 62

  1. #1

    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

    匿名8个月前 (05-21)回复
  2. #2

    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

    匿名3个月前 (10-30)回复
  3. #3

    Treatment is divided into two phases. https://genqpviag.com do generic viagra pills work

    匿名3个月前 (11-05)回复
  4. #4

    COX-2 is also involved in thermoregulation and the pain response to injury.
    viagra online cheap price
    online generic viagra reviews

    匿名3个月前 (11-06)回复
  5. #5

    viagra for hair es necesario receta para la viagra boyfriend secretly taking viagra

    匿名2个月前 (11-16)回复
  6. #6

    order viagra online canada viagra professional review viagra

    匿名2个月前 (11-18)回复
  7. #7

    ejakulation bei viagra buybuyviamen.com lampara viagra

    匿名2个月前 (11-18)回复
  8. #8

    top-viagra-store.com purevigra.com does viagra keep you erect after ejaculation

    匿名2个月前 (11-19)回复
  9. #9

    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 – 더킹카지노

    匿名2个月前 (11-20)回复
  10. #10

    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 카지노사이트

    匿名2个月前 (11-20)回复
  11. #11

    generic viagra discount paradiseviagira.com viagra dapoxetin

    匿名2个月前 (11-20)回复
  12. #12

    viagra v6 can diabetics take viagra email virus link to viagra

    匿名2个月前 (11-20)回复
  13. #13

    Order viagra online canada mastercard https://mygoviagar.com/ cheap viagra pills free shipping

    匿名2个月前 (11-21)回复
  14. #14

    Viagra sale https://lightvigra.com viagra price in malaysia

    匿名2个月前 (11-21)回复
  15. #15

    Viagra india hopeviagrin.com otc generic viagra

    匿名2个月前 (11-21)回复
  16. #16

    Where to buy female viagra pill https://miraclevigra.com how to purchase viagra pills

    匿名2个月前 (11-21)回复
  17. #17

    viagra tablet online india generic viagra no prescription viagra online lowest price

    匿名2个月前 (11-21)回复
  18. #18

    buy viagra online mastercard viagra professional pfizer viagra pills no prescription

    匿名2个月前 (11-26)回复
  19. #19

    get viagra free samples cuando vence patente viagra cari viagra

    匿名2个月前 (11-26)回复
  20. #20

    vaigra female viagra gel viagra for female for sale

    匿名2个月前 (11-27)回复
  21. #21

    where can you buy viagra over the counter viagra generic india viagra 100mg pills generic

    匿名2个月前 (11-28)回复
  22. #22

    man overdosed viagra natural viagra around the house dia diem ban viagra

    匿名2个月前 (11-28)回复
  23. #23

    research paper zika virus research proposal psychology essays

    匿名2个月前 (11-29)回复
  24. #24

    a personal statement college essays for sale guidelines in writing an essay

    匿名2个月前 (11-29)回复
  25. #25

    how i can get viagra in norway supreme suppliers viagra viagra 50mg price

    匿名2个月前 (11-30)回复
  26. #26

    writing a thesis statement for research paper probability homework help order term paper

    匿名2个月前 (11-30)回复
  27. #27

    buy essay paper expository essay assignment maths homework help online

    匿名2个月前 (12-01)回复
  28. #28

    essay on helping the poor writing a research paper why do you want to go to college essay

    匿名2个月前 (12-01)回复
  29. #29

    Buy essay https://buyessayhelpbtg.com buy
    essays online reviews

    匿名2个月前 (12-01)回复
  30. #30

    do all colleges require essays assignments writing services chemistry homework help online

    匿名2个月前 (12-02)回复
  31. #31

    essay writing on child labour algebra 1 homework 5 cpm homework help

    匿名2个月前 (12-03)回复
  32. #32

    writing essays for fun historical research proposal writing resume cover letter

    匿名2个月前 (12-03)回复
  33. #33

    cialis tablets australia generic cialis india cialis

    匿名2个月前 (12-10)回复
  34. #34

    buy viagra super force with mastercard order viagra online australia buy viagra in canada

    匿名2个月前 (12-10)回复
  35. #35

    generic viagra australia buy viagra without a perscription brand name viagra

    匿名2个月前 (12-11)回复
  36. #36

    viagra professional viagra cialis viagra mit pay pal bezahlen

    匿名1个月前 (12-12)回复
  37. #37

    cialis for daily use side effects cialis black 800 to buy in the uk one pill free cialis online

    匿名1个月前 (12-13)回复
  38. #38

    i want to buy cialis in australia brand cialis no prescription cialis with dapoxetine from canada

    匿名1个月前 (12-13)回复
  39. #39

    viagra with priligy buy viagra australia online no prescription ggeneric viagra for sale

    匿名1个月前 (12-14)回复
  40. #40

    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

    匿名1个月前 (12-15)回复
  41. #41

    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

    匿名1个月前 (12-15)回复
  42. #42

    brand viagra viagra 10 pills ВЈ20 mustang viagra

    匿名1个月前 (12-16)回复
  43. #43

    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

    匿名1个月前 (12-16)回复
  44. #44

    Pretty! This was a really wonderful article. Many thanks for supplying these details.

    Feel free to visit my homepage; hd1teen

    匿名1个月前 (12-16)回复
  45. #45

    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

    匿名1个月前 (12-16)回复
  46. #46

    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

    匿名1个月前 (12-16)回复
  47. #47

    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

    匿名1个月前 (12-16)回复
  48. #48

    Awesome article.

    My site :: girl

    匿名1个月前 (12-16)回复
  49. #49

    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

    匿名1个月前 (12-16)回复
  50. #50

    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

    匿名1个月前 (12-16)回复
  51. #51

    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

    匿名1个月前 (12-16)回复
  52. #52

    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

    匿名1个月前 (12-16)回复
  53. #53

    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

    匿名1个月前 (12-16)回复
  54. #54

    viagra shipping overnight can i buy viagra without a prescription where to buy generic viagra in canada

    匿名1个月前 (12-18)回复
  55. #55

    Keep on writing, great job!

    Feel free to surf to my web site … xxx (zhel.city)

    匿名1个月前 (12-22)回复
  56. #56

    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,

    匿名1个月前 (12-22)回复
  57. #57

    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; คาสิโน

    匿名4周前 (12-27)回复
  58. #58

    canada rx pharmacy canadian pharmacy cialis mens ed pills

    匿名2周前 (01-09)回复
  59. #59

    cheap rx cheap erectile dysfunction pills online rx pharmacy

    匿名2周前 (01-09)回复
  60. #60

    online drugstore pharmacy http://pharmacy-onlineasxs.com/ best canadian online pharmacy

    匿名2周前 (01-10)回复
  61. #61

    best drugstore primer best drugstore blush discount pharmacy

    匿名2周前 (01-11)回复
  62. #62

    pharmacy express online drug store best drugstore primer

    匿名2周前 (01-13)回复