import aiohttp
async def download():
pass
async def main():
url = 'https://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"4306063500"}'
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/'
'100.0.4896.60 Safari/537.36 Edg/100.0.1185.29',
'referer': 'https: // cn.bing.com /'
}
async with aiohttp.ClientSession() as session:
async with session.get(url=url, head=head) as resp:
dic = await resp.json()
print(dic)
if __name__ == '__main__':
main()
如果你报错TypeError: An asyncio.Future, a coroutine or an awaitable is required
这个,这个你就搜搜可以解决,下面已经可以提取你想要的了(可能你spider缺点东西会出现这个报错)
import aiohttp
import asyncio
async def download():
pass
async def main():
url = 'https://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"4306063500"}'
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/'
'100.0.4896.60 Safari/537.36 Edg/100.0.1185.29',
'referer': 'https: // cn.bing.com /'
}
print(head)
async with aiohttp.ClientSession() as session:
async with session.get(url=url) as resp:
dic = await resp.json()
print(dic)
if __name__ == '__main__':
asyncio.run(main(), debug=True)
还有,能给我发发网址你从哪看到 async with session.get(url=url) as resp:这一步里面有head的么,我看了看源码是没有这个参数的,但是里面有个**kwargs