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.0.tar.gz (5.7 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.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: twitterxscraper-1.1.0.tar.gz
  • Upload date:
  • Size: 5.7 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.0.tar.gz
Algorithm Hash digest
SHA256 1e081742623867f3b4c9a53eb155ff39300e04102ab6e84847e01a9986f194f7
MD5 e343de2cdf049ad98e21ad1e26b0294b
BLAKE2b-256 5cc129bc2598fb65c3f113c30e759ac68ebb185447ade737abca20f13f6eda18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twitterxscraper-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9162f5dc8a2526c020d06664c1c64df20e57ddb1a29ed908d958b970bd1438c3
MD5 84ee51421653de231d222c7204220f98
BLAKE2b-256 7433d90e35deb12166684595bae963e5d0388f5f78e477a1586261e4251b5617

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