温馨提示:
1. aiohttp的代码样例支持访问http,https网页
2. aiohttp不是python原生
3. 如Windows系统使用aiohttp访问https网站抛出异常,在import asyncio后调asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())即可解决
4. aiohttp只支持Python3.5及以上
参考样例
import random import asyncio import aiohttp import requests page_url="http://example.com"#要访问的目标网页 #用户名密码授权 username="username" password="password" proxy_auth=aiohttp.BasicAuth(username, password) async def fetch(url): async with aiohttp.ClientSession() as session: async with session.get(url,proxy="http://"+random.choice(proxy_list),proxy_auth=proxy_auth) as resp: content=await resp.read() print(f"status_code: {resp.status},content:{content}") def run(): loop=asyncio.get_event_loop() #异步发出5次请求 tasks=[fetch(page_url) for_in range(5)] loop.run_until_complete(asyncio.wait(tasks)) if __name__=='__main__': run()