Search code examples
pythoninstagram-api

Scrape data from different User on Instagram


I'm trying to scrape username and media information from a list of Instagram user using unofficial instagram-Api with python. The library is there

I understand how I can scrape information from the user that is logged but i can't understand how i can refer to another username.

This is the code for taking my Instagram information

from InstagramAPI import InstagramAPI
import time
username = 'myUser'
pwd = 'mypass'
API = InstagramAPI(username,pwd)
API.login()
time.sleep(2)
API.getProfileData()
pk = API.LastJson['user']['pk']
maxid = ''
while True:
API.getUserFeed(pk, maxid)
feed = API.LastJson
if 'fail' in feed['status']:
    break
for i in range(0, len(feed['items']) - 1):
    mediadata = feed['items'][i]
    print("\033[0;34m"
          "\n------------------------\n"
          "Media number: "
          "Like count:  "
          "Comment count: "
          .format(i,
                  mediadata['like_count'],
                  mediadata['comment_count']))
    if feed['items'][i]['caption'] is None:
        print("Caption:       ["
                         "\033[0;31m"
                         "No Caption available"
                         "\033[0;34m"
                         "]\n")
    else:
        caption = mediadata['caption']['text']
        if len(caption) > 30:
            caption = caption[:30] + ' (...)'
        print("Caption:       
     [\033[0;32m{}\033[0;34m]\n".format(caption))

Solution

  • I solved it with: API.searchUsername('IG_USERNAME') before this code: pk = API.LastJson['user']['pk']. It Works for me.

    Enjoy :D