mechanize

0.4.10 · maintenance · verified Thu Apr 16

mechanize provides a stateful, programmatic web browsing interface, allowing for opening URLs, following links, submitting forms, and handling cookies. It simulates a web browser's behavior without a GUI or JavaScript engine. The current version is 0.4.10, released in 2023, and it follows a slow release cadence, primarily for maintenance and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a mechanize Browser, configure it with a cookie jar and custom user-agent, and open a URL. It includes common settings like disabling robots.txt handling.

import mechanize
import http.cookiejar as cookielib

br = mechanize.Browser()

# Cookie Jar setup (optional but recommended for stateful browsing)
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_robots(False) # Often set to False for scraping

# User-Agent
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# Open a page
url = "http://www.example.com/"
# Use a placeholder for environment variables if authentication is needed
# url = os.environ.get('MECHANIZED_TARGET_URL', 'http://www.example.com/')

try:
    response = br.open(url)
    print(f"Title: {br.title()}")
    print(f"Status: {response.code}")
    # print(response.read().decode('utf-8'))
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →