SocialCrawl

Languages

SocialCrawl in Python

Read a profile, search many platforms and page through posts from Python with requests

Each step adds to one file, socialcrawl.py. Run it with python socialcrawl.py after each step.

Prerequisites

  • Python 3.10 or later.
  • A SocialCrawl API key. Keys start with sc_.

Checking your account…

Set your API key

Install requests, then set your key in the shell.

Terminal
pip install requests
export SOCIALCRAWL_API_KEY=sc_your_key_here

Keep the key on the server, out of your code and out of the browser.

Get a profile

Save this as socialcrawl.py. The call helper sends your key in the x-api-key header and stops when success is false.

socialcrawl.py
import json
import os

import requests

API = "https://www.socialcrawl.dev"
KEY = os.environ["SOCIALCRAWL_API_KEY"]


def call(method, path, **kwargs):
    """Send one request. Raise with error.type and error.doc_url when success is false."""
    res = requests.request(
        method, API + path, headers={"x-api-key": KEY}, timeout=60, **kwargs
    )
    body = res.json()
    if not body["success"]:
        error = body["error"]
        raise RuntimeError(f"{error['type']}: {error['message']} ({error.get('doc_url')})")
    return body


profile = call("GET", "/v1/tiktok/profile", params={"handle": "charlidamelio"})
print(f"followers: {profile['data']['author']['followers']} (credits_used {profile['credits_used']})")

Run python socialcrawl.py. It prints the follower count from data.author.followers and the credits_used of the call.

Every answer carries success. Branch on it, then on error.type, never on the message text.

Search across platforms

Add this to socialcrawl.py. With dry_run=1 the search runs nothing and is free. data.estimate holds what the real search would cost and how many seconds it would take.

socialcrawl.py
quote = call(
    "GET",
    "/v1/search/everywhere",
    params={"query": "oat milk", "format": "compact", "dry_run": 1},
)
estimate = quote["data"]["estimate"]
print("estimate:", json.dumps({"credits": estimate["credits"], "seconds": estimate["seconds"]}))

Drop dry_run to run the search for 20 credits.

Get posts and the next page

Add this to socialcrawl.py. Each answer holds one page of posts in data.items. Send pagination.next_cursor back as cursor, and stop when pagination.has_more is false.

socialcrawl.py
params = {"handle": "charlidamelio"}
for page in (1, 2):
    posts = call("GET", "/v1/tiktok/profile/videos", params=params)
    pagination = posts["pagination"]
    print(
        f"page {page}: {len(posts['data']['items'])} posts "
        f"(credits_used {posts['credits_used']}), next_cursor {pagination['next_cursor']}"
    )
    if not pagination["has_more"]:
        break
    params["cursor"] = pagination["next_cursor"]

A page of posts costs 1 credit. Run python socialcrawl.py again to see all three steps.

Next steps

On this page

Ready to build?

Start getting social data for free and scale as your project grows. No credit card needed.