Python 3 Instagram API User Profile Details Scraper or Grabber Command Line Script Using Requests Library Full Project For Beginners

Python 3 Instagram API User Profile Details Scraper or Grabber Command Line Script Using Requests Library Full Project For Beginners

 

Welcome folks today in this blog post we will be scraping instagram user details inside command line prompt in python. All the full source code of the application will be shown below.

 

 

 

Get Started

 

 

In order to get started you need to create an app.py file and copy paste the following code

 

app.py

 

 

import requests
from lxml import html
import re
import sys


def main(username):
    '''main function accept instagram username
    return an dictionary object containging profile deatils
    '''
    url = "https://www.instagram.com/{}/?hl=en".format(username)
    page = requests.get(url)
    tree = html.fromstring(page.content)
    data = tree.xpath('//meta[starts-with(@name,"description")]/@content')

    if data:
        data = tree.xpath('//meta[starts-with(@name,"description")]/@content')
        data = data[0].split(', ')
        followers = data[0][:-9].strip()
        following = data[1][:-9].strip()
        posts = re.findall(r'\d+[,]*', data[2])[0]
        name = re.findall(r'name":"\w*[\s]+\w*"', page.text)[-1][7:-1]
        aboutinfo = re.findall(r'"description":"([^"]+)"', page.text)[0]
        instagram_profile = {
            'success': True,
            'profile': {
                'name': name,
                'profileurl': url,
                'username': username,
                'followers': followers,
                'following': following,
                'posts': posts,
                'aboutinfo': aboutinfo
            }
        }
    else:
        instagram_profile = {
            'success': False,
            'profile': {}
        }
    return instagram_profile


#  python InstgramProfile.py username
if __name__ == "__main__":
    '''driver code'''

    if len(sys.argv) == 2:
        output = main(sys.argv[-1])
        print(output)
    else:
        print('=========>Invalid paramaters Valid Command is<=========== \
        \npython InstagramProfile.py username')

 

See also  Join Flask Python Whatsapp Group Link For Flask Python Developers in 2021 | Flask Whatsapp Group Links For Python Developers | Whatsapp Group Links For Flask Python Programmers

 

Now in order to run this application you need to type the below command and here also you need to provide an additional command line argument which is the instagram username as shown below

 

python app.py harshad_chopda

 

where harshad_chopda is the actual username of the instagram for which you are scraping user profile details if you execute it you will see the following information as shown below

 

 

 

Leave a Reply