3

I am trying to open the Instagram URL using urllib.request.urlopen(url).read() but I am getting the error urllib.error.HTTPError: HTTP Error 502: Bad Gateway

username = input('enter the username - ')
url = "https://www.instagram.com/{}".format(username)
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, 'html.parser')

How can I solve this issue?

EDIT: I got the solution and I have posted it. Cheers :)

4
  • What are you entering for username? Commented Oct 15, 2019 at 14:28
  • I am entering a string, typically a valid Instagram username Commented Oct 15, 2019 at 16:12
  • @DebdutGoswami, hey did you find a solution to this? If yes please can you share that, we are facing the same issue... Commented Nov 20, 2019 at 10:20
  • @SatyenUdeshi check my answer Commented Nov 28, 2019 at 11:40

1 Answer 1

2

First, add
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
then type
html = urllib.request.urlopen(req).read()
This will solve the problem. Previously it couldn't verify the origin of the request. We tricked it into thinking that the request is made from a genuine browser, i.e. Mozilla.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.