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
在图片显示为42行的我用的是3.9的python
少了一个对应括号