You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
Installation
PyPy3 is 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:
"""Example usage of MechanicalSoup to get the results from the Qwantsearch engine."""importreimportmechanicalsoupimporthtmlimporturllib.parse# Connect to Qwantbrowser=mechanicalsoup.StatefulBrowser(user_agent='MechanicalSoup')
browser.open("https://lite.qwant.com/")
# Fill-in the search formbrowser.select_form('#search-form')
browser["q"] ="MechanicalSoup"browser.submit_selected()
# Display the resultsforlinkinbrowser.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)
ifm:
href=urllib.parse.unquote(m.group(1))
print(link.text, '->', href)