Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to connect to a url with python request module

I’m unable to connect to the URL using requests module, but it works fine when browsed in a browser. Could it be some robots.txt issue Allowed/Disallowed issue ?

Below is the codebase.

import requests

r = requests.get('https://myntra.com')

print(r)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Some websites block access from non-web browser ‘User-Agents’ to prevent web scraping, including from the default Python’s requests ‘User-Agent’.

so you need to pass a user agent as like a web browser, for example:

r = requests.get('https://myntra.com/', headers = {
        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0",
    },)

The ‘User-Agent’ string contains information about which browser is being used, what version and on which operating system.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading