Skip to content

Instantly share code, notes, and snippets.

@freitzzz
Created July 31, 2018 17:48
Show Gist options
  • Save freitzzz/42fb2728141287facbafdd4e8c54c2f6 to your computer and use it in GitHub Desktop.
Save freitzzz/42fb2728141287facbafdd4e8c54c2f6 to your computer and use it in GitHub Desktop.
Get Count of Instagram Posts of a certain user by it's URL based on web scraping with Python
"""
Gets number of posts of a Instagram user 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_user_posts_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)
posts=json_content['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['count']
return int(posts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment