Created
February 19, 2024 03:50
-
-
Save thehappycheese/7254c35b517ba58f983157cf3b39cdfa to your computer and use it in GitHub Desktop.
pandas_aoihttp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import asyncio | |
from aiohttp import ClientSession, TCPConnector | |
@pd.api.extensions.register_series_accessor("aiohttp") | |
class AiohttpAccessor: | |
def __init__(self, pandas_obj): | |
self._obj = pandas_obj | |
async def _fetch_text(self, url: str, session): | |
async with session.get(url) as response: | |
return await response.text() | |
async def get(self, limit:int=100,ssl: bool=True): | |
async with ClientSession(connector=TCPConnector(limit=limit, ssl=ssl)) as session: | |
work = [self._fetch_text(url, session) for url in self._obj] | |
responses = await asyncio.gather(*work) | |
return pd.Series(responses) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment