【问题标题】:Fetching multiple urls with aiohttp in python在python中使用aiohttp获取多个url
【发布时间】:2019-01-14 11:57:24
【问题描述】:

在之前的 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 字典放在一个列表中。

【问题讨论】:

    标签: python-3.x api web-scraping python-asyncio aiohttp


    【解决方案1】:

    工作示例:

    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)
    

    【讨论】:

    • async with aiohhtp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False))不要再哭了
    • @YuriiKramarenko 不幸的是,这段代码对我不起作用,它会抛出这个错误:link
    • @Jannik 看起来很奇怪,你用的是哪个版本的 aiohttp\python?
    • @YuriiKramarenko 我正在使用 aiohttp==0.7.2、asyncio 3.4.3 和 python 3.6
    • @Jannik 尝试将您的 aiohttp 版本更新到 3.3.2(最新),因为您的版本非常旧。
    猜你喜欢
    • 2016-06-23
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    相关资源
    最近更新 更多