{"library":"mechanicalsoup","title":"MechanicalSoup","description":"MechanicalSoup is a Python library for automating interaction with websites. It builds on top of `requests` and `BeautifulSoup4` to provide a stateful browser experience, making it easy to navigate, fill forms, and submit data without a full-fledged browser. The current version is 1.4.0, and it maintains a moderate release cadence, typically releasing minor versions every 6-12 months with occasional patch releases.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install mechanicalsoup","pip install mechanicalsoup[full]"],"cli":null},"imports":["from mechanicalsoup import StatefulBrowser","from mechanicalsoup import Browser","from mechanicalsoup import Form"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import mechanicalsoup\nimport os\n\n# Create a headless browser instance\nbrowser = mechanicalsoup.StatefulBrowser()\n\n# Open a page (replace with a real URL for testing, e.g., a login page)\n# For a test, we'll use a mock login setup\n# In a real scenario, you'd open a target URL:\n# browser.open(\"http://example.com/login\")\n\n# Simulate a simple HTML page with a form\n# For demonstration, we'll parse a string. In reality, browser.open() returns a response.\nhtml_content = '''\n<html><body>\n  <form action=\"/login\" method=\"post\">\n    <input type=\"text\" name=\"username\" value=\"\">\n    <input type=\"password\" name=\"password\" value=\"\">\n    <input type=\"submit\" value=\"Login\">\n  </form>\n</body></html>\n'''\nbrowser.set_content(html_content)\n\n# Select the form (by index or CSS selector)\nbrowser.select_form('form[action=\"/login\"]')\n\n# Fill in the form fields\nbrowser[\"username\"] = os.environ.get('TEST_USERNAME', 'testuser')\nbrowser[\"password\"] = os.environ.get('TEST_PASSWORD', 'testpass')\n\n# Submit the form\n# In a real scenario, this would send the request to the action URL\n# response = browser.submit_selected()\n\nprint(f\"Form selected: {browser.form}\")\nprint(f\"Username field value: {browser['username']}\")\nprint(f\"Password field value: {browser['password']}\")\n# print(f\"Response URL after submission: {browser.url}\")\n# print(f\"Response content: {browser.page.text}\")\n","lang":"python","description":"This quickstart demonstrates how to initialize a `StatefulBrowser`, set its content (or open a URL), select a form, fill its fields, and prepare to submit it. For actual interaction with a website, replace the `set_content` call with `browser.open(\"http://your.site/login\")` and uncomment the submission and response handling lines.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}