Oh Snap!

Please turnoff your ad blocking mode for viewing your site content

Hit enter after type your search item

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

/
/
/
49 Views

 

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')

 

READ  Python 3 Script to Extract Text From Image Using pytesseract and Pillow Library | Python Optical Character Recognition (OCR) Library Full Tutorial For Beginners

 

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 Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Coding Shiksha 2020. All Rights Reserved.
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :