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())

  

上一篇:如何避免被大量的JavaScript框架和新版本所淹没?


下一篇:asyncio和aiohttp携程并发