# 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.

[![apidojo](/blog/authors/apidojo.png)**Apidojo** *15+ years of experience in data retrieval.*](/blog/authors/apidojo/)

**16 October 2026** Published

**3 min** Read

![Cover card reading 'How to Collect Instagram Data With Python'](/blog/covers/instagram-python.png)

**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](/blog/instagram-scraping/how-to-scrape-instagram/).

## 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](https://apify.com/apidojo/instagram-scraper)). 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](https://apify.com/apidojo/instagram-scraper-api)).

```
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](https://apify.com/apidojo/instagram-comments-scraper-api)). Comment fields are covered in [how to scrape Instagram comments](/blog/instagram-scraping/scrape-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](https://apify.com/apidojo/instagram-user-scraper)).

## 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](/blog/instagram-scraping/limits/). The same workflow for other platforms is in [how to collect TikTok data with Python](/blog/tiktok-scraping/tiktok-python/) and [how to collect tweets with Python](/blog/twitter-scraping/scrape-tweets-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.

## The scrapers behind this

-   [Instagram Scraper Posts and reels from profiles, hashtags, locations and audio URLs; 100–200 posts/sec $0.50 / 1K posts](/scrapers/instagram-scraper/)
-   [Instagram Comments Scraper API Comments and replies with user data; 15 comments free per post $0.0075 / post + $0.50 / 1K](/scrapers/instagram-comments-scraper-api/)
-   [Instagram Profile Scraper Profiles, followers, following and public emails; 40 profiles free per search $0.01 / profile + $0.50 / 1K](/scrapers/instagram-user-scraper/)

## 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](/blog/instagram-api/instagram-api-guide/).

### Can you run the script on a schedule?

Yes, with cron or an Apify schedule; see [how to monitor Instagram hashtags and brand mentions](/blog/instagram-scraping/monitor-hashtags/).

### 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.

## More on scraping Instagram data

-   [How to Scrape Instagram Data in 2026 What Instagram data you can scrape, the 3 ways to collect it, which apidojo actor fits each task, limits, costs and Python code.](/blog/instagram-scraping/how-to-scrape-instagram/)
-   [How to Scrape Instagram Profiles and Posts Scrape any public Instagram profile's posts and profile details with apidojo's actors, with fields, reels and tagged URLs, and costs.](/blog/instagram-scraping/scrape-profiles/)
-   [How to Scrape Instagram Posts by Hashtag or Keyword Scrape Instagram hashtag posts and reels or keyword searches, compare apidojo's two actors, and see real costs per 1,000 posts.](/blog/instagram-scraping/scrape-hashtags/)
-   [How to Scrape Instagram Comments Scrape comments from any public Instagram post with apidojo's comment actors, with inputs, fields, costs and what to do with the data.](/blog/instagram-scraping/scrape-comments/)
-   [How to Scrape Instagram Followers and Following Export public Instagram follower and following lists, with the inputs, fields, verified-account limit and real costs per user.](/blog/instagram-scraping/scrape-followers/)
-   [How to Scrape Instagram Location Data Scrape posts tagged at Instagram locations with apidojo's actors, with inputs, location fields, costs, uses and limits.](/blog/instagram-scraping/scrape-locations/)
-   [How to Scrape Instagram Reels and Audio Scrape a creator's reels, every reel using a sound, or a hashtag's reels, with play counts, durations, costs and uses.](/blog/instagram-scraping/scrape-reels-audio/)
-   [Instagram Scraping Limits: Pagination and Blocks The documented limits of apidojo's Instagram actors, how pagination works, what gets runs blocked, and fixes for missing results.](/blog/instagram-scraping/limits/)
-   [Instagram profiles: search or handle lookup? A keyword search costs $0.02 and brings forty profiles. A handle lookup costs $0.01 and brings one. The right choice depends on what you already know.](/blog/instagram/instagram-keyword-vs-handle/)
-   [Measuring a campaign hashtag inside its own window A hashtag query costs $0.016 and brings sixty items with it. That allowance is per hashtag, which decides how you should slice a campaign.](/blog/instagram/instagram-campaign-hashtag-window/)
-   [What replyCount tells you before you pay Instagram comments are billed per post plus per comment, and reply threads are comments too. The top-level replyCount is your estimate, free.](/blog/instagram/instagram-reply-threads-cost/)

![apidojo](/blog/authors/apidojo.png)

## [Apidojo](/blog/authors/apidojo/)

15+ years of experience in data retrieval.

API Dojo is always ready for you. With 15+ years of experience, data acquisition has never been that easy. Cheapest prices with high availability.

-   🏯 15+ years of experience in data retrieval.
-   💰 Cheapest prices, qualified data.
-   ⚙️ High availability, extreme maintenance.
-   🐉 Hyper communicative, excellent support.

-   [apify.com](https://apify.com/apidojo)
-   [x.com](https://x.com/apidojo_)
-   [github.com](https://github.com/apidojo-io)
