Asynchronous Python client for collecting public data from X (Twitter).
xscrape is a lightweight library for asynchronously collecting public data from X (Twitter): posts, profiles, threads, replies, and media. It supports account rotation, a built-in rate limiter, and export to JSON / CSV / SQLite.
- Features
- Installation
- Quick Start
- How It Works
- Configuration
- CLI
- Examples
- Docker
- Testing
- Project Layout
- Roadmap
- Contributing
- Disclaimer
- License
- π Post search β by keywords, hashtags, and operators (
from:,since:,until:) - π€ User profiles β metadata, followers, activity counters
- π§΅ Threads and replies β reconstruction of conversation chains
- πΌ Media β extraction of image and video links
- π Account rotation β session pool with automatic failover
- β± Rate limiting β adaptive control of request frequency
- πΎ Storage backends β JSON, CSV, SQLite out of the box
- π§© Plugins β custom handlers and exporters
- π₯ CLI β ready-to-use command line interface
- π³ Docker-ready β single command deployment
- π Structured logging β JSON logs with request tracing
git clone https://github.com/kloxeld/xscrape.git
cd xscrape
pip install -e .- Python 3.10+
aiohttp,pydantic,tenacity,orjson
pip install "xscrape[socks]" # SOCKS proxy support
pip install "xscrape[dev]" # development tools
pip install "xscrape[docs]" # documentation builders# Search posts
xscrape search "python asyncio" --limit 50 --out tweets.json
# User profile
xscrape user elonmusk
# User timeline
xscrape timeline elonmusk --limit 200 --out timeline.csv
# Reconstruct a thread
xscrape thread 1234567890123456789 --out thread.json
# Collect by hashtag into SQLite
xscrape hashtag "#opensource" --limit 1000 --db hashtag.db
# Multi-account pool
XSCRAPE_POOL=accounts.json xscrape search "data engineering" --limit 2000
Run xscrape --help for the full command reference.
xscrape talks to public GraphQL endpoints of X using session cookies. The pipeline looks like this:
ββββββββββββββ ββββββββββββββββ ββββββββββββββ ββββββββββββββ
β Client ββββΆβ AuthPool ββββΆβ Fetcher ββββΆβ Parser β
ββββββββββββββ ββββββββββββββββ ββββββββββββββ ββββββββββββββ
β β
βΌ βΌ
ββββββββββββββ ββββββββββββββ
β RateLimiterβ β Storage β
ββββββββββββββ ββββββββββββββ
- Client β public interface (
search,user,thread,replies). - AuthPool β session pool; picks a free account, handles 429/401.
- Fetcher β low-level HTTP requests with retries and exponential backoff.
- Parser β normalizes raw responses into typed models (
Tweet,User). - RateLimiter β per-account token bucket plus a global cap.
- Storage β serialization of results into the chosen format.
See docs/ARCHITECTURE.md for details.
All options are read from environment variables (see .env.example):
| Variable | Description | Default |
|---|---|---|
XSCRAPE_COOKIES |
Cookie string (auth_token, ct0) |
β |
XSCRAPE_POOL |
Path to JSON with account pool | None |
XSCRAPE_CONCURRENCY |
Max parallel requests | 4 |
XSCRAPE_TIMEOUT |
Request timeout (seconds) | 20 |
XSCRAPE_RETRIES |
Number of retries on error | 3 |
XSCRAPE_USER_AGENT |
Custom User-Agent | built-in |
XSCRAPE_PROXY |
Proxy (http://user:pass@host:port) |
None |
XSCRAPE_LOG_LEVEL |
Logging level | INFO |
XSCRAPE_LOG_FORMAT |
text or json |
text |
Full reference: docs/CONFIGURATION.md.
The examples/ directory contains ready-to-run scripts:
search_tweets.pyβ search with pagination and filtersuser_timeline.pyβ collect a user's timelineexport_to_csv.pyβ dump results to CSVexport_to_sqlite.pyβ persist results into SQLitethread_dump.pyβ reconstruct a full threadhashtag_monitor.pyβ long-running hashtag watchermulti_account_pool.pyβ usage of an account pool
Run:
python examples/search_tweets.py --query "openai" --limit 200docker build -f docker/Dockerfile -t xscrape:latest .cp .env.example .env
docker compose up --builddocker compose -f docker-compose.dev.yml up --buildSee docs/EXAMPLES.md for advanced Docker workflows.
pytest -q # run everything
pytest tests/unit # unit tests only
pytest tests/integration # integration tests onlyCoverage:
pytest --cov=xscrape --cov-report=htmlxscrape/
βββ xscrape/ # library source
β βββ client.py # public client
β βββ auth.py # session pool
β βββ parser.py # response parsing
β βββ ratelimit.py # rate limiter
β βββ storage.py # storage backends
β βββ plugins/ # plugin system
β βββ exporters/ # pluggable exporters
βββ tests/ # unit + integration tests
βββ examples/ # ready-to-run scripts
βββ docs/ # documentation
βββ docker/ # Dockerfiles
βββ scripts/ # helper shell scripts
βββ .github/ # CI workflows, templates
- Search and profiles
- Account pool and rate limiter
- JSON / CSV / SQLite export
- CLI
- Docker support
- Plugin exporter system
- Media download support
- Webhook notifications
- Web monitoring dashboard
- Prometheus metrics
- GraphQL query cache
Full roadmap: docs/ROADMAP.md.
We welcome contributions. Please read CONTRIBUTING.md and CODE_OF_CONDUCT.md before opening a PR.
This project is intended for educational purposes and work with public data only. Use it in accordance with the laws of your jurisdiction and the platform's rules. The authors are not responsible for any consequences of use.
MIT β see LICENSE.