Skip to main content

A Python package for interacting with X.com using Playwright-powered automation. Meant for use with AI agents. Requires a windowed environment, optimally Ubuntu or Windows.

Project description

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.

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

x_browser_client-0.1.0.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

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

x_browser_client-0.1.0-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file x_browser_client-0.1.0.tar.gz.

File metadata

  • Download URL: x_browser_client-0.1.0.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for x_browser_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3d2d7f494cf9e21e408fea1514c021b202c8810a2dc3a36fed4d7c8fc7ab9e24
MD5 9baea3f74efeca31d3407cfcbedaf60d
BLAKE2b-256 d05b95ce7867660e9c42b51519b544bfc5714067dc15509739b59aa5544f6081

See more details on using hashes here.

File details

Details for the file x_browser_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for x_browser_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 371e55b419aa66c34f718c318d68243b6062140f598b4d37cb78cacff31f1c9d
MD5 364f707b0e8dcb1453f6d2c52f9f78d7
BLAKE2b-256 7854622f20df4b0c88f4a33eeaff99d913dd3911360d50f84a24481f04173f19

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