其他分享
首页 > 其他分享> > 网络漫画转换器代码

网络漫画转换器代码

作者:互联网

import os
import requests
import xml.etree.ElementTree as ET

BookName = "output"
ChapterNum = 21
MaxPages = 100


def download_chapter(path,index):
    for i in range(MaxPages):
        if i+1<10:
            link="https://images.dmzj.com/d/%E4%B8%9C%E6%96%B9%E5%84%9A%E6%9C%88%E6%8A%84/{}0{}.jpg".format(path,i+1)
        else:
            link = "https://images.dmzj.com/d/%E4%B8%9C%E6%96%B9%E5%84%9A%E6%9C%88%E6%8A%84/{}{}.jpg".format(path,i + 1)
        try:
            pic = requests.get(link)
            if pic.status_code == 200:
                with open(os.path.join(str(index)) + os.sep + "%d.png"%(i+1), 'wb') as fp:
                    fp.write(pic.content)
                    fp.close()
            else:
                print("download", path,i+1)
                return
        except Exception as e:
            print(e)


def download():
    for i in range(1, ChapterNum+1):
        if not os.path.exists(str(i)):
            os.mkdir(str(i))
        if i<10:
            path="CH0{}/0".format(i)
        elif i<20:
            path = "CH{}/0".format(i)
        elif i==20:
            path ="第20话/第20话_00"
        else:
            path = "第21话%28完%29/第21话%28完%29_00"
        download_chapter(path,i)


def __indent(elem, level=0):
    i = "\n" + level*"\t"
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "\t"
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            __indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i


def create_book():
    content = ET.parse("page.htm")
    rt = content.getroot()
    for i in range(1,ChapterNum+1):
        for j in range(1,MaxPages):
            path = "Images\\{}\\{}.png".format(i,j)
            #print("release\\OEBPS\\" + path)
            if not os.path.exists(path): break
            rt[1].append(ET.Element("img",{"src": path}))
    __indent(rt)
    content.write(BookName + ".htm", encoding='utf-8')


download()
create_book()

  

标签:代码,ChapterNum,download,import,漫画,转换器,path,ElementTree,MaxPages
来源: https://www.cnblogs.com/linzhuohang/p/16684568.html