DEV Community

Cover image for How to Scrape Twitter/X Data: Complete Guide for 2026
AlterLab
AlterLab

Posted on • Originally published at alterlab.io

How to Scrape Twitter/X Data: Complete Guide for 2026

Disclaimer: This guide covers extracting publicly accessible data. Always review a site's robots.txt and Terms of Service before scraping.

Extracting data from Twitter/X requires moving beyond standard HTTP requests. The platform is a heavy Single Page Application (SPA) built on React, utilizing complex client-side rendering, dynamic data fetching via GraphQL, and strict rate limiting.

This guide demonstrates how to build a robust pipeline for extracting public tweets, profile metadata, and trending topics using Python, handling the technical requirements of modern web scraping.

Why collect social data from Twitter/X?

Engineering and data teams typically extract public X data for three primary workflows:

  1. Market research and sentiment analysis: Aggregating public mentions of brand names, product launches, or competitors to feed natural language processing pipelines.
  2. Real-time event monitoring: Tracking public announcements, service outages, or breaking news events via verified accounts.
  3. Financial data modeling: Correlating public executive statements or official corporate announcements with market movements.

To power these use cases, you need structured, reliable data extraction.

Technical challenges

Attempting to run a standard curl or Python requests.get() against a Twitter/X URL will fail to return the actual content. The server responds with a minimal HTML shell containing JavaScript bundles. The actual data (tweets, profiles) is fetched asynchronously and rendered in the browser.

To access public content, your scraping infrastructure must handle:

  • JavaScript Execution: You need a headless browser (like Chromium) to execute the React application and wait for the DOM to hydrate.
  • Dynamic Loading: Content loads infinitely as the user scrolls. Extracting a full timeline requires simulating user interaction.
  • Rate Limiting: Aggressive request patterns from a single IP address will result in rate limits or block pages.

Managing headless browser clusters and proxy pools at scale introduces significant infrastructure overhead. This is where an Anti-bot bypass API becomes necessary to abstract the browser management and focus on data extraction.

Quick start with AlterLab API

To bypass the infrastructure setup, we will use AlterLab to handle the JavaScript rendering and proxy rotation automatically.

First, ensure you have reviewed the Getting started guide to configure your environment.

Here is how to extract the rendered HTML of a public profile using Python.

```python title="scrape_twitter_profile.py" {4-6}

ALTERLAB_API_KEY = "your_api_key_here"
TARGET_URL = "https://twitter.com/XDevelopers"
ENDPOINT = "https://api.alterlab.io/v1/scrape"

payload = {
"url": TARGET_URL,
"render_js": True,
"wait_for_selector": '[data-testid="primaryColumn"]'
}

headers = {
"X-API-Key": ALTERLAB_API_KEY,
"Content-Type": "application/json"
}

response = requests.post(ENDPOINT, json=payload, headers=headers)
print(response.json().get("content"))




For environments where you prefer shell scripting or testing via the command line, the equivalent request looks like this:



```bash title="Terminal" {3-5}
curl -X POST https://api.alterlab.io/v1/scrape \
  -H "X-API-Key: your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://twitter.com/XDevelopers",
    "render_js": true,
    "wait_for_selector": "[data-testid=\"primaryColumn\"]"
  }'
Enter fullscreen mode Exit fullscreen mode

By setting render_js to true and providing a wait_for_selector, we instruct the API to hold the connection open until the React application has fully loaded the main content column.

Extracting structured data

Once you have the fully rendered HTML, the next step is parsing it into structured formats like JSON. Twitter/X uses heavily obfuscated CSS class names that change frequently (e.g., css-1dbjc4n). Relying on these classes leads to brittle scrapers.

Instead, rely on data-testid attributes, which X developers use for their own internal testing. These attributes are significantly more stable.

Here is a Python example using BeautifulSoup to parse the rendered HTML and extract public tweets.

```python title="parse_tweets.py" {11-15}
from bs4 import BeautifulSoup

def extract_tweets(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
tweets_data = []

# Locate all tweet articles
articles = soup.find_all('article', attrs={'data-testid': 'tweet'})

for article in articles:
    # Extract text content
    text_element = article.find('div', attrs={'data-testid': 'tweetText'})
    tweet_text = text_element.get_text(separator=' ', strip=True) if text_element else None

    # Extract timestamp
    time_element = article.find('time')
    timestamp = time_element['datetime'] if time_element and time_element.has_attr('datetime') else None

    if tweet_text:
        tweets_data.append({
            "text": tweet_text,
            "timestamp": timestamp
        })

return json.dumps(tweets_data, indent=2)
Enter fullscreen mode Exit fullscreen mode

Assume html_content is the response from the previous step

print(extract_tweets(html_content))




<div data-infographic="steps">
  <div data-step data-number="1" data-title="Request" data-description="Send URL to API with JS rendering enabled"></div>
  <div data-step data-number="2" data-title="Wait" data-description="Block until data-testid='tweet' appears in DOM"></div>
  <div data-step data-number="3" data-title="Parse" data-description="Extract structured text and timestamps via BeautifulSoup"></div>
</div>

## Best practices

When building pipelines for social platforms, adherence to best practices ensures your scraper remains reliable and compliant.

1.  **Respect Robots.txt**: Always check `https://twitter.com/robots.txt`. Certain paths are explicitly disallowed. Ensure your scraper only targets paths meant for public visibility and indexing.
2.  **Handle Dynamic Content gracefully**: Elements load asynchronously. Never hardcode static sleep times (e.g., `time.sleep(5)`). Always use explicit waits for specific DOM elements, as shown with the `wait_for_selector` parameter.
3.  **Implement Rate Limiting**: Even when scraping public data, aggressive polling strains target servers. Implement exponential backoff and jitter in your retry logic to simulate organic traffic patterns.

<div data-infographic="try-it" data-url="https://twitter.com/XDevelopers" data-description="Test JavaScript rendering on a public X profile"></div>

## Scaling up

Transitioning from a local script to a production data pipeline requires handling high concurrency and managing costs.

If you are tracking hundreds of public profiles, serial execution is too slow. You must implement asynchronous request batching. Python's `asyncio` combined with `aiohttp` allows you to dispatch multiple requests concurrently while waiting for the browser rendering to complete on the server side.

When operating at this scale, monitor your infrastructure expenses. Refer to the [AlterLab pricing](/pricing) page to model costs based on your expected monthly request volume and JavaScript rendering requirements. Using a managed service often yields a lower total cost of ownership compared to maintaining a fleet of EC2 instances running Puppeteer and managing your own proxy rotations.

## Key takeaways

Extracting data from modern SPAs requires specific tooling. Raw HTTP clients are insufficient for React-heavy applications. By utilizing headless browsers, targeting stable `data-testid` attributes, and relying on managed infrastructure to handle the rendering overhead, you can build reliable pipelines for public social data. Always prioritize compliant access and respect the target platform's operational limits.

### Related guides
*   [How to Scrape Instagram](/blog/how-to-scrape-instagram-com)
*   [How to Scrape YouTube](/blog/how-to-scrape-youtube-com)
*   [How to Scrape Reddit](/blog/how-to-scrape-reddit-com)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)