Get JSON from website (instagram)

20,949

Solution 1

I ran into a problem trying to get the API to do what I wanted, and really just needed JSON data including urls and captions for images for a specific account.

Use the following GET request:

https://www.instagram.com/account_name/?__a=1

where account_name is the profile I'm scraping.

It returns all JSON I needed for my task.

Solution 2

Trying to get the Json loaded in the background is too much work for a simple problem.

You should use the Instagram Api. Just put your name as a company.

Share:
20,949

Related videos on Youtube

Patrick.H
Author by

Patrick.H

Updated on July 05, 2022

Comments

  • Patrick.H
    Patrick.H almost 2 years

    I recently got the idea to scrape information from instagram accounts and their posts, like the amount of comments or amount of likes. I got so far that I figured out while debugging in chrome that for example the link https://www.instagram.com/instagram/?__a under the network tab returns a JSON with the wanted information, but what is actually loaded is still the normal website html code.

    json data

    so far I tried in python with this code:

    import urllib.request
    r = urllib.request.urlopen(url)
    print(r.read())
    

    or in javascript :

    window.onload = function () {
        res = fetch("https://www.instagram.com/instagram/?__a", {
            method: 'get'
        }).then(function (data) {
            return data.json();
        }).catch(function (error) {
            console.log("ERROR".concat(error.toString()));
        });
        console.log(res.user);
    };
    

    So the problem I have, is that when using these functions I only get the website code (html), is there a way to only get the JSON which is loaded in the background? I know people will recommend me using the instagram api, but I have no website nor a company to register.

  • Patrick.H
    Patrick.H over 6 years
    I didn't know that was possible, and which website do I type in as redirect url? I mean I don't own any
  • Michael Goetti
    Michael Goetti over 6 years
    The '=1' at the end of the GET URL appears to be critical. As the OP stated, without it, it just returns HTML. With it, you get just JSON.
  • Yusof Bandar
    Yusof Bandar over 6 years
    Here is a link to a Stack Overflow answer which should help.
  • Fernando Torres
    Fernando Torres almost 5 years
    how can i get more thumbnail resources from this url? thanks
  • Vlax
    Vlax almost 5 years
    How do you do pagination with this approach?
  • Vlax
    Vlax almost 5 years
    This is not more possible and heavily restricted
  • Haseeb Burki
    Haseeb Burki over 4 years
    This just saved my day :D How did you figure this out?!?!
  • Habib Karbasian
    Habib Karbasian over 4 years
    Check out stackoverflow.com/a/49341049/1874449 for pagination of post for a given user_id. It uses a unique query_hash given by instagram website available in the post.
  • brasofilo
    brasofilo about 3 years
    Facebook APIs were always a nightmare to work with, nowadays it's like impossible... lets scrape the hell out of them :P

Related