Yes, MechanicalSoup is compatible with Python 3. MechanicalSoup is a Python library for automating interaction with websites. It provides a simple API for navigating and manipulating web pages, which is especially useful for web scraping. MechanicalSoup acts as a wrapper around the libraries Requests and BeautifulSoup, both of which are also compatible with Python 3.
MechanicalSoup officially supports Python 3.4 and above, as mentioned in its documentation and GitHub repository. To install MechanicalSoup for use with Python 3, you can use pip
, the package installer for Python. You can install it directly from the command line with the following command:
pip3 install MechanicalSoup
Or, if you have multiple versions of Python installed and pip
is linked to Python 3 version:
pip install MechanicalSoup
Here is an example of how to use MechanicalSoup with Python 3 to log in to a website and get the contents of a page:
import mechanicalsoup
# Create a browser object
browser = mechanicalsoup.StatefulBrowser()
# Open the login page
browser.open("https://example.com/login")
# Find the login form and fill in the username and password fields
browser.select_form('form[id="loginForm"]')
browser["username"] = "your_username"
browser["password"] = "your_password"
# Submit the form
response = browser.submit_selected()
# Check if login was successful
if response.url == "https://example.com/profile":
# Login successful, proceed to scrape
print("Logged in successfully")
page = browser.get_current_page()
# Do something with `page` which is a BeautifulSoup object
else:
print("Login failed")
# Always be sure to close the browser session
browser.close()
Remember to replace https://example.com/login
, your_username
, and your_password
with the actual URL and credentials. The form selection form[id="loginForm"]
should also be adjusted to match the structure of the specific website you are trying to automate.
MechanicalSoup is a great choice for scripts and projects that need simple browser automation without JavaScript execution support. If you need to interact with a website that relies heavily on JavaScript, you might want to consider using Selenium or a headless browser like Puppeteer for Node.js.