Skip to content

Instantly share code, notes, and snippets.

@freitzzz
Created July 31, 2018 17:36
Show Gist options
  • Save freitzzz/121f34f53013168aaa685f583dcf3b9a to your computer and use it in GitHub Desktop.
Save freitzzz/121f34f53013168aaa685f583dcf3b9a to your computer and use it in GitHub Desktop.
Get Count of Instagram Comments of a certain post by it's URL based on web scraping with Python
"""
Gets number of comments of a Instagram Post based on page source using BS4 & JSON from GraphQL extraction
Thanks to <a href="http://edmundmartin.com/scraping-instagram-with-python/">edmundmartin</a> for the
beautiful instagram scraping methodology
"""
def get_instagram_post_comments_count(url):
restX,contentX=httplib2.Http().request(url)
soupX=BeautifulSoup(contentX,"html.parser")
body=soupX.find('body')
script_tag=body.find('script')
raw_string=script_tag.text.strip().replace('window._sharedData =', '').replace(';','')
json_content=json.loads(raw_string)
comments=json_content['entry_data']['PostPage'][0]['graphql']['shortcode_media']['edge_media_to_comment']['count']
return int(comments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment