Python Mechanize Cheat Sheet | Mechanize 功能速查表



A very useful python module for navigating through web forms is Mechanize.
In a previous post I wrote about “Browsing in Python with Mechanize”.
Today I found this excellent cheat sheet on scraperwiki that I would like to share.


Create a browser object

Create a browser object and give it some optional settings.


Open a webpage

Open a webpage and inspect its contents


Using forms

List the forms that are in the page

To go on the mechanize browser object must have a form selected


Using Controls

Iterate through the controls in the form.

Controls can be found by name

Having a select control tells you what values can be selected

Because ‘Select’ type controls can have multiple selections, they must be set
with a list, even if it is one element.

Text controls can be set as a string

Controls can be set to readonly and disabled.

OR disable all of them like so


Submit the form

When your form is complete you can submit


Finding Links

Following links in mechanize is a hassle because you need the have the link
Sometimes it is easier to get them all and find the link you want from the text.

Follow link and click links is the same as submit and click

I hope that you got more understanding of the Mechanize module in Python.


您的电子邮箱地址不会被公开。 必填项已用*标注