Skip to content Skip to sidebar Skip to footer

Asyncio/aiohttp Not Returning Response

I am trying to scrape some data from https://www.officialcharts.com/ by parallelising web requests using asyncio/aiohttp. I implemented the code given at the link here. I followed

Solution 1:

Try to use the latest version.

#!/usr/bin/env python3# -*- coding: utf-8 -*-from aiohttp import ClientSession, client_exceptions
from asyncio import Semaphore, ensure_future, gather, run
from json import dumps, loads

limit = 10
http_ok = [200]


asyncdefscrape(url_list):
    tasks = list()
    sem = Semaphore(limit)

    asyncwith ClientSession() as session:
        for url in url_list:
            task = ensure_future(scrape_bounded(url, sem, session))
            tasks.append(task)

        result = await gather(*tasks)

    return result


asyncdefscrape_bounded(url, sem, session):
    asyncwith sem:
        returnawait scrape_one(url, session)


asyncdefscrape_one(url, session):
    try:
        asyncwith session.get(url) as response:
            content = await response.read()
    except client_exceptions.ClientConnectorError:
        print('Scraping %s failed due to the connection problem', url)
        returnFalseif response.status notin http_ok:
        print('Scraping%s failed due to the return code %s', url, response.status)
        returnFalse

    content = loads(content.decode('UTF-8'))

    return content

if __name__ == '__main__':
    urls = ['http://demin.co/echo1/', 'http://demin.co/echo2/']
    res = run(scrape(urls))

    print(dumps(res, indent=4))

This is a template of a real project that works as predicted.

You can find this source code here

Post a Comment for "Asyncio/aiohttp Not Returning Response"