def writePicture(url, filepath, sleep=0.2, headers={}): time_start = time.time() with open(filepath, "wb") as jpg: jpg.write(requests.get(url, headers=headers).content) time_end = time.time() time.sleep(sleep) mylogger.info(filepath, time_end - time_start, 's')
def downloadPicture(url, filepath, sleep=0.2, headers={}): if os.path.exists(filepath): mylogger.info('skip', filepath) return try: writePicture(url, filepath, sleep, headers) except Exception as e: os.remove(filepath) mylogger.error('error skip', filepath)
def application(environ, set_response_header): set_response_header('200 OK', [('Content-Type', 'text/html;charset=utf-8')]) file_name = environ['PATH_INFO'] # if file_name == '/index.py': # return index() # elif file_name == '/center.py': # return center() # else: # return '<h1>我爱你中国!</h1>' mylogger.info("访问的是%s" % file_name) try: for url, func in URL_FUNC_DICT.items(): ret = re.match(url, file_name) if ret: return func(ret) else: mylogger.warning("请求的url(%s)没有对应的函数...." % file_name) return "请求的url(%s)没有对应的函数...." % file_name except Exception as ret: mylogger.error("产生了异常: %s" % str(ret)) return "产生了异常: %s" % str(ret)
from retrying import retry from lxml import html import zipfile import base64 import json import time import sys import os import requests import urllib import utils import mylogger if __name__ == '__main__': urlFmt = 'https://i.hamreus.com/ps3/y/yiquanchaoren/vol_19/{0:03d}.jpg.webp?cid=465855&md5=p8imqiLi0DQPzWGC-oKKcg' referer = 'https://www.manhuagui.com/comic/7580/465855.html' downloadDir = 'D:\\manga\\OnePunch\\19' if not os.path.exists(downloadDir): os.makedirs(downloadDir) for i in range(1, 228): url = urlFmt.format(i) mylogger.info(url) path = os.path.join(downloadDir, '{}.jpg'.format(i)) utils.downloadPicture(url, path, headers={'Referer': referer}) utils.compressZip(downloadDir, downloadDir + '.zip')