Instagram profile pages are useful sources for public fields such as a username, display name, biography, follower count, post count, verification status, and profile image. Direct page scraping is brittle because page markup and browser checks change. This tutorial uses the Instagram-specific GramScraper API so your Python code works with a stable JSON response instead.
What you will build
By the end, you will have a small Python script that accepts an Instagram username, requests the public profile, checks for errors, and prints a normalized summary. You need Python 3.9 or newer and a GramScraper API key.
1. Install the Python requests package
Create a virtual environment if you want to keep the dependency isolated, then install requests:
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install requests2. Get your GramScraper API key
- Create a free account on GramScraper.
- Open the dashboard and generate an API key.
- Store it in an environment variable instead of committing it to source control.
New accounts include 100 free credits. A profile lookup costs one credit, and purchased credits do not expire.
export GRAMSCRAPER_API_KEY="gs_your_api_key"Treat the API key like a password. Call the API from your server or local script, not from browser JavaScript that exposes the key to visitors.
3. Test the profile endpoint with curl
The endpoint is GET /api/instagram/v1/user/by/username. Pass the username without the @:
curl --get \
--url "https://gramscraper.com/api/instagram/v1/user/by/username" \
--header "Authorization: Bearer $GRAMSCRAPER_API_KEY" \
--data-urlencode "username=natgeo"A successful request returns a JSON object. Available fields can vary with the account and the public data Instagram makes available.
{
"username": "natgeo",
"full_name": "National Geographic",
"biography": "...",
"follower_count": 0,
"following_count": 0,
"media_count": 0,
"is_verified": true,
"is_private": false,
"profile_pic_url": "https://..."
}The numeric values above are placeholders to demonstrate the response types; live counts come from the API response.
4. Fetch an Instagram profile with Python
Read the key from the environment, pass it as a Bearer token, and set a timeout so network failures do not leave your script hanging:
import os
import requests
API_URL = (
"https://gramscraper.com/api/instagram/"
"v1/user/by/username"
)
def fetch_profile(username: str) -> dict:
api_key = os.environ["GRAMSCRAPER_API_KEY"]
response = requests.get(
API_URL,
headers={"Authorization": f"Bearer {api_key}"},
params={"username": username.lstrip("@")},
timeout=20,
)
response.raise_for_status()
return response.json()
profile = fetch_profile("natgeo")
print(profile)5. Parse the JSON response
Use dict.get() for optional fields so one missing value does not crash the entire job:
def profile_summary(profile: dict) -> dict:
return {
"username": profile.get("username"),
"name": profile.get("full_name"),
"bio": profile.get("biography", ""),
"followers": profile.get("follower_count", 0),
"following": profile.get("following_count", 0),
"posts": profile.get("media_count", 0),
"verified": profile.get("is_verified", False),
"private": profile.get("is_private", False),
"avatar": profile.get("profile_pic_url"),
}
summary = profile_summary(profile)
print(f"@{summary['username']} — {summary['name']}")
print(f"Followers: {summary['followers']:,}")
print(f"Posts: {summary['posts']:,}")6. Handle authentication, credit, and network errors
Production scripts should distinguish between a bad key, exhausted credits, a missing profile, and a temporary service failure:
try:
profile = fetch_profile("natgeo")
except requests.HTTPError as exc:
status = exc.response.status_code
if status == 401:
print("Check your API key.")
elif status == 402:
print("This account needs more credits.")
elif status == 404:
print("Profile not found.")
else:
print(f"API request failed with HTTP {status}.")
except requests.RequestException as exc:
print(f"Network error: {exc}")
else:
print(profile_summary(profile))Where to go next
You can store periodic snapshots to measure changes, feed public profile metrics into an analytics dashboard, or combine profile lookups with the documented media endpoints. Respect Instagram users, applicable law, and the platform terms relevant to your use case. Avoid collecting data you do not need.
GramScraper is focused on Instagram extraction. The free tier includes 100 credits; Standard is $29 for 15,400 credits, and credits do not expire.
Try a profile response
Use the limited playground without an API key, or create an account for 100 free credits.
Open API Playground →