xscrape

xscrape is a Python package and command-line tool that scrapes tweets and profiles from the x.com (Twitter) web
app, with the session of your browser.
- No API key: it calls the same GraphQL API as x.com's web app (
/i/api/graphql), with your session cookies
- Requests with curl_cffi, impersonating a real browser's TLS fingerprint
- Signs each call with an
x-client-transaction-id header, like the web app (x.com refuses searches without it)
- The GraphQL operations (query ids, feature flags) are built in, and can be refreshed from requests copied out of
the browser, so a new x.com release needs no code change
- Waits for the end of the rate-limit window on HTTP 429; retries on network errors and 5xx
- HTTP(S) and SOCKS proxies with failover
- Downloads photos, videos and GIFs in parallel (an
asyncio queue of workers, files written with aiofiles)
- Every account seen is remembered in a local SQLite cache (
aiosqlite), which saves a profile request per command
- Command
xscrape, or python -m xscrape, or use it as a library
Requires Python 3.11 or later.
Status: search (tweets, people, lists), profiles and their "About this account" panel, the tabs of a profile
(posts, replies, reposts, media), followers and following, single tweets and their replies, the tweets of a list, and
media downloads work.
Installation
Pick one of the options below. They all install the xscrape command.
With uv
# As a tool, available everywhere:
uv tool install xscrape
# The latest version from GitHub:
uv tool install git+https://github.com/AntoinePaix/xscrape
# Or from a clone (runs from the project's own virtual environment):
git clone https://github.com/AntoinePaix/xscrape
cd xscrape
uv sync
uv run xscrape --help
With pip
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install xscrape
Usage
Configuration
xscrape needs a logged-in x.com session: the auth_token and ct0 cookies. It takes them, in this order:
- from
XSCRAPE_COOKIES, in the environment or a .env file ($XSCRAPE_ENV_FILE, ./.env, then
~/.config/xscrape/.env);
- otherwise from the cookies of your browser (
--browser): by default (auto) the first browser logged in to x.com,
the most used first: Chrome, Edge, Safari, Firefox, Opera, Brave, Opera GX, Arc, Vivaldi, Chromium, LibreWolf.
The simplest way to fill the .env file: in the browser, open the developer tools on x.com, find a request to
/i/api/graphql/… in the Network tab (for example SearchTimeline while searching), Copy as cURL (POSIX), save
it to a file and run:
xscrape config import-curl request.curl # session -> ./.env, GraphQL operation -> ~/.config/xscrape/operations.json
xscrape config show # where the session comes from, known operations (no secret shown)
| Variable |
Description |
XSCRAPE_COOKIES |
Cookies of the session, as name=value; name2=value2 (auth_token and ct0 at least) |
XSCRAPE_BEARER |
Bearer token of the web app; without it, it is read from x.com's main script |
XSCRAPE_USER_AGENT |
User agent sent with the requests; default: the impersonated browser's |
XSCRAPE_ENV_FILE |
Path of a .env file to load first |
XSCRAPE_CACHE_DIR |
Folder of the cache: the users database and x.com's ondemand.s script (default ~/.cache/xscrape) |
The browser cookies are read with browser-cookie3. auto tries the
browsers in the order above, and stops at the first one logged in to x.com (with its auth_token and ct0
cookies): the browsers that are not installed are skipped silently, and one that is installed but cannot be read is
reported. Chrome and the browsers built on it encrypt their cookies: on Linux the key is in the keyring of your
session (GNOME Keyring, KWallet), which must be unlocked; on macOS, the system may ask once per browser to let
xscrape read its key; on Windows, recent Chrome versions cannot be read, and auto goes on with the next browser.
--browser NAME reads that browser only. The names are those of the list, in any case, with - or _
(opera-gx, Opera_GX); another name is refused with the list, and a suggestion for a typo (--browser crome:
"did you mean chrome?"). To use the browser after an import, remove the XSCRAPE_COOKIES line from .env: it
always comes first.
Real environment variables take precedence over the .env file. *Never commit .env or `.curlfiles**: they hold your session. They are listed in.gitignore, the pre-commit hooks refuse them, andpyproject.tomlleaves them out of the built package, so captures can be kept anywhere in the clone (for examplesrc/xscrape/curl_requests/`).
Commands
| Command |
Description |
xscrape search QUERY [-p TAB] [-n LIMIT] [-o FILE] |
Search tweets, people or lists; print them as JSON Lines |
xscrape user USERNAME... [-i FILE] [-o FILE] |
Show the profile of an account as JSON, of several as JSON Lines |
xscrape about USERNAME [-o FILE] |
Show the "About this account" panel: country, creation source, username changes |
xscrape tweets USERNAME [-t TAB] [-n LIMIT] [-o FILE] |
Print the tweets of an account as JSON Lines, newest first |
xscrape followers USERNAME [--verified] [-n LIMIT] [-o FILE] |
Print the followers of an account as JSON Lines |
xscrape following USERNAME [-n LIMIT] [-o FILE] |
Print the accounts an account follows as JSON Lines |
xscrape tweet TWEET... [-i FILE] [-o FILE] |
Show a tweet (id or URL) as JSON, several as JSON Lines |
xscrape replies TWEET [--sort ORDER] [-n LIMIT] [-o FILE] |
Print the replies to a tweet as JSON Lines |
xscrape retweeters TWEET [-n LIMIT] [-o FILE] |
Print the accounts that retweeted a tweet as JSON Lines |
xscrape list-tweets LIST [-n LIMIT] [-o FILE] |
Print the tweets of a list (id or URL) as JSON Lines |
xscrape download USERNAME [-t TAB] [-n LIMIT] [-d DIR] [-c N] |
Download the photos, videos and GIFs of an account |
xscrape download --search QUERY [-p TAB] [-n LIMIT] [-d DIR] [-c N] |
Download the photos, videos and GIFs of search results |
xscrape download --list LIST [-n LIMIT] [-d DIR] [-c N] |
Download the photos, videos and GIFs of the tweets of a list |
xscrape download --tweet TWEET [-d DIR] |
Download the photos, videos and GIFs of one tweet |
xscrape cache users [-s TEXT] [-n LIMIT] [-o FILE] |
List the accounts remembered in the cache |
xscrape cache stats / clear [-y] |
Cache location, number of users and age of the saved ondemand.s / empty the cache |
xscrape raw OPERATION [-V JSON] [-o FILE] |
Call an imported GraphQL operation and print its raw JSON answer |
xscrape config import-curl FILE |
Save the session and the GraphQL operation of a request copied as cURL |
xscrape config show |
Show where the session comes from and the known operations |
xscrape search "python lang:en" -n 200 -o python.jsonl # latest tweets, one JSON object per line
xscrape search "from:NASA filter:media" -p top -n 50 # x.com's search syntax: from:, since:, until:, lang:...
xscrape search "data science" -p people -n 40 # accounts
xscrape search python -p lists # lists
xscrape user @NASA # profile (also: NASA, https://x.com/NASA)
xscrape user id:11348282 # profile by user id (also: https://x.com/i/user/11348282)
xscrape user NASA X id:11348282 -o accounts.jsonl # several profiles: one request per 100
xscrape user -i usernames.txt -o accounts.jsonl # one username or id per line
xscrape about X # country, source, username changes, verified since
xscrape tweets NASA -n 500 -o nasa.jsonl # tweets of an account
xscrape tweets NASA -t replies -n 50 # its replies (also: reposts, media)
xscrape following NASA -n 500 -o nasa_following.jsonl # the accounts it follows
xscrape followers NASA --verified -n 200 # its followers with a verified account
xscrape tweet https://x.com/NASA/status/2124903027370774900 # one tweet
xscrape tweet -i tweet_ids.txt -o tweets.jsonl # many tweets: one request per 100 ids
jq -r .reply_to tweets.jsonl | grep -v null | xscrape tweet -i - # the tweets they answer, from standard input
xscrape replies 2124903027370774900 --sort recent -n 200 # its replies, newest first
xscrape retweeters 2124903027370774900 -n 1000 # the accounts that retweeted it
xscrape list-tweets https://x.com/i/lists/<id> -n 300 # the tweets of a list (find lists with search -p lists)
xscrape download NASA -n 100 # its last 100 posts with media -> downloads/NASA/
xscrape download NASA -t reposts -d ~/x -c 8 # media of what it retweeted, 8 files at a time
xscrape download --search "from:NASA filter:media" -n 50 # media of 50 search results -> downloads/search/from_NASA_filter_media/
xscrape download -s "#Artemis since:2026-11-01" -p latest # every result with media, newest first
xscrape raw SearchTimeline # with the variables of the imported request
xscrape raw SearchTimeline -V '{"rawQuery":"python","count":20,"querySource":"typed_query","product":"Latest"}' -o search.json
USERNAME is a username (NASA, @NASA, https://x.com/NASA) or a user id, written id:11348282 or
https://x.com/i/user/11348282 (UserByRestId), in every command that takes one: user, about, tweets,
followers, following and download. A bare number is a username: x.com usernames can be all digits. An id keeps
working after the account changes its username.
From a clone installed with uv sync, prefix the commands with uv run. The exit code is 1 on an error, 2 on a
usage error and 130 on Ctrl-C.
Common options
Same options, with the same names, as in the other scraper repos:
| Option |
Default |
Description |
-o, --output |
|
File to write the results to, instead of the terminal |
-x, --proxy |
|
Proxy to use; repeat for several (see Proxies) |
--proxy-file |
|
File with one proxy per line |
--impersonate |
chrome |
curl_cffi browser fingerprint |
--timeout |
30 |
Timeout of each request, in seconds |
--retries |
3 |
Retries per request on network errors or 5xx responses |
-v, --verbose |
|
Show debug logs (each request) |
-q, --quiet |
|
Only show warnings and errors |
--version |
|
Show the version and exit |
Options specific to xscrape: