MechanicalSoup was created by M Hickford, who was a fond user of the Mechanize library. Unfortunately, Mechanize is incompatible with Python 3 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_duck_duck_go.py, code to get the results from a DuckDuckGo search:
"""Example usage of MechanicalSoup to get the results from DuckDuckGo.""" import mechanicalsoup # Connect to duckduckgo browser = mechanicalsoup.StatefulBrowser() browser.open("https://duckduckgo.com/") # Fill-in the search form browser.select_form('#search_form_homepage') browser["q"] = "MechanicalSoup" browser.submit_selected() # Display the results for link in browser.get_current_page().select('a.result__a'): print(link.text, '->', link.attrs['href'])
More examples are available in examples/.
Instructions for building, testing and contributing to MechanicalSoup: see CONTRIBUTING.rst.
Read the FAQ.