instaloader: Hashtag.from_name(): KeyError: 'graphql'

Found an error given by using wrong json metadata. Tried using different hashtags and to revert to previous versions of Instaloader, but it still gives the problem. I also checked your built-in code and everything looks fine to me, so I suppose it has something to do with Instagram changing it’s metadata format. Maybe this was already fixed for other types of objects but you forgot to fix it for Hashtags (?)

Traceback (most recent call last):
  File "C:\Users\exyss\Desktop\v3.0-beta\src\test.py", line 6, in <module>
    hashtag = instaloader.Hashtag.from_name(_loader.context, "cats")
  File "C:\Users\exyss\AppData\Local\Programs\Python\Python39\lib\site-packages\instaloader\structures.py", line 1302, in from_name
    hashtag._obtain_metadata()
  File "C:\Users\exyss\AppData\Local\Programs\Python\Python39\lib\site-packages\instaloader\structures.py", line 1316, in _obtain_metadata        
    self._node = self._query({"__a": 1})
  File "C:\Users\exyss\AppData\Local\Programs\Python\Python39\lib\site-packages\instaloader\structures.py", line 1311, in _query
    return self._context.get_json("explore/tags/{0}/".format(self.name),
KeyError: 'graphql'

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 23
  • Comments: 32 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks to you all for sharing your observations and for creating such a beautiful collection of workarounds for this issue!

The download of hashtags should now be fixed in Instaloader’s latest pre-release, Version 4.9b1.

Here’s a snippet that might elucidate #874 with datetime functionality taken from #121 . It takes posts between dates following a hashtag using the NodeIterator code and stores them in a list.

START = datetime(2021, 5, 28)
END = datetime(2021, 5, 30)
HASHTAG = "<Some Hashtag>"
GraphQL_Hash = "<Some GQL hash>"
posts = []

post_iterator = instaloader.NodeIterator(
    L.context, GraphQL_Hash,
    lambda d: d['data']['hashtag']['edge_hashtag_to_media'],
    lambda n: instaloader.Post(L.context, n),
    {'tag_name': HASHTAG},
    f"https://www.instagram.com/explore/tags/{HASHTAG}/"
)
        
def gen(start, end, posts):
    for post in posts:
        if post.date > start and post.date > end:
            pass
        elif post.date >= start and post.date <= end:
            yield post
        elif post.date <= start:
            break

for post in gen(start=START, end=END, posts=post_iterator):
    posts.append(post)

Hope this helps. Edited for formatting.

What is a “GQL hash”? I did it: er1 And the output was this error: er2 Where can i find the GQL hashtag for my “home” hashtag?

@hobzcalvin you can temporarily use my code. Just go through JSON and fetch all data you need (line 34).

import instaloader
import settings
from datetime import datetime


def instaLogin():
    print("Logging in...")

    ses = instaloader.Instaloader()
    ses.context.sleep = False
    ses.login(settings.USER, settings.PASSWORD)

    print("...done")

    return ses


def fetchPostsData(session, hashtag):
    print("Fetching posts data...")

    jsonData = session.context.get_json(path="explore/tags/" + hashtag + "/", params={"__a": 1})
    hasNextPage = True
    pageNumber = 1

    while hasNextPage:
        print("Page " + str(pageNumber))
        sections = jsonData['data']['recent']['sections']

        for section in sections:
            for post in section['layout_content']['medias']:
                username = post['media']['user']['username']
                timestamp = datetime.fromtimestamp(post['media']['taken_at']).date()

                # ADD YOUR CODE HERE
                print(username, timestamp)

        hasNextPage = jsonData['data']['recent']['more_available']
        if hasNextPage:
            jsonData = session.context.get_json(
                path="explore/tags/" + hashtag + "/",
                params={"__a": 1,
                        "max_id": jsonData['data']['recent']['next_max_id']}
            )
        pageNumber += 1


def main():
    session = instaLogin()

    if session.context.is_logged_in:
        fetchPostsData(session, "github")
    else:
        raise Exception("Authentication failure!")


if __name__ == '__main__':
    main()

I have partially isolated the issue: it’s only a problem when you’re logged in.

Yes, without --login it works well, but I often get a redirect HTTP redirect from https://www.instagram.com/graphql/query to https://www.instagram.com/accounts/login/

I have partially isolated the issue: it’s only a problem when you’re logged in. Here’s an example using the CLI:

This works:

instaloader "#climatechange" --no-pictures --no-videos --no-compress-json --no-profile-pic --count 5 --no-captions

This doesn’t, with the familiar graphql error:

instaloader "#climatechange" --login USERNAME --no-pictures --no-videos --no-compress-json --no-profile-pic --count 5 --no-captions

Same error here, @frankjuniorr . I’m using the version 4.7.4

@aandergr can you clarify how #874 (comment) can be used to fix this issue? Actually, I’m trying to use the command-line interface so any help getting that to work would be much appreciated.

I am using Instaloader 4.7.1. I have the same error. It seems to be a bug due to some recent changes made by Instagram.

I also have this error, an update would be nice

Same problem every time I try to log in