温馨提示:
1. 基于aiohttp的代码样例支持访问http,https网页
2. aiohttp不是python原生库,需要安装才能使用: pip install aiohttp, pip install aiohttp-socks0
3. aiohttp只支持Python3.5及以上
4. 如果在Windows系统使用aiohttp访问https网站时抛出异常,可以在import asyncio后调用 asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())来解决。
参考样例
import aiohttp import aiohttp_socks import asyncio async def fetch(url, proxy): # 设置代理连接器 connector = aiohttp_socks.ProxyConnector.from_url(proxy) async with aiohttp.ClientSession(connector=connector) as session: async with session.get(url) as response: print(f"Status: {response.status}") print(await response.text()) # 若是用户名密码授权,需要带上user和password,若是终端IP授权则不需要 proxy_url = "socks5://user:password@ip:port" # 抓取目标的URL url_to_fetch = "http://example.com" asyncio.run(fetch(url_to_fetch, proxy_url))