Created
July 31, 2018 17:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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