Skip to content

Instantly share code, notes, and snippets.

@oliver-batey
Created December 22, 2020 20:22
Show Gist options
  • Save oliver-batey/146522befd1f7321948ec0fb63aec8fd to your computer and use it in GitHub Desktop.
Save oliver-batey/146522befd1f7321948ec0fb63aec8fd to your computer and use it in GitHub Desktop.
Instagram Post Info
import pandas as pd
from instascrape import Post, Profile
def get_post_data(post_object,
attributes=['caption',
'upload_date',
'location',
'likes',
'comments',
'id']):
'''
Inputs:
post_object: an instascrape Post object
attributes: list of keys we want to extract from post_object
Returns:
Dictonary containing the key-value pairs specifed in attributes
'''
return {key:post_object[key] for key in attributes}
if __name__ == '__main__':
#Substitute 'username' with the name of the instagram
#profile you want to scrape (as a string)
profile = Profile('username')
#Scrape the profile
profile.scrape()
#Get list of the recent posts
recents = profile.get_recent_posts()
#Build dataframe
data = pd.DataFrame([get_post_data(post) for post in recents])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment