JSON request on Instagram

Asked

Viewed 769 times

0

I’m trying to get comments from instagram posts.

With the code below, I can catch a json with some information about the post through the post link with a parameter __a=1: https://instagram.com/p/post-code/? __a=1

The post-code is the post code, for example: Candglqjl2j

Ignore the space in the "links" instagram, because I can not post more than 8 links here.

import json, requests

url = 'https:// instagram  . com/p/CAnDglQJl2J/?__a=1'

response = requests.get(url)
data = json.loads(response.text)

print(data)

The answer returned by the above code is a json with some posting information, including some (not all) comments, and some other information indicating whether that post has more records (in this case, comments).

To continue picking up the rest of the comments, I need to request the following query url: https://instagram . com/graphql/query/? query_hash=hash-code&variables={"shortcode": post-code, "first": 12, "after": end_cursor}

The value of hash code is a hash indicating the operation to be performed (in this case, get more comments), and the value of it at this time is: bc3296d1ce80a24b1b6e40b1e72903f5

The key shortcode is the posting code (Candglqjl2j).

The key first is the amount of records (comments) that will be returned.

The value of the key after in the url query is exactly what I need to get more comments, and the value of the same I find in the key end_cursor contained in the json response of the first request (https://instagram . com/p/Candglqjl2j/? __a=1).

The problem is that when I request from the browser, by placing the request link (https://instagram . with/p/Candglqjl2j/?__a=1) in the request’s url field, json is shown normally, and the key value end_cursor is presented as follows::

"{\"cached_comments_cursor ": "18112231093126073 ", "bifilter_token ": "Kiobarya0abwaegaoaaoabgaeaaqaagacaaiaf07zzz5na88vaxs35x9_1ek5934vfrnhr99gslxaafmdmw3zdzufvnwzyekbrzmrr4lfafloz2n4jwvtl4fj_iuoffl__pf_t_uotf-X96u95jx6vd__yq_vunx9_qu-Jgtlsjniwhfarqyiwcaaaaa==\"}"

Valor da chave end_cursor no navegador

But when I request for python, it returns the json with the key value end_cursor presented as follows:

QVFETVdxME5fLXJ3TGEzVE5yaXNiby1ydnpUVkFJaEFsZGxYVkJDNVJ3d0IzNlBPV2p0R1R3OGI5eHZXdzhnUGhYWk8yall2R2tYZzdlYVdkbm00T2VoNg==

Valor da chave end_cursor no python

Because the value of the same key is presented one way in the browser, and another completely different in python?

I need to know how to get the key value end_cursor in python in the same way it is displayed in the browser, for the request in query url to work (https://instagram . com/graphql/query/?query_hash=hash-code&variables={"shortcode": post-code, "first": 12, "after": end_cursor})because I tried with the format that python returns to me but it doesn’t work.

For example, to get more comments, I need to make a request as follows in python:

import json, requests

url = 'https:// instagram . com/graphql/query/?query_hash=bc3296d1ce80a24b1b6e40b1e72903f5&variables={"shortcode": "CAnDglQJl2J", "first": 12, "after": "{\"cached_comments_cursor\": \"17856088486976357\", \"bifilter_token\": \"KEQBDACgABgAEAAQAAgACAD_3_f__P_vf_v_9f7____7u-_36zfoHB8tutf7__9_f33_tf____ff_57_6--__qf4HtTIgAA=\"}"}'

response = requests.get(url)
data = json.loads(response.text)

print(data)

Remembering that the value of end_cursor i got the json returned by the first request (https://instagram . com/p/Candglqjl2j/? __a=1)

Because the value of the key end_cursor of the same request is presented one way in the browser and another way in python? And how can I solve this problem?

Because the request using the url query does not return me comments in json when I use the key value end_cursor python-printed.

For example:

import json, requests

url = 'https:// instagram . com/graphql/query/?query_hash=bc3296d1ce80a24b1b6e40b1e72903f5&variables={"shortcode": "CAnDglQJl2J", "first": 12, "after": "QVFETVdxME5fLXJ3TGEzVE5yaXNiby1ydnpUVkFJaEFsZGxYVkJDNVJ3d0IzNlBPV2p0R1R3OGI5eHZXdzhnUGhYWk8yall2R2tYZzdlYVdkbm00T2VoNg=="}'

response = requests.get(url)
data = json.loads(response.text)

print(data)

This is returned here, both in the browser and in python: JSON retornado usando o valor da chave end_cursor retornada pelo python na query url

That’s why I need python to return the value of this key in the same format as it is displayed in the browser, as shown in the images, because only then does a json containing more comments get returned (and other information too).

For example:

import json, requests

url = 'https:// instagram . com/graphql/query/?query_hash=bc3296d1ce80a24b1b6e40b1e72903f5&variables={"shortcode": "CAnDglQJl2J", "first": 12, "after": "{\"cached_comments_cursor\": \"17856088486976357\", \"bifilter_token\": \"KEQBDACgABgAEAAQAAgACAD_3_f__P_vf_v_9f7____7u-_36zfoHB8tutf7__9_f33_tf____ff_57_6--__qf4HtTIgAA=\"}"}'

response = requests.get(url)
data = json.loads(response.text)

print(data)

This is returned here in the browser, because in python the key value end_cursor is that other way: JSON retornado usando o valor da chave end_cursor retornado pelo navegador

PS: The post-code and end_cursor values shown here are illustrative.

I really appreciate anyone who can help me.

2 answers

0

Hello sorry for the delay.

yes can access instagram by python, requests tbm does authentication.

Just not tried using , but in theory and explanations doc informs that it is possible.

readthedocs

I’ve done something similar to Spotify in thesis the authentication medium is the same.

Here is a list of questions some may be relevant to you

swarm.dev

I hope this is a solution to what you’re looking for and any other way we’re here!

:)

0

Hello!

I think everything so far is correct in your code.

Your browser has the credentials try to play this url path in an anonymous tab, where it does not cache or disable the browser cache and try to think it will return error.

Try Beautifulsoup that might solve.

Or try using application Apis such as "https://i.instagram.com/api/v1/"

:==> https://instagram.api-docs.io/1.0/accounts/Q7Yn9rxnTKeDiorjx

The various documentations on github that show that still right

Requires authentication and the result satisfies any purpose :)

Here’s an example using this dev template.

http://www.zumzet.ro/api/instagram/Instagram-API-python/InstagramAPI/InstagramAPI.py

  • Hello. I accessed through the anonymous tab and really, the value of the key end_cursor has the same format that is presented by python. Could you tell me what kind of encryption that is?

  • I have to be logged in to receive the json with the end_cursor key value the way I need it, I just tested it here. There’s a way I can authenticate on instagram with python, just so I can solve the problem?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.