A Python library for automating interaction with websites
MechanicalSoup was created by M Hickford, who was a fond user of the Mechanize library. Unfortunately, Mechanize was incompatible with Python 3 until 2019 and its development stalled for several years. MechanicalSoup provides a similar API, built on Python giants Requests (for HTTP sessions) and BeautifulSoup (for document navigation). Since 2017 it is a project actively maintained by a small team including @hemberger and @moy.
PyPy and PyPy3 are also supported (and tested against).
Download and install the latest released version from PyPI:
pip install MechanicalSoup
Download and install the development version from GitHub:
pip install git+https://github.com/MechanicalSoup/MechanicalSoup
Installing from source (installs the version in the current working directory):
python setup.py install
(In all cases, add --user to the install command to install in the current user’s home directory.)
From examples/expl_qwant.py, code to get the results from a Qwant search:
"""Example usage of MechanicalSoup to get the results from the Qwant search engine. """ import re import mechanicalsoup import html import urllib.parse # Connect to duckduckgo browser = mechanicalsoup.StatefulBrowser(user_agent='MechanicalSoup') browser.open("https://lite.qwant.com/") # Fill-in the search form browser.select_form('#search-form') browser["q"] = "MechanicalSoup" browser.submit_selected() # Display the results for link in browser.page.select('.result a'): # Qwant shows redirection links, not the actual URL, so extract # the actual URL from the redirect link: href = link.attrs['href'] m = re.match(r"^/redirect/[^/]*/(.*)$", href) if m: href = urllib.parse.unquote(m.group(1)) print(link.text, '->', href)
More examples are available in examples/.
Instructions for building, testing and contributing to MechanicalSoup: see CONTRIBUTING.rst.
Read the FAQ.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size MechanicalSoup-1.0.0-py2.py3-none-any.whl (18.8 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
|Filename, size MechanicalSoup-1.0.0.tar.gz (47.6 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for MechanicalSoup-1.0.0-py2.py3-none-any.whl