Skip to main content

x_browser_client

x_browser_client is a Python package that automates x.com (formerly Twitter) interactions using Playwright. This library can log in, post new tweets, scrape timelines and posts, reply, like, repost, quote, bookmark, and follow/unfollow accounts.

Features

  • Automatic or manual login to x.com
  • Posting and replying to tweets
  • Scraping timelines, individual posts, and comment threads
  • Liking, reposting, quoting, bookmarking tweets
  • Following and unfollowing users
  • Session management to persist cookies (via Playwright contexts)

Installation

Via PyPI

pip install x_browser_client

Via GitHub

git clone https://github.com/laelhalawani/x_browser_client
cd x_browser_client
pip install .

Note: If you have not installed Playwright browsers before, you may need to run:

playwright install chromium

This ensures that Playwright can automate the Chromium browser.

Requirements

Basic Usage

Below is a minimal example of how to use x_browser_client:

from x_browser_client import XClient, PostStatusData

# Create an XClient instance
client = XClient(
    timeout=60,         # default timeout (seconds) for browser interactions
    locale="en-US",     # language/locale
    post_character_limit=280
)

# Start the client, which:
# 1) Opens the browser
# 2) Loads or creates a session context if context_name is passed
# 3) Attempts login (auto or manual)
client.start_client(context_name="my_session", accept_cookies=True, skip_notifications=True)

# Scrape some of the latest posts from the 'for_you' tab
new_posts = client.scrape_new_posts(num_posts=5, tab="for_you")

# Iterate over posts and optionally interact
for post in new_posts:
    print(post)  # shows basic info about the post
    if post.has_text():
        # Like the post
        client.post_like(post)

# Exit the client, optionally saving context so next time it can reuse login
client.exit_client(save_context=True, close_browser_after=0)

Environment Variables

You can store your X credentials (login, password, username) in environment variables (e.g., in a .env file):

X_LOGIN=myEmailOrPhone
X_PASSWORD=mySecurePassword
X_USERNAME=myActualUsername

The XClient will automatically pick them up if you do not explicitly set them:

client = XClient()
client.start_client()

Additional Examples

Refer to examples.py or the repository's examples.py file for more in-depth usage, including:

  • Replying to a post with AI-generated content
  • Scraping specific post threads
  • Following or unfollowing a user
  • Manually controlling cookies and contexts

Contributing

Contributions are welcome! To set up a development environment:

  1. Clone the repository and install in editable mode:
    git clone https://github.com/laelhalawani/x_browser_client
    cd x_browser_client
    pip install -e .
    
  2. (Optional) Install additional dev dependencies for linting/testing as needed.

Please open issues or submit pull requests on GitHub.


Disclaimer: This package is intended solely for educational and testing purposes. Be aware of any usage constraints or policies on x.com, and ensure compliance with all terms of service and legal requirements.

Metadata

Release files for x-browser-client 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for x-browser-client 0.1.0
File Size Uploaded Provenance Actions
x_browser_client-0.1.0.tar.gz 21.9 kB Without provenance Details for x_browser_client-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for x-browser-client 0.1.0
File Interpreter ABI Platform Actions
x_browser_client-0.1.0-py3-none-any.whl Python 3 none any Details for x_browser_client-0.1.0-py3-none-any.whl

Total release size: 42.9 kB

Release files / x_browser_client-0.1.0.tar.gz

Download URL x_browser_client-0.1.0.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3d2d7f494cf9e21e408fea1514c021b202c8810a2dc3a36fed4d7c8fc7ab9e24
BLAKE2b-256 checksum
How to use checksums
d05b95ce7867660e9c42b51519b544bfc5714067dc15509739b59aa5544f6081
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / x_browser_client-0.1.0-py3-none-any.whl

Download URL x_browser_client-0.1.0-py3-none-any.whl
Size 21.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
371e55b419aa66c34f718c318d68243b6062140f598b4d37cb78cacff31f1c9d
BLAKE2b-256 checksum
How to use checksums
7854622f20df4b0c88f4a33eeaff99d913dd3911360d50f84a24481f04173f19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Security for x-browser-client 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page