Skip to main content

A Python CLI tool for scraping public X (Twitter) profile tweets using Playwright.

Project description

twitter-scraper

An open source Python tool for scraping public X (Twitter) tweets using Playwright.

Why I Built

I built this because I wanted to understand how scraping actually works.

I wanted to deal with a real modern site like X, where the page never fully settles, things keep loading in the background, and you can’t just wait for a simple page load event and hope for the best.

I also wanted to stop hardcoding values everywhere and start writing code that can be reused without opening files and changing strings every time. Passing inputs at runtime, handling limits properly, and structuring things like a real tool mattered to me.

More than anything, I wanted to build something small, real, and finished, not just another experiment that works once and gets abandoned.

What it can do

At a basic level, this scraper pulls public tweets from any username you give it. It loads the page properly, scrolls to fetch more tweets, and then extracts only the actual tweet text instead of all the surrounding UI noise.

It also grabs timestamps so the data is not just text without context. Once the scrape is done, everything is saved to a CSV file so you can inspect it, analyze it, or use it elsewhere.

Everything runs from the terminal. You pass the username, optionally pass a limit, and the scraper does the rest.

What it does not do

This scraper does not log into any accounts and it does not touch private profiles. It only works with what is already publicly visible on X.

There are no API keys involved and no attempt to pretend this is more stable than it really is. If X changes their layout in the future, some parts of this will need to be updated. That is just how scraping works.

This is scraping. Stuff breaks sometimes. That’s part of it.

Tech used

  • Python
  • Playwright
  • Pandas

I just used tools that get the job done.

Setup

Clone the repo.

    git clone https://github.com/calchiwo/twitter-scraper.git
    cd twitter-scraper

Install dependencies.

    python -m pip install -r requirements.txt
    python -m playwright install chromium

Usage

Run the example script and pass a username.

    python examples/scrape_user.py elonmusk

With a custom limit.

    python examples/scrape_user.py elonmusk 15

This creates a CSV file named after the username, for example elonmusk.csv.
CSV files are ignored by git and stay local.

Using it in your own code

You can also use it directly as a Python class.

    from twitter_scraper.scraper import TwitterScraper

    scraper = TwitterScraper()
    tweets = scraper.scrape_user("orcdev", limit=10)

    print(tweets)

Nothing runs on import. Also scraping only happens when you call the method.

Notes

X never becomes network idle, so this uses domcontentloaded.
Playwright launches a real browser.
The first run might feel slow. That’s normal.
If X changes their layout, selectors may need updates.

This is part of the game.

Disclaimer

This project is for educational and research purposes only.

Be responsible.
Respect platform rules.
Do not abuse it. If you use it for purposes like commercial uses you must inform.

Final thoughts

If you are learning scraping, packaging, or just want to understand how things work under the hood, feel free to explore the code.

If it helps you, cool.

If it breaks, fix it!. That’s the fun part tbh.

Byeeee.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twitterxscraper-1.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

twitterxscraper-1.1.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file twitterxscraper-1.1.1.tar.gz.

File metadata

  • Download URL: twitterxscraper-1.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for twitterxscraper-1.1.1.tar.gz
Algorithm Hash digest
SHA256 76382af457e8c2c896f794372e5038fc477e5ff7463e23e51b1c65c3c65a11c3
MD5 e840aa283d6ba813345a22f3cc794419
BLAKE2b-256 171245d97ab7ad9f81cdc4639bfe8f4a2d9e93a6fcd8683de05f703aba581ba0

See more details on using hashes here.

File details

Details for the file twitterxscraper-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for twitterxscraper-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 46dfcbc05b986e85705ea5be703a4e0739c153812ae7036277b95b8d6c461232
MD5 be5fdae13290df1c86223c34ab342b4f
BLAKE2b-256 2a5f1489a7848b0e0c3971f5c23bf6fa80972d084bd32ac588af24018ce71c0a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page