首页 文章

在python中使用aiohttp获取多个url

提问于
浏览
2

在之前的question中,用户建议使用 aiohttp 获取多个URL(API调用)的以下方法:

import asyncio
import aiohttp


url_list = ['https://api.pushshift.io/reddit/search/comment/?q=Nestle&size=30&after=1530396000&before=1530436000', 'https://api.pushshift.io/reddit/search/comment/?q=Nestle&size=30&after=1530436000&before=1530476000']

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.json()['data']


async def fetch_all(session, urls, loop):
    results = await asyncio.gather(*[loop.create_task(fetch(session, url)) for url in urls], return_exceptions= True)
    return results

if __name__=='__main__':
    loop = asyncio.get_event_loop()
    urls = url_list
    with aiohttp.ClientSession(loop=loop) as session:
        htmls = loop.run_until_complete(fetch_all(session, urls, loop))
    print(htmls)

但是,这会导致仅返回属性错误:

[AttributeError('__aexit__',), AttributeError('__aexit__',)]

(我启用了,其他它只会破坏) . 我真的希望这里有人,可以帮助解决这个问题,但仍然很难找到 asyncio 等资源 . 返回的数据是json格式 . 最后,我想把所有json dicts放在一个列表中 .

1 回答

  • 2

    工作范例:

    import asyncio
    import aiohttp
    import ssl
    
    url_list = ['https://api.pushshift.io/reddit/search/comment/?q=Nestle&size=30&after=1530396000&before=1530436000',
                'https://api.pushshift.io/reddit/search/comment/?q=Nestle&size=30&after=1530436000&before=1530476000']
    
    
    async def fetch(session, url):
        async with session.get(url, ssl=ssl.SSLContext()) as response:
            return await response.json()
    
    
    async def fetch_all(urls, loop):
        async with aiohttp.ClientSession(loop=loop) as session:
            results = await asyncio.gather(*[fetch(session, url) for url in urls], return_exceptions=True)
            return results
    
    
    if __name__ == '__main__':
        loop = asyncio.get_event_loop()
        urls = url_list
        htmls = loop.run_until_complete(fetch_all(urls, loop))
        print(htmls)
    

相关问题