以下是官网原话:
Make a Request
Begin by importing the aiohttp module:
import aiohttp
Now, let’s try to get a web-page. For example let’s query http://httpbin.org/get
:
async with aiohttp.ClientSession() as session:
async with session.get('http://httpbin.org/get') as resp:
print(resp.status)
print(await resp.text())
然后,我这里报错
>>> async with aiohttp.ClientSession() as session:
File "<stdin>", line 1
async with aiohttp.ClientSession() as session:
^
SyntaxError: invalid syntax
一脸懵逼的自己手敲了一遍,结果还是这样。
搜索发现这篇文章,给出了解决方案:
https://www.v2ex.com/amp/t/339447
import asyncio import aiohttp async def fetch(): async with aiohttp.ClientSession() as session: async with session.get('https://api.github.com/events') as r: print(r.status) print(await r.text()) loop = asyncio.get_event_loop() loop.run_until_complete(fetch()) loop.close()
最那啥的官方文档,多学习人家requests的官方文档。