brightdata-instagram-scraper 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,542 @@
1
+ Metadata-Version: 2.5
2
+ Name: brightdata-instagram-scraper
3
+ Version: 0.1.0
4
+ Summary: Instagram scraper in Python: profiles, posts, reels and comments as JSON, on the Bright Data Instagram Scraper API (Web Scraper API). No Instagram login, no browser. 5,000 free credits a month.
5
+ Project-URL: Homepage, https://github.com/brightdata/instagram-scraper
6
+ Project-URL: Documentation, https://docs.brightdata.com/products/scrapers/instagram/introduction
7
+ Project-URL: Source, https://github.com/brightdata/instagram-scraper
8
+ Project-URL: Issues, https://github.com/brightdata/instagram-scraper/issues
9
+ Project-URL: Changelog, https://github.com/brightdata/instagram-scraper/blob/main/CHANGELOG.md
10
+ Author: Bright Data
11
+ License: Apache-2.0
12
+ License-File: LICENSE
13
+ Keywords: bright-data,instagram,instagram-api,instagram-scraper,profiles,reels,scraper,web-scraping
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: Apache Software License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3 :: Only
20
+ Classifier: Topic :: Internet :: WWW/HTTP
21
+ Requires-Python: >=3.10
22
+ Requires-Dist: brightdata-sdk<3,>=2.5.2
23
+ Requires-Dist: rich<15,>=13
24
+ Provides-Extra: dev
25
+ Requires-Dist: pytest>=8; extra == 'dev'
26
+ Requires-Dist: ruff>=0.6; extra == 'dev'
27
+ Description-Content-Type: text/markdown
28
+
29
+ [![Scrape Instagram data with the Instagram Scraper API: profiles, posts, reels, search. Collect or discover by URL and username. Start free.](https://raw.githubusercontent.com/brightdata/instagram-scraper/main/.github/banner.png)](https://brightdata.com/products/web-scraper/instagram?utm_source=github&utm_medium=referral&utm_content=instagram-scraper)
30
+
31
+ # Instagram scraper for Python
32
+
33
+ Scrape Instagram profiles, posts, reels and comments to JSON with Python. No Instagram login, no access token, no browser.
34
+ Built on the [Bright Data Instagram Scraper API](https://brightdata.com/products/web-scraper/instagram?utm_source=github&utm_medium=referral&utm_content=instagram-scraper). New accounts get 5,000 free credits a month.
35
+
36
+ [![Live check](https://github.com/brightdata/instagram-scraper/actions/workflows/live.yml/badge.svg)](https://github.com/brightdata/instagram-scraper/actions/workflows/live.yml)
37
+ [![last verified](https://img.shields.io/badge/last%20verified-12%20Nov%202026-brightgreen)](https://github.com/brightdata/instagram-scraper/actions/workflows/live.yml) <!-- verified: rewritten by the daily run -->
38
+
39
+ [Quickstart](#how-to-scrape-instagram-data-with-python) · [No login](#scrape-instagram-without-login-or-an-access-token) · [CLI](#bulk-instagram-scraper-cli) · [Endpoints](#scrape-instagram-posts-profiles-reels-and-comments) · [Data](#instagram-post-data-fields-json) · [Errors](#instagram-scraper-errors) · [MCP and Claude Code](#instagram-mcp-server-and-claude-code-skill) · [Free credits](#is-this-instagram-scraper-free) · [Docs](https://docs.brightdata.com/products/scrapers/instagram/introduction) · [Support](#support)
40
+
41
+ Instagram profiles, posts, reels and comments as JSON, in Python. No Instagram
42
+ login, no browser. Built on the
43
+ [Bright Data Instagram Scraper API](https://brightdata.com/products/web-scraper/instagram?utm_source=github&utm_medium=referral&utm_content=instagram-scraper).
44
+
45
+ Uses the [Bright Data Python SDK](https://github.com/brightdata/sdk-python).
46
+ Full API docs:
47
+ [Instagram Scraper API](https://docs.brightdata.com/products/scrapers/instagram/introduction).
48
+
49
+ Also here: a one-command CLI for posts, and the
50
+ [Bright Data CLI](#instagram-mcp-server-and-claude-code-skill), which needs no Python at all.
51
+
52
+ ## How to scrape Instagram data with Python
53
+
54
+ Python 3.10 or newer.
55
+
56
+ ```bash
57
+ pip install brightdata-sdk
58
+ export BRIGHTDATA_API_TOKEN=YOUR_API_KEY
59
+ ```
60
+
61
+ Get a token from the
62
+ [Bright Data control panel](https://brightdata.com/cp/setting/users). A `.env`
63
+ file in the project root works too, as long as the virtualenv is inside the
64
+ project.
65
+
66
+ Or skip the token. Run `npx -p @brightdata/cli bdata login` once: it opens a
67
+ browser, and from then on the SDK finds the stored credentials on its own, for
68
+ you and for any coding agent working in that terminal. Agents cannot click
69
+ through the login, so do it yourself first. The same CLI scrapes Instagram
70
+ directly; see [Instagram MCP server and Claude Code skill](#instagram-mcp-server-and-claude-code-skill).
71
+
72
+ No account yet? [Create one](https://brightdata.com/cp/start); new accounts get
73
+ [5,000 free credits a month](https://docs.brightdata.com/general/account/billing-and-pricing/free-tier).
74
+
75
+ ```python
76
+ from brightdata import SyncBrightDataClient
77
+
78
+ with SyncBrightDataClient(auto_create_zones=False) as client:
79
+ result = client.search.instagram.posts("https://www.instagram.com/nasa/", num_of_posts=5)
80
+ for post in result.data:
81
+ print(post["likes"], post["num_comments"], post["url"])
82
+ ```
83
+
84
+ ```
85
+ 2437761 4692 https://www.instagram.com/p/Db9IVmrDvQ4/
86
+ 104278 902 https://www.instagram.com/reel/DcMXl1IPNtB/
87
+ 553843 4101 https://www.instagram.com/p/DcOX3hWFiey/
88
+ 39490 324 https://www.instagram.com/reel/Dc4u1yKPj_s/
89
+ 119582 1535 https://www.instagram.com/reel/Dc37MPNSAZq/
90
+ ```
91
+
92
+ Expect one to three minutes: the API runs a job and the SDK waits for it. One
93
+ [credit](https://brightdata.com/pricing/web-scraper) per post.
94
+
95
+ Pass `auto_create_zones=False` every time. Left on, the SDK creates zones on
96
+ startup for Web Unlocker and SERP, two other Bright Data products this scraper
97
+ never touches, and zone creation fails on accounts without a payment method
98
+ ([sdk-python#57](https://github.com/brightdata/sdk-python/issues/57)).
99
+
100
+ ## Scrape Instagram without login or an access token
101
+
102
+ The Instagram Scraper API needs no Instagram account, no Meta access token and
103
+ no Instagram business account. The only credential is a Bright Data API key,
104
+ set as `BRIGHTDATA_API_TOKEN` or stored by `bdata login`.
105
+
106
+ ## Bulk Instagram scraper CLI
107
+
108
+ The command in this repo runs the Python quickstart for several accounts at
109
+ once and writes one JSON file.
110
+
111
+ ```bash
112
+ pip install git+https://github.com/brightdata/instagram-scraper
113
+ instagram-scraper nasa natgeo
114
+ ```
115
+
116
+ ```
117
+ Fetching up to 5 recent posts per account, for: nasa, natgeo
118
+ Usually one to three minutes each. One credit per post, 5,000 free per month.
119
+ asking @nasa...
120
+ got @nasa: 5 posts
121
+ asking @natgeo...
122
+ got @natgeo: 5 posts
123
+
124
+ Saved 10 posts as JSON to instagram.json (34 fields per post)
125
+ ```
126
+
127
+ In a terminal the `asking` lines are replaced by this, updating in place, so
128
+ you can see it is working and how long it has been going:
129
+
130
+ ```
131
+ ⠹ @nasa 0:01:47
132
+ ```
133
+
134
+ ```
135
+ --limit N posts per account, default 5, minimum 1
136
+ --out PATH output file, default instagram.json
137
+ ```
138
+
139
+ `python -m instagram_scraper` works too.
140
+
141
+ Import it instead of running it, for `ok`, `note` and `error` per account
142
+ instead of raw rows. `scrape` never raises for one bad account; check `ok`
143
+ before reading `posts`:
144
+
145
+ ```python
146
+ from instagram_scraper import scrape
147
+
148
+ for outcome in scrape(["nasa", "zz_not_a_real_account_zz"], limit=1):
149
+ if outcome.ok:
150
+ print(f"{outcome.handle}: {len(outcome.posts)} posts")
151
+ else:
152
+ print(f"{outcome.handle} failed: {outcome.error}")
153
+ ```
154
+
155
+ ```
156
+ nasa: 1 posts
157
+ zz_not_a_real_account_zz failed: Crawler error: Cannot read properties of null (reading 'pk')
158
+ ```
159
+
160
+ An account with nothing recent is a success with no posts. The reason lands in
161
+ `note`, not `error`.
162
+
163
+ ## Scrape Instagram posts, profiles, reels and comments
164
+
165
+ The command covers one endpoint. The SDK has eight, documented in the
166
+ [Instagram Scraper API docs](https://docs.brightdata.com/products/scrapers/instagram/introduction).
167
+ Every snippet below is complete
168
+ and needs only `brightdata-sdk`: paste it as is. Every one of them runs in Actions each Monday, a smaller check
169
+ runs every other day, and the badge at the top is the latest result.
170
+
171
+ | you have | want | call |
172
+ | --- | --- | --- |
173
+ | a profile URL | recent posts | `client.search.instagram.posts(url, num_of_posts=5)` |
174
+ | a username | the profile | `client.search.instagram.profiles("nasa")` |
175
+ | a profile URL | the profile | `client.scrape.instagram.profiles(url)` |
176
+ | a profile URL | recent reels | `client.search.instagram.reels(url, num_of_posts=5, timeout=420)` |
177
+ | a profile URL | every reel ever | `client.search.instagram.reels_all(url)`, one credit per reel the account has posted |
178
+ | post URLs | those posts | `client.scrape.instagram.posts([url, url])` |
179
+ | reel URLs | those reels | `client.scrape.instagram.reels([url])` |
180
+ | a post or reel URL | its comments | `client.scrape.instagram.comments(url)`, one credit per comment |
181
+
182
+ Every one of these is an asynchronous job. The SDK triggers it, polls, and
183
+ returns when it is ready. That is why a call takes one to three minutes, and
184
+ why there is no faster path in Python. The API's
185
+ [synchronous endpoint](https://docs.brightdata.com/api-reference/scrapers/synchronous-requests),
186
+ 20 URLs and a one-minute limit, is raw HTTP only.
187
+
188
+ Error rows, like the empty-window one above, appear because the SDK asks for
189
+ them with `include_errors=true`. The API default is off.
190
+
191
+ `search` calls return a list in `result.data`. `scrape` calls with one URL
192
+ return one record as a dict, and several as a list.
193
+
194
+ ### Scrape multiple Instagram accounts in one job
195
+
196
+ A list of URLs is one job, not one per account.
197
+
198
+ ```python
199
+ from brightdata import SyncBrightDataClient
200
+
201
+ with SyncBrightDataClient(auto_create_zones=False) as client:
202
+ result = client.search.instagram.posts(
203
+ ["https://www.instagram.com/nasa/", "https://www.instagram.com/natgeo/"], num_of_posts=1
204
+ )
205
+ for post in result.data:
206
+ print(post["user_posted"], post["url"])
207
+ ```
208
+
209
+ ```
210
+ nasa https://www.instagram.com/p/DcOX3hWFiey/
211
+ natgeo https://www.instagram.com/reel/Dbru79IAdH-/
212
+ ```
213
+
214
+ ### Trigger now, fetch later
215
+
216
+ For anything bigger than a few accounts, do not block a process for an hour.
217
+ Trigger, keep the snapshot id, fetch when ready. Snapshots stay downloadable
218
+ for 30 days.
219
+
220
+ ```python
221
+ import time
222
+
223
+ from brightdata import SyncBrightDataClient
224
+
225
+ with SyncBrightDataClient(auto_create_zones=False) as client:
226
+ job = client.scrape.instagram.posts_trigger("https://www.instagram.com/p/Dc1W1uFj-CW/")
227
+ print("snapshot:", job.snapshot_id)
228
+ while (status := client.scrape.instagram.posts_status(job.snapshot_id)) not in ("ready", "failed"):
229
+ time.sleep(5)
230
+ print("status:", status)
231
+ record = client.scrape.instagram.posts_fetch(job.snapshot_id)[0]
232
+ print("fetched:", record["url"], "likes:", record["likes"])
233
+ ```
234
+
235
+ ```
236
+ snapshot: sd_mtrapflrzynbfyid9
237
+ status: ready
238
+ fetched: https://www.instagram.com/p/Dc1W1uFj-CW/ likes: 184
239
+ ```
240
+
241
+ ### Filter Instagram posts by date
242
+
243
+ ```python
244
+ from brightdata import SyncBrightDataClient
245
+
246
+ with SyncBrightDataClient(auto_create_zones=False) as client:
247
+ result = client.search.instagram.posts(
248
+ "https://www.instagram.com/nasa/",
249
+ num_of_posts=3,
250
+ start_date="08-01-2026", # MM-DD-YYYY
251
+ end_date="09-07-2026",
252
+ )
253
+ for post in result.data:
254
+ print(post["date_posted"], post["content_type"], post["url"])
255
+ ```
256
+
257
+ ```
258
+ 2026-08-12T21:28:58.000Z Image https://www.instagram.com/p/Db9IVmrDvQ4/
259
+ 2026-08-18T19:37:40.000Z Reel https://www.instagram.com/reel/DcMXl1IPNtB/
260
+ 2026-08-19T14:11:47.000Z Image https://www.instagram.com/p/DcOX3hWFiey/
261
+ ```
262
+
263
+ `post_type="Post"` keeps posts only and `post_type="Reels"` keeps reels only,
264
+ both verified. The SDK's docstring says `"Reel"`; that spelling returns an
265
+ error row and no reels ([sdk-python#59](https://github.com/brightdata/sdk-python/issues/59)).
266
+ `posts_to_not_include` takes a list of post IDs.
267
+
268
+ ### Scrape an Instagram profile by username
269
+
270
+ No URL needed. The record carries the account's followers count and post count.
271
+
272
+ ```python
273
+ from brightdata import SyncBrightDataClient
274
+
275
+ with SyncBrightDataClient(auto_create_zones=False) as client:
276
+ profile = client.search.instagram.profiles("nasa").data[0]
277
+ print(profile["account"], "followers:", profile["followers"], "posts:", profile["posts_count"])
278
+ ```
279
+
280
+ ```
281
+ nasa followers: 104387874 posts: 4913
282
+ ```
283
+
284
+ ### Scrape comments from an Instagram post or reel
285
+
286
+ One credit per comment, so check `num_comments` on the post first.
287
+
288
+ ```python
289
+ from brightdata import SyncBrightDataClient
290
+
291
+ with SyncBrightDataClient(auto_create_zones=False) as client:
292
+ comments = client.scrape.instagram.comments("https://www.instagram.com/p/Dc1W1uFj-CW/").data
293
+ print(len(comments), "comments; first:", repr(comments[0]["comment"][:60]))
294
+ ```
295
+
296
+ ```
297
+ 6 comments; first: 'Those two destroyed jazz'
298
+ ```
299
+
300
+ ### Scrape Instagram reels from a profile
301
+
302
+ Reels discovery is slower. The default 180-second timeout expired; 420 did not.
303
+
304
+ ```python
305
+ from brightdata import SyncBrightDataClient
306
+
307
+ with SyncBrightDataClient(auto_create_zones=False) as client:
308
+ reels = client.search.instagram.reels("https://www.instagram.com/nasa/", num_of_posts=2, timeout=420)
309
+ for reel in reels.data:
310
+ print(reel["date_posted"], reel["url"])
311
+ ```
312
+
313
+ ```
314
+ 2026-08-18T19:37:40.000Z https://www.instagram.com/p/DcMXl1IPNtB/
315
+ 2026-09-05T01:00:10.000Z https://www.instagram.com/p/Dc4u1yKPj_s/
316
+ ```
317
+
318
+ ## Instagram post data fields (JSON)
319
+
320
+ The fields most people want:
321
+
322
+ ```
323
+ url date_posted description hashtags likes num_comments user_posted
324
+ ```
325
+
326
+ The code hardcodes no field list. Whatever the API returns lands in
327
+ `result.data`, and in the command's file.
328
+ A collaborative post carries the co-author's handle in `user_posted`, so a post
329
+ fetched from nasa can say `nasajohnson`; `coauthor_producers` lists everyone.
330
+
331
+ <!-- fields:start -->
332
+ <details>
333
+ <summary>All 44 fields, with type and description</summary>
334
+
335
+ Regenerated every day from the dataset schema, via
336
+ `client.datasets.instagram_posts.get_metadata()`, so it cannot go stale. A
337
+ post carries the fields that apply to it; the sample file has 34
338
+ of these 44.
339
+
340
+ | field | type | description |
341
+ | --- | --- | --- |
342
+ | `url` | url | The direct URL of the Instagram post |
343
+ | `user_posted` | text | Username of the post creator |
344
+ | `description` | text | Post text description |
345
+ | `hashtags` | array | Hashtags used in the post |
346
+ | `num_comments` | number | Number of comments |
347
+ | `date_posted` | date | Post publication date |
348
+ | `likes` | number | Number of likes on the post |
349
+ | `photos` | array | URLs of attached photos, URLs can be expired due to Instagram policy |
350
+ | `videos` | array | URLs of attached videos, URLs can be expired due to Instagram policy |
351
+ | `location` | array | Geographical location associated with the post |
352
+ | `location_details` | object | Detailed geographical location metadata as returned by Instagram |
353
+ | `latest_comments` | array | Recent comments on the post |
354
+ | `post_id` | text | Unique post identifier |
355
+ | `discovery_input` | object | Discovery input values used to trigger the collection |
356
+ | `has_handshake` | boolean | Indicates if the post has a handshake (collaborative agreement between accounts) |
357
+ | `display_url` | text | Deprecated: previously used as the display URL of the post media |
358
+ | `shortcode` | text | The shortcode of the Instagram post, used in the post URL path |
359
+ | `content_type` | text | The type of content: Posts or Reels |
360
+ | `pk` | text | The primary key of the media content as assigned by Instagram |
361
+ | `content_id` | text | The content ID of the media item |
362
+ | `engagement_score_view` | number | Video view count used as an engagement score metric |
363
+ | `thumbnail` | text | The URL of the post's display image or video thumbnail |
364
+ | `video_view_count` | text | The number of views on the video post |
365
+ | `product_type` | text | The type of product associated with the post, such as 'clips' for Reels |
366
+ | `coauthor_producers` | array | List of co-authors or producers who collaborated on the post |
367
+ | `tagged_users` | array | List of users tagged in the post |
368
+ | `video_play_count` | number | The number of times the video has been played |
369
+ | `followers` | number | Number of followers the post owner has at the time of collection |
370
+ | `posts_count` | number | The total count of posts made by the account at the time of collection |
371
+ | `profile_image_link` | text | URL linking directly to the Instagram profile image of the post owner |
372
+ | `is_verified` | boolean | Indicates whether the post owner's account is verified |
373
+ | `is_paid_partnership` | boolean | Indicates whether the post is a sponsored or paid partnership |
374
+ | `partnership_details` | object | Details of the paid partnership brand associated with the post |
375
+ | `user_posted_id` | text | The Instagram user ID of the account that posted the post |
376
+ | `post_content` | array | List of media items (photos or videos) attached to the post, including carousel items |
377
+ | `audio` | object | Audio track metadata associated with the post or Reel |
378
+ | `profile_url` | url | URL of the Instagram profile that posted the post |
379
+ | `videos_duration` | array | List of video durations for each video attached to the post |
380
+ | `images` | array | List of image objects attached to the post |
381
+ | `alt_text` | text | Accessibility alt text for the post's main image: descriptive text that conveys the meaning of the image for blind or visually impaired users |
382
+ | `photos_number` | number | Total number of photos attached to the post |
383
+ | `audio_url` | url | Direct URL of the audio track used in the post |
384
+ | `thumbnail_array` | array | Deprecated: array of thumbnail URLs for the post media |
385
+ | `country` | text | Some profiles are restricted by location. Please set the country code in Alpha-2 format |
386
+
387
+ </details>
388
+ <!-- fields:end -->
389
+
390
+ <details>
391
+ <summary>The start of a real output file, from <code>instagram-scraper nasa --limit 1</code></summary>
392
+
393
+ ```json
394
+ {
395
+ "generated_at": "2026-08-14T11:01:05.410349+00:00",
396
+ "handles": [
397
+ {
398
+ "handle": "nasa",
399
+ "posts": [
400
+ {
401
+ "url": "https://www.instagram.com/p/Db_SePSltfz/",
402
+ "user_posted": "nasa",
403
+ "description": "A dance of darkness and light\n\nThis composite image shows the total solar eclipse from beginning to end, as seen in San Mill\u00e1n de los Caballeros, Spain on Aug. 12, 2026.\n\nWhile eclipses are beautiful and magical to experience, they're also a great opportunity for science. Total solar eclipses are particularly important because they allow scientists to see a part of the Sun\u2019s atmosphere known as the corona. The corona is too faint to see except when the bright light of the Sun is blocked.\n\nStudying the corona is key to answering important questions about how heat and energy are transferred from the Sun out into the solar wind, the constant stream of particles that the Sun spews into the solar system.\n\nCredit: NASA\n\n#NASA #Sun #SolarEclipse #Moon",
404
+ "hashtags": [
405
+ "#NASA",
406
+ "#Sun",
407
+ "#SolarEclipse",
408
+ "#Moon"
409
+ ],
410
+ "num_comments": 1109,
411
+ "date_posted": "2026-08-13T17:36:02.000Z",
412
+ "likes": 627573,
413
+ "photos": [
414
+ ...
415
+ ```
416
+
417
+ The whole file, one post with every field, is
418
+ [examples/sample_output.json](https://github.com/brightdata/instagram-scraper/blob/main/examples/sample_output.json).
419
+
420
+ </details>
421
+
422
+ ## Instagram scraper errors
423
+
424
+ | you see | what it means |
425
+ | --- | --- |
426
+ | `API token required but not found.` | Exit 2, before any request. Set the token. |
427
+ | `failed @name: ...` | Exit 1. No such account, usually a typo. The wording varies: "Sorry, this page isn't available." and "Crawler error: Cannot read properties of null" are both this. |
428
+ | `got @name: 0 posts, the account has no public posts in the period searched` | Exit 0, and correct. The API reports an empty window as an error row. |
429
+ | `failed @name: timeout` | Exit 1. A request gives up after 180 seconds. Run it again. |
430
+
431
+ Any failure exits 1, so a run is safe to gate a script on.
432
+
433
+ From the SDK, the same conditions look like this:
434
+
435
+ | you see | what it means |
436
+ | --- | --- |
437
+ | `AuthenticationError: Unauthorized (401)` | The token is set but wrong. |
438
+ | `result.success` is `False`, `result.status` is `"timeout"` | The SDK gave up waiting, 180 seconds by default. Pass `timeout=420`, or run it again. |
439
+ | a row in `result.data` with an `error` key | The API's answer for one input: an empty window, or no such account. The other rows are fine. |
440
+
441
+ ## Instagram MCP server and Claude Code skill
442
+
443
+ No Python, nothing installed. Paste both lines; the first opens a browser
444
+ once, or use `bdata login --device` over SSH and in CI:
445
+
446
+ ```bash
447
+ npx -p @brightdata/cli bdata login
448
+ npx -p @brightdata/cli bdata pipelines run gd_lk5ns7kz21pck8jpis collect_by_url "https://www.instagram.com/p/Db_SePSltfz/"
449
+ ```
450
+
451
+ The CLI runs a scraper by its dataset ID. The four Instagram scrapers each
452
+ take a post, reel or profile URL and return that one record;
453
+ `bdata pipelines search instagram` lists their IDs and methods.
454
+
455
+ `npx skills add brightdata/skills` teaches Claude Code, Cursor and Codex these
456
+ commands and the docs, so plain language works afterwards. Full guide:
457
+ [Bright Data for your coding agent](https://docs.brightdata.com/quickstart-coding-agent).
458
+
459
+ No terminal, for a hosted assistant? The
460
+ [Bright Data MCP server](https://github.com/brightdata/brightdata-mcp#which-tool-to-use)
461
+ has the same four Instagram tools, one URL each, in its `social` group, which
462
+ is off unless you ask for it:
463
+
464
+ https://mcp.brightdata.com/mcp?token=YOUR_API_TOKEN&groups=social
465
+
466
+ An agent can also open the account itself, no signup form:
467
+ [agent registration](https://brightdata.com/auth.md). Everything else Bright
468
+ Data connects to, from LangChain to Zapier and n8n:
469
+ [integrations](https://docs.brightdata.com/integrations/introduction).
470
+
471
+ <details>
472
+ <summary>Every Instagram command the CLI has, each run today</summary>
473
+
474
+ Four scrapers, each run with `bdata pipelines run <dataset_id> collect_by_url`:
475
+ profiles `gd_l1vikfch901nx3by4`, posts `gd_lk5ns7kz21pck8jpis`, reels
476
+ `gd_lyclm20il4r5helnj` and comments `gd_ltppn085pokosxh13`. Each takes URLs,
477
+ prints JSON, and costs one credit per record. Prefix each command with `npx -p @brightdata/cli`, or install once with
478
+ `npm i -g @brightdata/cli`.
479
+
480
+ ```bash
481
+ bdata pipelines run gd_l1vikfch901nx3by4 collect_by_url "https://www.instagram.com/nasa/" --pretty
482
+ ```
483
+
484
+ 1 record, 25 fields, 50 seconds. `"account": "nasa"`, `"followers": 104396028`.
485
+
486
+ ```bash
487
+ bdata pipelines run gd_lk5ns7kz21pck8jpis collect_by_url "https://www.instagram.com/p/Dc1W1uFj-CW/" --format csv -o posts.csv
488
+ ```
489
+
490
+ 37 seconds. The CSV header carries the same field names as the table below.
491
+
492
+ ```bash
493
+ bdata pipelines run gd_lyclm20il4r5helnj collect_by_url "https://www.instagram.com/reel/DcMXl1IPNtB/" --pretty
494
+ ```
495
+
496
+ 1 record, 25 fields, 23 seconds.
497
+
498
+ ```bash
499
+ bdata pipelines run gd_ltppn085pokosxh13 collect_by_url "https://www.instagram.com/p/Dc1W1uFj-CW/" --pretty
500
+ ```
501
+
502
+ 5 records, 11 fields, 9 seconds. One credit per comment, so check the post's
503
+ `num_comments` first.
504
+
505
+ `bdata pipelines info <dataset_id> <method>` shows a scraper's input fields.
506
+ `bdata pipelines run --help` shows `--format json|csv|ndjson|jsonl`, `-o FILE`,
507
+ `--timeout` and `--async`. CLI 0.4.0 replaced the old
508
+ `bdata pipelines instagram_posts <url>` form with these commands.
509
+
510
+ </details>
511
+
512
+ ## Is this Instagram scraper free?
513
+
514
+ New Bright Data accounts get
515
+ [5,000 free credits a month](https://docs.brightdata.com/general/account/billing-and-pricing/free-tier).
516
+ Posts, reels and profiles cost one credit per record, and comments one credit
517
+ per comment, so the free credits cover 5,000 posts a month.
518
+ [Pricing](https://brightdata.com/pricing/web-scraper) lists the rates after that.
519
+
520
+ ## Support
521
+
522
+ Bugs in this repo:
523
+ [open an issue](https://github.com/brightdata/instagram-scraper/issues), and
524
+ [CONTRIBUTING.md](https://github.com/brightdata/instagram-scraper/blob/main/CONTRIBUTING.md) says what to put in it.
525
+ Anything about the API, your account or your credits:
526
+ [Bright Data support](https://brightdata.zendesk.com/hc/en-us/requests/new).
527
+
528
+ ## License
529
+
530
+ [Apache 2.0](LICENSE).
531
+
532
+ ## Instagram scraper for Node.js
533
+
534
+ The same Instagram scraper is available for Node.js: [brightdata/instagram-scraper-node](https://github.com/brightdata/instagram-scraper-node).
535
+
536
+ ## More open source scrapers from Bright Data
537
+
538
+ Other Python scrapers built the same way, with live-verified examples:
539
+
540
+ - [TikTok scraper for Python](https://github.com/brightdata/tiktok-scraper)
541
+ - [Facebook scraper for Python](https://github.com/brightdata/facebook-scraper)
542
+ - [YouTube scraper for Python](https://github.com/brightdata/youtube-scraper)
@@ -0,0 +1,8 @@
1
+ instagram_scraper/__init__.py,sha256=nrzlAaCOQ4q2KXLxiVq_31Mq6uc9mIMEk-QFNN6bNUU,211
2
+ instagram_scraper/__main__.py,sha256=OUqE_4OEa6hww5fHWLjatCBXV5GtQfHgsFwZqcB84GI,3029
3
+ instagram_scraper/scrape.py,sha256=EQBmdr_D2KKKg3c3T4nkmROnGvQUpX6cFNhuJ73EkQI,6280
4
+ brightdata_instagram_scraper-0.1.0.dist-info/METADATA,sha256=C_uPSHDVn558LPaszPpMYl_Xa4nRmCRzVJsZ11DaIzo,24471
5
+ brightdata_instagram_scraper-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
6
+ brightdata_instagram_scraper-0.1.0.dist-info/entry_points.txt,sha256=N-LIuQgN2Xsoq-OoV0b2BSz1LMvebOXesWUVAAim14k,70
7
+ brightdata_instagram_scraper-0.1.0.dist-info/licenses/LICENSE,sha256=yVuuHRzgI17MzTVgt3LsHvuX80innw--CmNPDCzO_iw,11358
8
+ brightdata_instagram_scraper-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ instagram-scraper = instagram_scraper.__main__:main
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
@@ -0,0 +1,6 @@
1
+ """An Instagram scraper on Bright Data's Scraper API."""
2
+
3
+ from .scrape import Outcome, rows, scrape, scrape_handle, write
4
+
5
+ __all__ = ["Outcome", "rows", "scrape", "scrape_handle", "write"]
6
+ __version__ = "0.1.0"
@@ -0,0 +1,73 @@
1
+ """instagram-scraper nasa natgeo, or python -m instagram_scraper nasa natgeo"""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import sys
7
+
8
+ from brightdata import BrightDataError
9
+ from rich.progress import Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
10
+
11
+ from .scrape import client_context, scrape_handle, write
12
+
13
+ #: The SDK's own advice lists a Python parameter. A command has no such thing.
14
+ NO_TOKEN = (
15
+ "API token required but not found.\n"
16
+ " export BRIGHTDATA_API_TOKEN=YOUR_API_KEY token: https://brightdata.com/cp/setting/users\n"
17
+ " or run once: npx -p @brightdata/cli bdata login"
18
+ )
19
+
20
+ #: A spinner and a running clock, so a minute of waiting looks alive.
21
+ WAITING = (SpinnerColumn(), TextColumn("{task.description}"), TimeElapsedColumn())
22
+
23
+
24
+ def positive(value: str) -> int:
25
+ """Reject a limit the API would charge for and not honour."""
26
+ number = int(value)
27
+ if number < 1:
28
+ raise argparse.ArgumentTypeError(f"{value} is not 1 or more")
29
+ return number
30
+
31
+
32
+ def main(argv: list[str] | None = None) -> int:
33
+ parser = argparse.ArgumentParser(
34
+ prog="instagram-scraper",
35
+ description="Scrape recent Instagram posts for one or more handles.",
36
+ )
37
+ parser.add_argument("handles", nargs="+", help="handles, with or without the @")
38
+ parser.add_argument("--limit", type=positive, default=5, help="posts per handle, default 5")
39
+ parser.add_argument("--out", default="instagram.json", help="output file, default %(default)s")
40
+ args = parser.parse_args(argv)
41
+
42
+ print(
43
+ f"Fetching up to {args.limit} recent posts per account, for: {', '.join(args.handles)}\n"
44
+ "Usually one to three minutes each. One credit per post, 5,000 free per month.",
45
+ flush=True, # piped, an unflushed header would print after the error below
46
+ )
47
+
48
+ outcomes = []
49
+ try:
50
+ with client_context() as client, Progress(*WAITING, transient=True) as bar:
51
+ for handle in args.handles:
52
+ if not bar.console.is_terminal:
53
+ print(f"asking @{handle}...", flush=True) # a log wants a line, not a spinner
54
+ task = bar.add_task(f"@{handle}")
55
+ outcome = scrape_handle(client, handle, args.limit)
56
+ bar.remove_task(task)
57
+ # markup off: an API message with brackets in it is not markup
58
+ bar.console.print(outcome.line(), markup=False, highlight=False)
59
+ outcomes.append(outcome)
60
+ except BrightDataError as exc:
61
+ # Almost always a missing token, which reads as a crash under a traceback.
62
+ print(NO_TOKEN if "token required" in str(exc) else exc, file=sys.stderr)
63
+ return 2
64
+
65
+ path = write(outcomes, args.out)
66
+ posts = [post for outcome in outcomes for post in outcome.posts]
67
+ fields = f" ({len(posts[0])} fields per post)" if posts else ""
68
+ print(f"Saved {len(posts)} posts as JSON to {path}{fields}")
69
+ return 0 if all(o.ok for o in outcomes) else 1
70
+
71
+
72
+ if __name__ == "__main__":
73
+ sys.exit(main())
@@ -0,0 +1,170 @@
1
+ """Get a creator's recent Instagram posts by handle.
2
+
3
+ handle -> search.instagram.posts -> full post records
4
+
5
+ No second collection step. Discovery returns the complete record: 34 fields on
6
+ 2026-08-14, against 33 from collecting the same post afterwards. The extra call
7
+ cost a record per post, added 75 seconds, and dropped every reel.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import json
13
+ import re
14
+ from collections.abc import Iterable
15
+ from contextlib import nullcontext
16
+ from dataclasses import dataclass, field
17
+ from datetime import datetime, timezone
18
+ from pathlib import Path
19
+ from typing import Any
20
+ from urllib.parse import urlsplit
21
+
22
+ from brightdata import SyncBrightDataClient
23
+
24
+ #: An empty date window comes back as an error row, not an empty list. Match on
25
+ #: the message: a dead page carries the same error_code.
26
+ EMPTY_WINDOW = "There are no public posts in the profile for the specified period"
27
+
28
+ _HANDLE = re.compile(r"^[A-Za-z0-9._]{1,30}$")
29
+
30
+
31
+ def clean_handle(handle: str) -> str:
32
+ """Accept nasa, @nasa, or a profile URL. Reject anything else.
33
+
34
+ A URL counts only when its host is instagram.com or a subdomain of it, so
35
+ https://evil.com/instagram.com/nasa is rejected rather than read as nasa.
36
+ """
37
+ cleaned = (handle or "").strip().lstrip("@")
38
+ if "/" in cleaned:
39
+ url = urlsplit(cleaned if "://" in cleaned else f"https://{cleaned}")
40
+ host = (url.hostname or "").lower()
41
+ if host != "instagram.com" and not host.endswith(".instagram.com"):
42
+ raise ValueError(f"{handle!r} is not an Instagram handle")
43
+ segments = [part for part in url.path.split("/") if part]
44
+ cleaned = segments[0] if segments else ""
45
+ if not _HANDLE.match(cleaned):
46
+ raise ValueError(f"{handle!r} is not an Instagram handle")
47
+ return cleaned
48
+
49
+
50
+ def profile_url(handle: str) -> str:
51
+ return f"https://www.instagram.com/{clean_handle(handle)}/"
52
+
53
+
54
+ def rows(result: Any) -> list[dict[str, Any]]:
55
+ """Flatten a ScrapeResult, or a list of them, into plain dicts."""
56
+ if isinstance(result, list):
57
+ return [row for item in result for row in rows(item)]
58
+ data = getattr(result, "data", result)
59
+ if isinstance(data, dict):
60
+ data = [data]
61
+ if not isinstance(data, list):
62
+ return []
63
+ return [row for row in data if isinstance(row, dict)]
64
+
65
+
66
+ def envelope_error(result: Any) -> str | None:
67
+ """A failed or timed out request carries no rows to explain itself.
68
+
69
+ Without this check the run prints "0 posts", which reads like a creator with
70
+ nothing recent rather than a request that never came back.
71
+ """
72
+ if getattr(result, "success", True):
73
+ return None
74
+ return str(getattr(result, "error", None) or getattr(result, "status", "request failed"))
75
+
76
+
77
+ def split(result: Any) -> tuple[list[dict[str, Any]], list[str], list[str]]:
78
+ """Sort rows into records, empty-window notes, and real errors."""
79
+ records: list[dict[str, Any]] = []
80
+ notes: list[str] = []
81
+ errors: list[str] = []
82
+ for row in rows(result):
83
+ error = row.get("error")
84
+ if not error:
85
+ records.append(row)
86
+ elif EMPTY_WINDOW in str(error):
87
+ notes.append("the account has no public posts in the period searched")
88
+ else:
89
+ errors.append(str(error))
90
+ return records, notes, errors
91
+
92
+
93
+ @dataclass
94
+ class Outcome:
95
+ """What happened to one handle."""
96
+
97
+ handle: str
98
+ posts: list[dict[str, Any]] = field(default_factory=list)
99
+ error: str | None = None
100
+ note: str = ""
101
+
102
+ @property
103
+ def ok(self) -> bool:
104
+ return self.error is None
105
+
106
+ def line(self) -> str:
107
+ """One line a reader can understand without having read the source."""
108
+ if not self.ok:
109
+ return f"failed @{self.handle}: {self.error}"
110
+ tail = f", {self.note}" if self.note else ""
111
+ return f"got @{self.handle}: {len(self.posts)} posts{tail}"
112
+
113
+
114
+ def scrape_handle(client: Any, handle: str, limit: int = 5) -> Outcome:
115
+ """Fetch one handle. Never raises: a failure becomes an Outcome."""
116
+ try:
117
+ name = clean_handle(handle)
118
+ except ValueError as exc:
119
+ return Outcome(handle=str(handle), error=str(exc))
120
+
121
+ outcome = Outcome(handle=name)
122
+ try:
123
+ result = client.search.instagram.posts(profile_url(name), num_of_posts=limit)
124
+ failed = envelope_error(result)
125
+ if failed:
126
+ outcome.error = failed
127
+ return outcome
128
+ found, notes, errors = split(result)
129
+ outcome.note = "; ".join(dict.fromkeys(notes))
130
+ if errors:
131
+ outcome.error = "; ".join(errors)
132
+ else:
133
+ outcome.posts = found
134
+ except Exception as exc: # one bad handle must not end the run
135
+ outcome.error = f"{type(exc).__name__}: {exc}"
136
+ return outcome
137
+
138
+
139
+ def client_context(client: Any = None) -> Any:
140
+ """The client passed in, or one we open and own.
141
+
142
+ SyncBrightDataClient builds its event loop in __enter__, so it must be
143
+ entered. An unentered client fails with an AttributeError about
144
+ run_until_complete. Callers wanting progress hold this open and drive
145
+ scrape_handle themselves, as the CLI does.
146
+ """
147
+ # auto_create_zones defaults to True: the SDK tries to create a Web Unlocker
148
+ # zone on startup, which this scraper never uses. Creating a zone needs a
149
+ # payment method, so leaving it on breaks the first run for free accounts.
150
+ return nullcontext(client) if client is not None else SyncBrightDataClient(
151
+ auto_create_zones=False
152
+ )
153
+
154
+
155
+ def scrape(handles: Iterable[str], limit: int = 5, client: Any = None) -> list[Outcome]:
156
+ """Run every handle and return one Outcome each, in order."""
157
+ with client_context(client) as opened:
158
+ return [scrape_handle(opened, handle, limit) for handle in handles]
159
+
160
+
161
+ def write(outcomes: list[Outcome], path: str | Path) -> Path:
162
+ """Write one JSON file: when it ran, and the posts found per handle."""
163
+ document = {
164
+ "generated_at": datetime.now(timezone.utc).isoformat(),
165
+ "handles": [{"handle": o.handle, "posts": o.posts} for o in outcomes],
166
+ }
167
+ target = Path(path)
168
+ target.parent.mkdir(parents=True, exist_ok=True)
169
+ target.write_text(json.dumps(document, indent=2) + "\n", encoding="utf-8")
170
+ return target