PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 26, 2022

[FIXED] how to get a json that varais each request by it's number

 October 26, 2022     instagram, instagram-api, instagram-story, python, web-scraping     No comments   

Issue

I made a request to Instagram v1 API it gives back the response in JSON like this The JSON data on pastebin.com

I noticed that I can get the number of IDs and the IDs by :

IDs = response['reels'][ide]["media_ids"]
count=response['reels'][ide]["media_count"]
  • I don't know where I can use these IDs to help extract the stories URL
  • I don't know how to use it to get the media URLs cause it changes with the number of stories also if there is another way to extract it, it may solve my problem
  • the "url" key is not unique it's used in other values

Solution

Assuming the "media URLs" are the values associated with a key "url" then you can just do this:

import json

def print_url(jdata):
    if isinstance(jdata, list):
        for v in jdata:
            print_url(v)
    elif isinstance(jdata, dict):
        if (url := jdata.get('url')):
            print(url)
        else:
            print_url(list(jdata.values()))


with open('instagram.json', encoding='utf-8') as data:
    print_url(json.load(data))


Answered By - OldBill
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing