Answer the question
In order to leave comments, you need to log in
How to get comments from an Instagram post?
Hello! In general, I ran into a problem. You need to get the comments from under the Instagram post and display them.
Tried to implement this with requests and BeautifulSoup, but it doesn't work.
Prompt how to carry out it, what libraries to use.
Answer the question
In order to leave comments, you need to log in
Comments are in the script in the page code
import requests
import json
import re
headers = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'}
r = requests.get('https://www.instagram.com/p/CKJ4986gdbW/', headers=headers)
script = re.search(r'window._sharedData = (.+?);</script>', r.text).group(1)
data = json.loads(script)
edges = data['entry_data']['PostPage'][0]['graphql']['shortcode_media']['edge_media_to_parent_comment']['edges']
for edge in edges:
text = edge['node']['text']
author = edge['node']['owner']['username']
print(author, text)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question