qq_45373654
2022-08-06 16:22
采纳率: 0%
浏览 22

python语法问题wait

我想要达到的结果
import asyncio
import json

import aiofiles  # 用于异步下载
import aiohttp  # 异步登录地址
import requests


async def getChapterContent(cid,book_id,title):
    data = {"book_id":cid,
            "cid":f"{cid}|{book_id}",
            "need_bookinfo":1
    }
    data = json.dumps(data)   #转化为列表形式
    url = f'https://dushu.baidu.com/api/pc/getChapterContent?data={data}'

    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            dic = await resp.json()
            print(dic['data']['novel']['content'])
            async with aiofiles.open('shuju/'+title,mode='w') as f:
                await f.write(dic['data']['novel']['content'])



async def getCatalog(url):
    resp = requests.get(url)
    # print(resp.json())
    dic = resp.json()

    tasks = []

    for item in dic['data']['novel']['items']:
        title = item['title']
        cid = item['cid']
        tasks.append(getChapterContent(asyncio.create_task(cid),
                                       asyncio.create_task(book_id),
                                       asyncio.create_task(title)
                                       )


    await asyncio.wait(tasks)


if __name__ == '__main__':
    book_id = '4306063500'
    url = 'https://dushu.baidu.com/api/pc/getCatalog?data={"book_id": "' +book_id+ '"}'
    asyncio.run(getCatalog(url))

为什么会报错出现line 50
await asyncio.wait(tasks)
^
SyntaxError: invalid syntax

3条回答 默认 最新

相关推荐 更多相似问题