InstagramHow-to

Instagram scraper in Python: profiles, posts, comments

A free 345-line Python script scrapes public Instagram profiles, posts, comments and search. It returned 10 profiles in 35 seconds, with no login.

5 min read

Short answer

  • One Python file with curl_cffi reads public Instagram profiles, single posts, comments and keyword search with no login or API key, and returned 10 profiles in 35 seconds.
  • It cannot list followers, following or a user's posts, and its comments stop short of the post's count: 185 of 258 on the post we tried.
  • ToolzerHub's Instagram API returns those lists with one API key, at 1 credit per call, with 100 free credits a month.
On this page

You can scrape public Instagram profiles, posts, comments and keyword search with one Python file and no login: toolzerhub/instagram-scraper on GitHub. It is 345 lines, depends on one package (curl_cffi) and returned 10 profiles in 35 seconds. It does not list followers, following or a user's posts; the API section below does.

Install the script and run it

Clone the repo, install curl_cffi and run profile on any public username.

bash
git clone https://github.com/toolzerhub/instagram-scraper.git
cd instagram-scraper
pip install -r requirements.txt
python instagram_scraper.py profile natgeo

The output, trimmed:

json
{
  "id": "787132",
  "username": "natgeo",
  "full_name": "National Geographic",
  "biography": "Step into wonder and find your inner explorer with National Geographic",
  "external_url": "http://visitstore.bio/natgeo",
  "bio_links": ["http://visitstore.bio/natgeo", "https://ngmdomsubs.nationalgeographic.com/..."],
  "category": null,
  "is_business": false,
  "is_private": false,
  "is_verified": true,
  "followers_count": 268396518,
  "following_count": 194,
  "profile_pic_url": "https://scontent.cdninstagram.com/..."
}

The other three commands take a post URL or shortcode (the part after /p/ or /reel/) or a keyword. Every command accepts -o file.json to save the result.

bash
python instagram_scraper.py post https://www.instagram.com/reel/Dd6-VBbhtY6/
python instagram_scraper.py comments Dd6-VBbhtY6 --limit 100 -o comments.json
python instagram_scraper.py search "national park" --limit 24 -o search.json

What each command returned

Each command returned live data in 2.6 to 9.0 seconds, and comments stopped at 185 of the post's 258.

  • profile natgeo took 7.3 seconds and returned the fields above.
  • post Dd6-VBbhtY6 took 2.6 seconds. It returned a reel about national parks with 5,985 likes, 258 comments, the caption, a timestamp and signed image and video URLs.
  • comments Dd6-VBbhtY6 --limit 100 took 8.7 seconds and returned 100 comments, two pages of 50. With --limit 1000 we got 185 comments in 8.8 seconds, then paging stopped. Every one had reply_count 0, so the file holds top-level comments only.
  • search "national park" --limit 24 took 9.0 seconds and returned 24 posts from 24 different accounts, all videos, with play_count from 1,686 to 8,268,768 (median about 115,600).
  • profile for 10 accounts in one command (natgeo, nasa, smithsonian, noaa and six more) returned 10 of 10 in 34.6 seconds.

The signed image_url, video_url and profile_pic_url values expire, so download media soon after you scrape it.

Export a list of profiles to CSV

Instagram is a class you can import, so a list of usernames becomes a CSV in a few lines. This script sits next to instagram_scraper.py, reuses one session for the whole list, waits 2 seconds between profiles and reports the ones that fail.

python
import csv
import os
import sys
import time

from instagram_scraper import Instagram, ScrapeError

FIELDS = ["username", "full_name", "followers_count", "following_count", "is_verified", "category", "external_url"]


def main(path, usernames):
    ig = Instagram(os.environ.get("HTTPS_PROXY"))  # one session for the whole list
    done = 0
    with open(path, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=FIELDS, extrasaction="ignore")
        writer.writeheader()
        for name in usernames:
            try:
                writer.writerow(ig.profile(name))
                done += 1
            except ScrapeError as exc:
                print(f"{name}: {exc}", file=sys.stderr)
            time.sleep(2)  # pause between profiles
    print(f"wrote {done} of {len(usernames)} profiles to {path}")


if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2:])
bash
python profiles_to_csv.py profiles.csv natgeo nasa nationalparkservice noaa smithsonian discovery zz_no_such_user_88123

It wrote 6 of 7 profiles in 36 seconds; the made-up name is skipped and the rest are kept:

csv
username,full_name,followers_count,following_count,is_verified,category,external_url
natgeo,National Geographic,268396513,194,True,,http://visitstore.bio/natgeo
nasa,NASA,104341563,90,True,,https://www.nasa.gov
nationalparkservice,National Park Service,7218966,602,True,Government organization,http://nps.gov
noaa,NOAA,917172,53,True,Government organization,http://www.noaa.gov
smithsonian,Smithsonian,1202952,223,True,,https://sprout.link/smithsonian
discovery,Discovery,19741243,936,True,,http://discovery.com/

Followers and posts with ToolzerHub

We make ToolzerHub. Our Instagram API returns what the script can't, as JSON from one API key:

You need Script ToolzerHub endpoint What one call returned
A user's posts no /v1/instagram/user/posts 12 posts for nasa, plus next_max_id
Reels no /v1/instagram/user/reels 12 reels for nasa
Followers no /v2/instagram/user/followers pages of 24, 20 and 18 users for a small business account
Following no /v2/instagram/user/following 25 accounts for nasa, plus next_max_id
Similar accounts no /v2/instagram/user/similar 79 accounts for natgeo in one call
Hashtag posts keyword search only /v2/instagram/hashtag/media a page of posts for #nationalpark, plus next_max_id
Comments 185 of 258 /v1/instagram/post/comments 15 comments plus an end_cursor

A user's posts, as a request and a trimmed response:

bash
curl -H "x-api-key: $TOOLZERHUB_API_KEY" \
  "https://api.toolzerhub.com/v1/instagram/user/posts?username_or_url=nasa"
json
{
  "data": {
    "num_results": 12,
    "more_available": true,
    "next_max_id": "...",
    "items": [
      { "code": "DeJ28eTxo4s", "taken_at": 1791292211, "product_type": "feed", "like_count": 237615, "comment_count": 731 },
      { "code": "DeCPVhERA8e", "taken_at": 1791036618, "product_type": "clips", "like_count": 225058, "comment_count": 2337 }
    ]
  }
}

That call took 6.0 seconds and returned 12 posts; the real items carry many more fields, including the caption. Those like_count and comment_count fields are all an Instagram engagement rate needs. Very large accounts get a short followers list (nasa returned 48 users with no next page), and comments come 15 a call, so a post with thousands of comments takes hundreds of calls. For a complete comments export, see export Instagram comments to CSV, and for the follower paging loop, see the Instagram API followers list.

What the API costs

Each of those endpoints is 1 credit per call. Exporting 1,000 followers at about 20 per page is roughly 50 calls, or 50 credits, and 1,000 comments at about 15 per call are roughly 67 credits. The 100 free credits a month need no card; $19 buys 10,000 credits that never expire, and plans for higher volume cover more accounts. Set your key and call the posts route on your first account.

For the script next to other tools, see the best Instagram scrapers compared. Meta's own API reaches only accounts you manage, and personal accounts have none since Basic Display shut down.

We are not affiliated with Instagram or Meta Platforms, Inc.

Get this data from your own code

The Instagram API returns it as JSON with one x-api-key header. 100 free credits monthly.