zoukankan      html  css  js  c++  java
  • Python下载漫画

    上午起来提不起劲,于是就用电脑看漫画,但是在线看漫画好烦,就想下下来看。一个一个点太麻烦,于是花了点时间用python写了个demo,把爱漫画的漫画下载下来,这样就可以随时随地看了。这也是我首次尝试用python写不是数据处理的东西,还是很开心的。
          做的具体思路就是利用python提供的urllib,urllib2的内容来做的,其中辅助了正则表达式模块re,用来进行匹配。因为自己水平不咋地,代码有点乱。。
    代码:

    #!/usr/bin/env python
    #-*- coding: utf-8 -*-

    import urllib as ub1
    import urllib2 as ub2
    import re

    def downloadCartoon(bookurl,localdir):
        req = ub2.Request(bookurl)
        f = ub2.urlopen(req)
        for eachline in f:
            line = eachline.strip()     
            if re.match('.*下载.*',line):
                wordList = line.split('"')
                downloadurl='http://www.bbhou.com/'+wordList[1]
                req1 = ub2.Request(downloadurl)#第2个是下载链接的页面URL地址,构造这个request,然后再次请求一个网页
                #跳转到下载的页面,再次请求并进行下载
                f1 = ub2.urlopen(req1)
                for item in f1:
                    item2 = item.strip()               
                    if re.match('.*zip',item2):
                        wl = item2.split('"')   
                        localname = localdir+wl[3]+'.zip'                           
                        ub1.urlretrieve(wl[1],localname.decode('utf-8'))#利用下载链接,下载到本地
                        print localname.decode('utf-8')+' downloaded.'               

    def GetCartoon(url):
        #这里是漫画所有的列表,区域,这里循环获得每一卷的地址,然后调用上一个函数去进行下载
        rooturl = 'http://www.bbhou.com/' #这是漫画地址的根目录,后面的连接需要根据这个进行跳转
        request = ub2.Request(url)
        response = ub2.urlopen(request)   
        lsturl = []
        for every in response:
            line = every.strip()
            if re.match('.*/manhua/.*html.*',line):
                piclist = line.split('"')
                for item in piclist:
                    if re.match('.*html.*',item):
                        cururl = rooturl+item
                        lsturl.append(cururl)
        for url in lsturl:
            try:           
                downloadCartoon(url,'E:\')
            except Exception,e:
                continue   
               
    GetCartoon('http://www.bbhou.com/manhua/jinjidejuren/')

  • 相关阅读:
    Ehcache(04)——设置缓存的大小
    Ehcache(03)——Ehcache中储存缓存的方式
    Ehcache(02)——ehcache.xml简介
    Ehcache(01)——简介、基本操作
    linux 下开机自动启动tomcat服务
    Git_常用链接
    Android_listview点击失效
    Android_Drawable Bitmap Canvas Paint之间区别[转]
    Android_广播
    Notification(Notification的通知栏常驻、Notification的各种样式、Notification点击无效)
  • 原文地址:https://www.cnblogs.com/ahnucao/p/4938741.html
Copyright © 2011-2022 走看看