Skip to main content

A Python library for automating interaction with websites

Project description


A Python library for automating interaction with websites. MechanicalSoup automatically stores and sends cookies, follows redirects, and can follow links and submit forms. It doesn't do Javascript.

I was a fond user of the [Mechanize]( library, but unfortunately it's [incompatible with Python 3]( and development is inactive. MechanicalSoup provides a similar API, built on Python giants [Requests]( (for http sessions) and [BeautifulSoup]( (for document navigation).


[![Latest Version](](

From [PyPI](

pip install MechanicalSoup

Python versions 2.6-2.7, 3.3-3.6, PyPy and PyPy3 are supported (and tested against).


From [``](, code to log into the GitHub website:

"""Example app to login to GitHub using the StatefulBrowser class."""

from __future__ import print_function
import argparse
import mechanicalsoup
from getpass import getpass

parser = argparse.ArgumentParser(description="Login to GitHub.")
args = parser.parse_args()

args.password = getpass("Please enter your GitHub password: ")

browser = mechanicalsoup.StatefulBrowser()
# Uncomment for a more verbose output:
# browser.set_verbose(2)"")
browser.select_form('#login form')
browser["login"] = args.username
browser["password"] = args.password
resp = browser.submit_selected()

# Uncomment to launch a web browser on the current page:
# browser.launch_browser()

# verify we are now logged in
page = browser.get_current_page()
messages = page.find("div", class_="flash-messages")
if messages:


# verify we remain logged in (thanks to cookies) as we browse the rest of
# the site
page3 ="")

For an example with a more complex form (checkboxes, radio buttons and textareas), read [`tests/`](tests/ and [`tests/`](tests/

Common problems

### "No parser was explicitly specified"

> UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

Recent versions of BeautifulSoup show a harmless warning to encourage you to specify which HTML parser to use. You can do this in MechanicalSoup:


Or if you have the parser [lxml]( installed:


See also


[![Build Status](](

### Tests


### Roadmap

* Draw [Substack-style]( readme art (imagine a steaming bowl of cogs and noodles)
* [Write docs and publish website](

See also

* [RoboBrowser]( a similar library, also based on Requests and BeautifulSoup.
* [Hacker News post](
* [Reddit discussion](

Project details

Download files

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

Files for MechanicalSoup, version 0.7.0
Filename, size File type Python version Upload date Hashes
Filename, size MechanicalSoup-0.7.0.tar.gz (10.6 kB) File type Source Python version None Upload date Hashes View
Filename, size MechanicalSoup-0.7.0-py2.py3-none-any.whl (7.6 kB) File type Wheel Python version 2.7 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page