Skip to content

Latest commit

Β 

History

35 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

xscrape

Asynchronous Python client for collecting public data from X (Twitter).

Python License Build Version Docker

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.


Table of Contents


Features

  • πŸ” 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

πŸ“¦ Installation

From source

git clone https://github.com/kloxeld/xscrape.git
cd xscrape
pip install -e .

Requirements

  • Python 3.10+
  • aiohttp, pydantic, tenacity, orjson

Optional

pip install "xscrape[socks]"    # SOCKS proxy support
pip install "xscrape[dev]"      # development tools
pip install "xscrape[docs]"     # documentation builders

Quick Start

# 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.

How It Works

xscrape talks to public GraphQL endpoints of X using session cookies. The pipeline looks like this:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Client    │──▢│  AuthPool    │──▢│  Fetcher   │──▢│  Parser    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚                β”‚
                                            β–Ό                β–Ό
                                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                     β”‚ RateLimiterβ”‚   β”‚  Storage   β”‚
                                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  1. Client β€” public interface (search, user, thread, replies).
  2. AuthPool β€” session pool; picks a free account, handles 429/401.
  3. Fetcher β€” low-level HTTP requests with retries and exponential backoff.
  4. Parser β€” normalizes raw responses into typed models (Tweet, User).
  5. RateLimiter β€” per-account token bucket plus a global cap.
  6. Storage β€” serialization of results into the chosen format.

See docs/ARCHITECTURE.md for details.


Configuration

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.


Examples

The examples/ directory contains ready-to-run scripts:

  • search_tweets.py β€” search with pagination and filters
  • user_timeline.py β€” collect a user's timeline
  • export_to_csv.py β€” dump results to CSV
  • export_to_sqlite.py β€” persist results into SQLite
  • thread_dump.py β€” reconstruct a full thread
  • hashtag_monitor.py β€” long-running hashtag watcher
  • multi_account_pool.py β€” usage of an account pool

Run:

python examples/search_tweets.py --query "openai" --limit 200

🐳 Docker

Build

docker build -f docker/Dockerfile -t xscrape:latest .

Run with docker-compose

cp .env.example .env
docker compose up --build

Development stack

docker compose -f docker-compose.dev.yml up --build

See docs/EXAMPLES.md for advanced Docker workflows.


Testing

pytest -q                    # run everything
pytest tests/unit            # unit tests only
pytest tests/integration     # integration tests only

Coverage:

pytest --cov=xscrape --cov-report=html

Project Layout

xscrape/
β”œβ”€β”€ 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

Roadmap

  • 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.


Contributing

We welcome contributions. Please read CONTRIBUTING.md and CODE_OF_CONDUCT.md before opening a PR.


⚠️ Disclaimer

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.


License

MIT β€” see LICENSE.

About

Async Python client for collecting public data from X (Twitter) - search, profiles, threads, replies, and media. Built-in account rotation, adaptive rate limiting, and export to JSON/CSV/SQLite.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

256 stars

Watchers

9 watching

Forks

Releases

Packages

Contributors

Languages