欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)爬蟲下載漫畫示例

 更新時(shí)間:2014年02月16日 15:30:59   作者:  
本文主要介紹了python實(shí)現(xiàn)爬蟲下載漫畫的示例,對(duì)游俠網(wǎng)的漫畫資源進(jìn)行解析,下載其全部章節(jié)漫畫

復(fù)制代碼 代碼如下:

#!/usr/bin/python3.2
import os,socket
import urllib
import urllib.request,threading,time
import re,sys
global manhuaweb,weburl,floder,chapterbegin,currentthreadnum,threadcount,mutex,mutex2

weburl=''
floder=''
chapterbegin=0
currentthreadnum=0
threadcount=6


if len(sys.argv)>=3:
  weburl=sys.argv[1]
  floder=sys.argv[2]
else:
    print("usag: downloadmanhua weburl floder chapterbegin=0 threadnnum=6")
    sys.exit(0)
if len(sys.argv)>=4:
  chapterbegin=int(sys.argv[3])
if len(sys.argv)>=5:
  threadcount=(int)(sys.argv[4])

 

def jin(i,jinzhi):
        finalans=""
        answer=i%jinzhi
        i=int(i/jinzhi)
        if answer>9:
                finalans=finalans+chr(ord('a')+(answer-10))
        else:
                finalans=finalans+str(answer)
        if i!=0:
                finalans=jin(i,jinzhi)+finalans
        return finalans
def urlparse(p,a,c,k):
        d={}
        e=lambda c:     jin(c,36)
        if 1:
                while c:
                        c=c-1
                        if not k[c]:
                                d[jin(c,36)]=jin(c,36)
                        else:
                                d[jin(c,36)]=k[c]
                k=[lambda e:d[e]]
                e=lambda c:'\\w+'
                c=1
        newstr=""
        while c:
                c=c-1
                if k[c]:
                        for i in range(0,len(p)):
                                tempi=p[i]
                                tempi=ord(tempi)
                                if tempi>=ord('a') and tempi<=ord('f'):
                                        newstr+=d[chr(tempi)]
                                elif tempi>=ord('0') and tempi<=ord('9'):
                                        newstr+=d[chr(tempi)]
                                else:
                                        newstr+=chr(tempi)
        return newstr
def meispower(s):
        p=re.compile(r"(?=\}\().*",re.IGNORECASE)
        s=p.findall(s)
        s=s[0]
        s=s[0:(len(s)-19)]
        par=s.split(',')
        par[3]=par[3][1:len(par[3])]
        answer=par[3].split('|')
        chapterpath=urlparse(par[0],int(par[1]),int(par[2]),answer)
        allurl=re.findall('imgpath=[^;]*',chapterpath)[0]
        allurl=allurl[10:(len(allurl)-2)]
        return allurl
def pictofile(weburl,filename,loop=100):
        if loop<0:
                print('can\'t download the picture %s'%weburl)
                return
        loop=loop-1
        if os.path.exists(filename):
            return
        try:
                url=urllib.request.urlopen(weburl)
                data=url.read()
                if len(data)<2048:
                        url.close()
                        pictofile(weburl,filename,loop)
                else:
                        print('download from %s name is %s\n'%(weburl,filename))
                        myfile=open('%s'%filename,'wb')
                        myfile.write(data)
                        myfile.close()
                        url.close();
        except socket.timeout:
                print('timeout')
                pictofile(weburl,filename,loop)
        except Exception as e:
          print('error',e)
          pictofile(weburl,filename,loop)
        finally:
            pass
def downloadpic(url,loadpicdir,num):
    #download the all url picture to loadpicdir
    global currentthreadnum,mutex,mutex2
    mymode=re.compile(r'[0-9a-z.]*\Z')
    try:
                mutex2.acquire()
                os.chdir(loadpicdir)
                mutex2.release()
    except:
                print("can't open the floder %s will be create"%loadpicdir)
                try:
                    if(mutex2.locked()):
                        os.mkdir(loadpicdir)
                        os.chdir(loadpicdir)
                        mutex2.release()
                    print('create floder succeed')
                except:
                    print("can't create floder %s"%loadpicdir)
                    if(mutex.acquire()):
                        mutex.release()
                    quit(0)
    name=mymode.findall(url)
    filename='manhua'+name[0]
    pictofile(url,loadpicdir+'//'+str(num)+'-'+filename)
    mutex.acquire()
    currentthreadnum=currentthreadnum-1
    mutex.release()
def downloadchapter(url,loadpicdir,num,begin=0):
        global manhuaweb,threadcount,currentthreadnum,mutex
        print(manhuaweb+url)
        webdata=urllib.request.urlopen(manhuaweb+url).read()
        webdata=webdata.decode('UTF-8')
        chaptername=re.findall(r'<title>[^_]*',webdata)[0]
        chaptername=chaptername[7:len(chaptername)]
        webscrip=re.findall(r'eval.*[^<>]',webdata)
        chapterurl=meispower(webscrip[0]);
        chapterurl='http://mhimg.ali213.net'+chapterurl
        for i in range(begin,num):
                try:
                        while(currentthreadnum>=threadcount):
                                time.sleep(0.5)
                        mutex.acquire()
                        currentthreadnum=currentthreadnum+1
                        mutex.release()
                        threading.Thread(target=downloadpic,args=(r'%s%d.jpg'%(chapterurl,i),loadpicdir+chaptername,num)).start()
                except socket.error:
                        mutex.acquire()
                        i=i-1
                        currentthreadnum=currentthreadnum-1
                        mutex.release()
                except Exception as error:
                        print(error,'break')
                        print('download chapter %d of picture make a error'%i)
                        break
if __name__=='__main__':
        manhuaweb=r'http://manhua.ali213.net'
        socket.setdefaulttimeout(60.0)
        mutex=threading.Lock()
        mutex2=threading.Lock()

       
        webfile=urllib.request.urlopen(weburl)
        webdata=webfile.read();
        webdata=webdata.decode('UTF-8')
        meshmode=re.compile(r'<div class="detail_body_right_sec_con">.*</div>')
        meshdata=meshmode.findall(webdata)[0]
        indexmode=re.compile(r'([0-9]*頁)')
        indexdata=indexmode.findall(meshdata)

        picurlmode=re.compile(r'/comic/[0-9/]*.html')
        picurldata=picurlmode.findall(meshdata)


        chapterlength=len(picurldata)
        nummode=re.compile(r'[\d]+')

        i=chapterbegin
        while i<chapterlength:
                manhuachapter=picurldata[chapterlength-i-1]
                downloadchapter(manhuachapter,floder,int(nummode.findall(indexdata[chapterlength-i-1])[0]))
                i=i+1

相關(guān)文章

  • python語法之通過value找key問題

    python語法之通過value找key問題

    這篇文章主要介紹了python語法之通過value找key問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python實(shí)現(xiàn)吃蘋果小游戲

    python實(shí)現(xiàn)吃蘋果小游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)吃蘋果小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • windows上安裝python3教程以及環(huán)境變量配置詳解

    windows上安裝python3教程以及環(huán)境變量配置詳解

    這篇文章主要介紹了windows上安裝python3教程以及環(huán)境變量配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python單鏈表原理與實(shí)現(xiàn)方法詳解

    Python單鏈表原理與實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Python單鏈表原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Python單鏈表的具體概念、原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • Java多線程編程中ThreadLocal類的用法及深入

    Java多線程編程中ThreadLocal類的用法及深入

    這篇文章主要介紹了Java多線程編程中ThreadLocal類的用法及深入,嘗試了自己實(shí)現(xiàn)一個(gè)ThreadLocal類以及對(duì)相關(guān)的線程安全問題進(jìn)行討論,需要的朋友可以參考下
    2016-06-06
  • Python實(shí)現(xiàn)的讀取/更改/寫入xml文件操作示例

    Python實(shí)現(xiàn)的讀取/更改/寫入xml文件操作示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的讀取/更改/寫入xml文件操作,涉及Python針對(duì)xml文件的讀取、節(jié)點(diǎn)操作、寫入等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-08-08
  • Python的進(jìn)程及進(jìn)程池詳解

    Python的進(jìn)程及進(jìn)程池詳解

    這篇文章主要為大家介紹了Python的進(jìn)程及進(jìn)程池,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • Python實(shí)現(xiàn)提前查詢考研成績功能

    Python實(shí)現(xiàn)提前查詢考研成績功能

    這篇文章主要介紹了Python實(shí)現(xiàn)提前查詢考研成績,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • Python讀取excel中的圖片完美解決方法

    Python讀取excel中的圖片完美解決方法

    excel中的圖片非常常見,但是通過python讀取excel中的圖片沒有很好的解決辦法。今天小編給大家分享一種比較聰明的方法,感興趣的朋友跟隨腳本之家小編看看吧
    2018-07-07
  • python查看FTP是否能連接成功的方法

    python查看FTP是否能連接成功的方法

    這篇文章主要介紹了python查看FTP是否能連接成功的方法,實(shí)例分析了Python連接FTP的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07

最新評(píng)論