aiohttp showing 403 error but requests.get giving 200 response

I'm using aiohttp to asynchronously retrieve a price from a url. Previously, I used requests.get for synchronous reading. I can successfully fetch the data with requests.get, but the same url gives a 403 error when I try to do it with aiohttp. I'm trying to figure out what the problem might be, but so far I'm not having any luck.

import aiohttp
import asyncio
import requests

url = 'https://www.olx.ua/uk/list/q-%D0%BA%D0%BE%D0%BF%D0%BE%D1%87%D0%BA%D0%B8-43/?search%5Bfilter_float_price:to%5D=1500'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36',
    'Accept-Encoding': 'gzip, deflate',
    'Accept': '*/*',
    'Connection': 'keep-alive',
}

resp = requests.get(url=url, headers=headers)
print(resp.status_code)
#Output 200

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers=headers) as response:
            print(response.status)

asyncio.run(main())
#Output 403

I expect to get 200 status in both cases

Upvotes: 0

Views: 35

Answers (0)

Related Questions