A Python script to scrape tweets from X (formerly Twitter) using their web API. This script uses GraphQL endpoints and session cookies for authentication.
- Fetch tweets from any public Twitter account
- Get tweets from your home timeline
- Get pinned tweets
- Save tweets to JSON file
- Configurable number of tweets to fetch
- Uses GraphQL API for better reliability
- Python 3.7+
- Required packages (install via pip install -r requirements.txt):- requests
- python-dotenv
 
- Clone the repository:
git clone https://github.com/yourusername/XScraperBot.git
cd XScraperBot- Create a virtual environment and activate it:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Create a .envfile with your Twitter session cookies and tokens:
# Required for scraping tweets
X_AUTH_TOKEN=your_auth_token_here
X_CT0=your_ct0_here
# Required for getting latest GraphQL hashes
X_CSRF_TOKEN=your_csrf_token_here
X_TWITTER_AUTH_TYPE=OAuth2Session
X_TWITTER_CLIENT_LANGUAGE=en
X_TWITTER_ACTIVE_USER=yes
To get these tokens:
- Log into Twitter in your browser
- Open Developer Tools (F12)
- Go to Application tab
- Look for Cookies under Storage
- Find and copy the following values:
- auth_token→ X_AUTH_TOKEN
- ct0→ X_CT0 and X_CSRF_TOKEN
- twid→ X_TWITTER_AUTH_TYPE
- lang→ X_TWITTER_CLIENT_LANGUAGE
- active_user→ X_TWITTER_ACTIVE_USER
 
- First, get the latest GraphQL hashes:
python get_hashes.py- Then fetch tweets using one of these methods:
python scraper.py username [--count NUMBER]Example:
python scraper.py elonmusk --count 5python scraper.py --home [--count NUMBER]Example:
python scraper.py --home --count 10The script will:
- Fetch the specified number of tweets
- Display them in the console (with usernames for home timeline tweets)
- Save them to a JSON file:
- username_tweets.jsonfor user timeline
- home_timeline_tweets.jsonfor home timeline
 
- The script uses Twitter's web API, so it's subject to their rate limits
- Make sure to keep your session tokens secure and never commit them to version control
- The GraphQL hashes need to be updated periodically as Twitter updates their web client
- Home timeline access requires valid authentication tokens
MIT License - feel free to use this code for any purpose.
This tool is for educational purposes only. Make sure to comply with Twitter's terms of service and rate limits when using this script.