计算机系菜鸟一个..  初次开独立博客,请多多指教!


#!/usr/bin/env python3
# coding: utf-8
 
import asyncio
import json
import tqdm
import aiohttp

url = 'http://cs.sosohaha.com/request.php'
#url = 'http://csbk.sosohaha.com/request.php'
#url = 'http://cs.bukamanhua.com/request.php'
sem = asyncio.Semaphore(3)
database = []

@asyncio.coroutine
def requesturl(i, t=0):
    micdata = {
        "f"   : "func_getdetail",
        "mid" : i
    }
    postdata = {
        'i': json.dumps(micdata),
        'c': 'C13D889A44A978B7A71B922F5A81334A',
        'v': '9'
    }
    with (yield from sem):
        flag = True
        while flag:
            try:
                rep = yield from aiohttp.request('post', url, data=postdata)
                reptext = yield from rep.text()
                repjson = json.loads(reptext)
                if repjson.get('name'):
                    comicname = repjson['name']
                    comicauthor = repjson['author']
                    comicinfo = [i, comicname, comicauthor]
                    database.append(comicinfo)
                flag = False
            except Exception as e:
                print('Exception: ', i)
                flag = True

@asyncio.coroutine
def wait_with_progress(coros):
    for f in tqdm.tqdm(asyncio.as_completed(coros), total=len(coros)):
        yield from f

def main():
    loop = asyncio.get_event_loop()
    pool = wait_with_progress([requesturl(i) for i in range(1, 300000)])
    loop.run_until_complete(pool)
    loop.close()

    database.sort()
    with open('index1.txt', 'a', encoding='utf-8') as index:
        for item in database:
            index.write('{0:<8}  {1:<30}  {2}\n'.format(item[0], item[1], item[2]))

if __name__ == '__main__':
    main()