Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple gets from JSON?
#1
Hello, I am writing a script using and Instagram API to get data from users. Here is the link for the Github page: https://github.com/LevPasha/Instagram-API-python, and here's the code I have so far. It's trying to get a list of everyone an account follows.

from InstagramAPI import InstagramAPI
import time

#Setup and login
username='example'
password='example1' 
API = InstagramAPI(username,password)
API.login()
user='alex36540' #this is the user to perform actions on

#find User ID
def get_userID(user):
    API.searchUsername(user)
    try:
        return API.LastJson["user"]["pk"]
    except Exception:
        print("Username doesn't exist")
        return False

#Get Following
following=[]
def get_Followers(user):
    next_max_id=True
    while next_max_id:
        print(next_max_id)
        if next_max_id==True:next_max_id=''
        _=API.getUserFollowings(get_userID(user),maxid=next_max_id)
        following.extend(API.LastJson.get('users',[]))
        print(following)
        next_max_id=API.LastJson.get('next_max_id','')
        time.sleep(60)
    len(following)
    unique_following={
        f['pk']:f
        for f in following
    }
    len(unique_following)
get_Followers(user)
I added the print in line 29 to show the JSON data that was getting added to the list, and this is one list item (there were hundreds of these):
[{'pk': 0000000, 'username': 'example1', 'full_name': 'John Doe', 'is_private': True, 'profile_pic_url': 'https://url.jpg', 'is_verified': False, 'has_anonymous_profile_picture': False, 'reel_auto_archive': 'unset', 'is_favorite': False}]
I was wondering how could I change line 28 so that I can pick out specific items I want to get, specifically just pk, username, and full name?

Also, I would like some help understanding what exactly the code starting from line 32 means, as it is not originally mine.
Reply
#2
Quote:I was wondering how could I change line 28 so that I can pick out specific items I want to get, specifically just pk, username, and full name?

I think what your looking for is this?

def get_Followers(user):
    next_max_id = True

    while next_max_id:
        if next_max_id == True:
            next_max_id = ''
            API.getUserFollowings(get_userID(user), maxid=next_max_id)
            followers = API.LastJson.get('users', []) 

            if type(followers) is list:
                for follower in followers:
                    following.append((follower['pk'],
                                      follower['username'],
                                      follower['full_name']
                                      )
                                    )   
            next_max_id = API.LastJson.get('next_max_id', '') 
Reply
#3
(Jan-04-2018, 04:19 PM)hshivaraj Wrote: I think what your looking for is this?
Sort of, and thank you for the effort. I would like to keep the original format of dictionaries as each of the list items. Is there any way to just get the ones I want or cut out the items I don't?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  geojson to json --missing multiple row output yoshi 9 2,730 Mar-06-2022, 08:34 PM
Last Post: snippsat
  Parse JSON multiple objects larkin_L 8 5,687 May-27-2020, 11:18 AM
Last Post: nuffink
  print python json dump onto multiple lines lhailey 2 19,790 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  Download multiple large json files at once halcynthis 0 2,774 Feb-14-2019, 08:41 AM
Last Post: halcynthis
  Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil anandmn85 2 4,222 Apr-19-2018, 05:56 AM
Last Post: anandmn85

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020