aiohttp使用方法

import aiohttp
import asyncio


async def request_jd_comment(session):
    url = "http://httpbin.org/ip"
    proxy = "http://14.67.8.20:2045"  # 这里的代理参数是proxy,内容是一个str,例如("http://14.67.8.20:2045");requests请求的代理参数是proxies,内容是一个字典,例如{"http": "http://14.67.8.20:2045", "https": "http://14.67.8.20:2045"}
    async with session.get(url, proxy=proxy) as response:
        return await response.text()


async def crawl():
    async with aiohttp.ClientSession() as session:
        result_content = await request_jd_comment(session)
        return result_content


async def main():
    result = await crawl()
    print(result)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

  

原文地址:https://www.cnblogs.com/qiaoer1993/p/14062824.html