How to Collect Instagram Data With Python

To collect Instagram data with Python, install apify-client, call apidojo's Instagram actors with your Apify token, and read each run's dataset.

Cover card reading 'How to Collect Instagram Data With Python'

To collect Instagram data with Python, install apify-client, call apidojo’s Instagram actors with your Apify token, and read each run’s dataset. One script can collect a hashtag’s posts, pull comments on the most-discussed ones, and profile the authors, with no Instagram login, proxies or Graph API approval.

This guide builds that script step by step: setup, posts, comments, profiles, cleaning and saving. Which actor fits which job is explained in how to scrape Instagram data in 2026.

What Do You Need?

You need Python 3, the apify-client and pandas packages, and an Apify API token on a paid plan. apidojo’s Instagram actors run in demo mode on Apify’s free plan, 5 runs a month with 10 items each (Apify). Listed below are the 3 setup steps.

  1. Install the packages: pip install apify-client pandas.
  2. Copy your API token from Apify Console.
  3. Store it as an environment variable: export APIFY_TOKEN=your_token.

Step 1: Collect Posts

Call the Instagram Scraper with a hashtag URL and read the dataset. This is the input format the actor takes (Apify).

import os
from apify_client import ApifyClient

client = ApifyClient(os.environ["APIFY_TOKEN"])

run = client.actor("apidojo/instagram-scraper").call(run_input={
    "startUrls": ["https://www.instagram.com/explore/tags/coffee/"],
    "maxItems": 300,
})
posts = list(client.dataset(run.default_dataset_id).iterate_items())
print(len(posts), "posts")

Each post includes id, code, url, createdAt, caption, likeCount, commentCount, owner and, for videos, video.playCount (Apify).

Step 2: Collect Comments on the Top Posts

Pass the code of the most-commented posts to the Comments Scraper API’s postIds input.

top = sorted(posts, key=lambda p: p.get("commentCount") or 0, reverse=True)[:10]

run = client.actor("apidojo/instagram-comments-scraper-api").call(run_input={
    "postIds": [p["code"] for p in top],
    "maxItems": 2000,
})
comments = list(client.dataset(run.default_dataset_id).iterate_items())
print(len(comments), "comments")

It charges $0.0075 per post, with the first 15 comments included (Apify). Comment fields are covered in how to scrape Instagram comments.

Step 3: Profile the Authors

Send the post authors’ usernames to the User Scraper with both list options off, so you only pay for profiles.

handles = sorted({p["owner"]["username"] for p in top})

run = client.actor("apidojo/instagram-user-scraper").call(run_input={
    "handles": handles,
    "getFollowers": False,
    "getFollowings": False,
})
profiles = list(client.dataset(run.default_dataset_id).iterate_items())

Each profile includes followerCount, biography, category, isBusiness and publicEmail where available (Apify).

Step 4: Clean and Save the Data

Load each list into pandas, remove duplicates, and save to CSV.

import pandas as pd

pd.json_normalize(posts).drop_duplicates("id").to_csv("posts.csv", index=False)
pd.json_normalize(comments).drop_duplicates("id").to_csv("comments.csv", index=False)
pd.json_normalize(profiles).drop_duplicates("id").to_csv("profiles.csv", index=False)

json_normalize flattens nested fields, so owner.username becomes its own column. Keep IDs as text in spreadsheets, because long numbers get rounded.

What Rules Should Your Script Follow?

Respect each actor’s documented rules, or runs can be rate-limited or blocked. Listed below are the 4 rules (Apify).

  1. Fetch at least 10 posts per query or profile on the Instagram Scraper.
  2. Don’t use customMapFunction to filter; filter in pandas instead.
  3. Don’t set a very near until date.
  4. Check run.status is SUCCEEDED before reading results.

More detail is in Instagram scraping limits. The same workflow for other platforms is in how to collect TikTok data with Python and how to collect tweets with Python.

What Does This Script Cost?

The example costs about $0.33: $0.15 for 300 posts, $0.075 for comments on 10 posts within the included 15 each, and about $0.105 for 10 profiles. Larger comment threads add a per-comment charge beyond the included 15.

Questions people ask

Is there an official Instagram Python library?

Meta's Instagram API is called over HTTP and only covers professional accounts that authorise your app; see Instagram API in 2026.

Can you run the script on a schedule?

Yes, with cron or an Apify schedule; see how to monitor Instagram hashtags and brand mentions.

Can you use JavaScript instead?

Yes, the apify-client npm package works the same way.

Where are the results stored?

In each run's dataset on Apify, downloadable as JSON, CSV or Excel.