Check If User is Verified on Instagram using Python?

If you have crashed here then certainly you are not able to get the information whether user profile is verified or not on Instagram using code.

After spending a lot of time in exploring Instagram graph API, I got to know that I cant get this information using their official API. I wish someone told this to me sooner. You can get only limited information related to an user. Check here for more information.

If you need to get this information for a user, then you need to fetch data from below url-

"https://www.instagram.com/{instagram_username}/?__a=1"

For example, If you need to check whether user profile ‘enriqueiglesias’ is verified or not, you need to pass like this-

https://www.instagram.com/enriqueiglesias/?__a=1

If you will open this url in browser, you will see json structure like this-

Now, If you are a decent observer, then you can see the is_verified key in bottom left corner of above image. You can test the value of this key with other non verified usernames and verified usernames also.

Get url data and JSON parsing

Next step would be to get the contents of the url and then parse the JSON to get the is_verified key. I am using python language to perform this task.

  • Firstly, you need to set the base url keeping username configurable, which would look something like below.
base_url = 'https://www.instagram.com/'+ ig_username + '/' + '?__a=1'
  • I am using urllib package to get the url content. Refer the official documentation for more details. Now comes a very important step which is to provide headers while we are making request to this url. It is important because if you don’t provide the correct headers, then it redirects you to a login page or in some case it throws HTTP error 429 – Too Many Requests when hitting Instagram.
  • Now how to get the correct headers for the url? This is very simple. Follow below steps-
    1. Right Click and do ‘Inspect’ on the page.
    2. Go to ‘Network’ section.
    3. Go to ‘All’ section and reload the page.
    4. Click on the document in ‘Name’ section.
    5. From ‘Request Headers’ section, grab the values of ‘accept’ and ‘user-agent’.

Now, we have all the required things. Lets use them in the code-

from urllib.request import urlopen, Request
ig_username = 'zara.outfits'
base_url = 'https://www.instagram.com/'+ ig_username + '/' + '?__a=1'

hdr = {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
       "User-Agent": "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Mobile Safari/537.36"}

my_request = Request(base_url, None, headers=hdr)
page_source = urlopen(my_request, timeout=10).read().decode('utf-8')

source = json.loads(page_source)
username = source['graphql']['user']['username']

if username == ig_username:
   is_verified = source['graphql']['user']['is_verified']
   print(f'User profile {username} is verified - {is_verified}')          

Below output can be seen by running above script-

Output

Please note – This solution will work providing Instagram does not amend the JSON structure or meta tags. This solution is developed as Instagram API presently doesn’t offer this info. Best would be if they update their API.

Conclusion

Let me know in comments in case you face any problem using this solution. I will be happy to help.

Happy Learning!

Advertisement

4 thoughts on “Check If User is Verified on Instagram using Python?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s