MechanicalSoup

1.4.0 · active · verified Fri Apr 17

MechanicalSoup is a Python library for automating interaction with websites. It builds on top of `requests` and `BeautifulSoup4` to provide a stateful browser experience, making it easy to navigate, fill forms, and submit data without a full-fledged browser. The current version is 1.4.0, and it maintains a moderate release cadence, typically releasing minor versions every 6-12 months with occasional patch releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `StatefulBrowser`, set its content (or open a URL), select a form, fill its fields, and prepare to submit it. For actual interaction with a website, replace the `set_content` call with `browser.open("http://your.site/login")` and uncomment the submission and response handling lines.

import mechanicalsoup
import os

# Create a headless browser instance
browser = mechanicalsoup.StatefulBrowser()

# Open a page (replace with a real URL for testing, e.g., a login page)
# For a test, we'll use a mock login setup
# In a real scenario, you'd open a target URL:
# browser.open("http://example.com/login")

# Simulate a simple HTML page with a form
# For demonstration, we'll parse a string. In reality, browser.open() returns a response.
html_content = '''
<html><body>
  <form action="/login" method="post">
    <input type="text" name="username" value="">
    <input type="password" name="password" value="">
    <input type="submit" value="Login">
  </form>
</body></html>
'''
browser.set_content(html_content)

# Select the form (by index or CSS selector)
browser.select_form('form[action="/login"]')

# Fill in the form fields
browser["username"] = os.environ.get('TEST_USERNAME', 'testuser')
browser["password"] = os.environ.get('TEST_PASSWORD', 'testpass')

# Submit the form
# In a real scenario, this would send the request to the action URL
# response = browser.submit_selected()

print(f"Form selected: {browser.form}")
print(f"Username field value: {browser['username']}")
print(f"Password field value: {browser['password']}")
# print(f"Response URL after submission: {browser.url}")
# print(f"Response content: {browser.page.text}")

view raw JSON →