MECHANIZE(1) | mechanize | MECHANIZE(1) |
mechanize - mechanize Documentation
Stateful programmatic web browsing in Python. Browse pages programmatically with easy HTML form filling and clicking of links.
mechanize works on all python versions, python 2 (>= 2.7) and 3 (>= 3.5).
html5lib
mechanize is licensed under the BSD-3-clause license.
See debugging.
No, sorry. See JavaScript is messing up my web-scraping. What do I do?
mechanize.Browser's response objects support the .seek() method, and can still be used after .close() has been called. Response data is not fetched until it is needed, so navigation away from a URL before fetching all of the response will truncate it. Call response.get_data() before navigation if you don't want that to happen.
Look in the examples/ directory. Note that the examples on the forms page are executable as-is. Contributions of example code would be very welcome!
Netscape and RFC 2965. RFC 2965 handling is switched off by default.
RFC 2109 cookies are currently parsed as Netscape cookies, and treated by default as RFC 2965 cookies thereafter if RFC 2965 handling is enabled, or as Netscape cookies otherwise.
See cookies.
Did you call response.read() (e.g., in a debug statement), then forget that all the data has already been read? In that case, you may want to use mechanize.response_seek_wrapper. mechanize.Browser always returns seekable responses, so it's not necessary to use this explicitly in that case.
.load() appends cookies from a file. .revert() discards all existing cookies held by the CookieJar first (but it won't lose any existing cookies if the loading fails).
See threading.
Refer to the API documentation in browser_api.
print(form) is usually all you need. In your code, things like the HTMLForm.items attribute of mechanize.HTMLForm instances can be useful to inspect forms at runtime. Note that it's possible to use item labels instead of item names, which can be useful — use the by_label arguments to the various methods, and the .get_value_by_label() / .set_value_by_label() methods on ListControl.
A * next to an item means that item is selected.
Parentheses (foo) around an item mean that item is disabled.
Either the control is disabled, or it is not successful for some other reason. 'Successful' (see HTML 4 specification) means that the control will cause data to get sent to the server.
Because by default, it follows browser behaviour when setting the initially-selected items in list controls that have no items explicitly selected in the HTML.
Clicking on a RESET button doesn't do anything, by design - this is a library for web automation, not an interactive browser. Even in an interactive browser, clicking on RESET sends nothing to the server, so there is little point in having .click() do anything special here.
Clicking on a BUTTON TYPE=BUTTON doesn't do anything either, also by design. This time, the reason is that that BUTTON is only in the HTML standard so that one can attach JavaScript callbacks to its events. Their execution may result in information getting sent back to the server. mechanize, however, knows nothing about these callbacks, so it can't do anything useful with a click on a BUTTON whose type is BUTTON.
Generally, JavaScript may be messing things up in all kinds of ways. See JavaScript is messing up my web-scraping. What do I do?.
As with any control, set the control's readonly attribute false.
form.find_control("foo").readonly = False # allow changing .value of control foo form.set_all_readonly(False) # allow changing the .value of all controls
See debugging.
import bisect def closest_int_value(form, ctrl_name, value):
values = map(int, [item.name for item in form.find_control(ctrl_name).items])
return str(values[bisect.bisect(values, value) - 1]) form["distance"] = [closest_int_value(form, "distance", 23)]
Use the developer tools for your browser (you may have to install them first). These provide excellent views into all HTTP requests/responses in the browser.
JavaScript is used in web pages for many purposes -- for example: creating content that was not present in the page at load time, submitting or filling in parts of forms in response to user actions, setting cookies, etc. mechanize does not provide any support for JavaScript.
If you come across this in a page you want to automate, you have a few options. Here they are, roughly in order of simplicity:
API documentation for the mechanize Browser object. You can create a mechanize Browser instance as:
from mechanize import Browser br = Browser()
BrowserStateError is raised whenever the browser is in the wrong state to complete the requested operation - e.g., when back() is called when the browser history is empty, or when follow_link() is called when the current response does not contain HTML data.
Public attributes:
request: current request (mechanize.Request)
form: currently selected form (see select_form())
key_file and cert_file must be filenames of the key and certificate files, in PEM format. You can use e.g. OpenSSL to convert a p12 (PKCS 12) file to PEM format:
openssl pkcs12 -clcerts -nokeys -in cert.p12 -out cert.pem openssl pkcs12 -nocerts -in cert.p12 -out key.pem
Note that client certificate password input is very inflexible ATM. At the moment this seems to be console only, which is presumably the default behaviour of libopenssl. In future mechanize may support third-party libraries that (I assume) allow more options here.
n: go back this number of steps (default 1 step)
Arguments are as for find_link(), except that a link may be supplied as the first argument.
Links are returned as mechanize.Link objects. Examples:
# Return third link that .search()-matches the regexp "python" (by # ".search()-matches", I mean that the regular expression method # .search() is used, rather than .match()). find_link(text_regex=re.compile("python"), nr=2) # Return first http link in the current page that points to # somewhere on python.org whose link text (after tags have been # removed) is exactly "monty python". find_link(text="monty python",
url_regex=re.compile("http.*python.org")) # Return first link with exactly three HTML attributes. find_link(predicate=lambda link: len(link.attrs) == 3)
Links include anchors <a>, image maps <area>, and frames <iframe>.
All arguments must be passed by keyword, not position. Zero or more arguments may be supplied. In order to find a link, all arguments supplied must match.
If a matching link is not found, mechanize.LinkNotFoundError is raised.
Arguments are as for click_link().
Return value is same as for open().
The returned form objects implement the mechanize.HTMLForm interface.
The "global" form object contains all controls that are not descendants of any FORM element.
The returned form object implements the mechanize.HTMLForm interface.
This is a separate method since the global form is not regarded as part of the sequence of forms in the document -- mostly for backwards-compatibility.
Browser state (including request, response, history, forms and links) is left unchanged by calling this function.
The interface is the same as for open().
This is useful for things like fetching images.
See also retrieve()
The returned object has the same interface as the object returned by open()
For remote objects, the default filename will refer to a temporary file. Temporary files are removed when the OpenerDirector.close() method is called.
For file: URLs, at present the returned filename is None. This may change in future.
If the actual number of bytes read is less than indicated by the Content-Length header, raises ContentTooShortError (a URLError subclass). The exception's .result attribute contains the (filename, headers) that would have been returned.
This is a bit like giving a form the "input focus" in a browser.
If a form is selected, the Browser object supports the HTMLForm interface, so you can call methods like set_value(), set(), and click().
Another way to select a form is to assign to the .form attribute. The form assigned should be one of the objects returned by the forms() method.
If no matching form is found, mechanize.FormNotFoundError is raised.
If name is specified, then the form must have the indicated name.
If predicate is specified, then the form must match that function. The predicate function is passed the mechanize.HTMLForm as its single argument, and should return a boolean value indicating whether the form matched.
nr, if supplied, is the sequence number of the form (where 0 is the first). Note that control 0 is the first form matching all the other arguments (if supplied); it is not necessarily the first control in the form. The "global form" (consisting of all form controls not contained in any FORM element) is considered not to be part of this sequence and to have no name, so will not be matched unless both name and nr are None.
You can also match on any HTML attribute of the <form> tag by passing in the attribute name and value as keyword arguments. To convert HTML attributes into syntactically valid python keyword arguments, the following simple rule is used. The python keyword argument name is converted to an HTML attribute name by: Replacing all underscores with hyphens and removing any trailing underscores. You can pass in strings, functions or regular expression objects as the values to match. For example:
# Match form with the exact action specified br.select_form(action='http://foo.com/submit.php') # Match form with a class attribute that contains 'login' br.select_form(class_=lambda x: 'login' in x) # Match form with a data-form-type attribute that matches a regex br.select_form(data_form_type=re.compile(r'a|b'))
This method accepts the same arguments as the ssl.SSLContext.load_verify_locations() method from the python standard library. You can also pass a pre-built context via the context keyword argument. Note that to use this feature, you must be using python >= 2.7.9. In addition you can directly pass in a pre-built ssl.SSLContext as the context argument.
Note that it is NOT necessary to call this method under ordinary circumstances: cookie handling is normally entirely automatic. The intended use case is rather to simulate the setting of a cookie by client script in a web page (e.g. JavaScript). In that case, use of this method is necessary because mechanize currently does not support JavaScript, VBScript, etc.
The cookie is added in the same way as if it had arrived with the current response, as a result of the current request. This means that, for example, if it is not appropriate to set the cookie based on the current request, no cookie will be set.
The cookie will be returned automatically with subsequent responses made by the Browser instance whenever that's appropriate.
cookie_string should be a valid value of the Set-Cookie header.
For example:
browser.set_cookie(
"sid=abcdef; expires=Wednesday, 09-Nov-06 23:12:40 GMT")
Currently, this method does not allow for adding RFC 2986 cookies. This limitation will be lifted if anybody requests it.
See also set_simple_cookie() for an easier way to set cookies without needing to create a Set-Cookie header string.
Logging is performed using module logging. The logger name is "mechanize.http_redirects". To actually print some debug output, eg:
import sys, logging logger = logging.getLogger("mechanize.http_redirects") logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.INFO)
Other logger names relevant to this module:
To turn on everything:
import sys, logging logger = logging.getLogger("mechanize") logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.INFO)
See set_debug_redirects() for details of logging.
Response objects may be .seek()able if this is set (currently returned responses are, raised HTTPError exception responses are not).
Response objects may be .seek()able if this is set (currently returned responses are, raised HTTPError exception responses are not).
For example: ua.set_handled_schemes(["http", "ftp"])
If this fails (with ValueError) because you've passed an unknown scheme, the set of handled schemes will not be changed.
Allows you to then parse that HTML, especially to extract forms information. If no URL was given then the default is "example.com".
The default is to try to obtain proxy settings from the system (see the documentation for urllib.urlopen for information about the system-specific methods used -- note that's urllib, not urllib2).
To avoid all use of proxies, pass an empty proxies dict.
>>> ua = UserAgentBase() >>> def proxy_bypass(hostname): ... return hostname == "noproxy.com" >>> ua.set_proxies( ... {"http": "joe:password@myproxy.example.com:3128", ... "ftp": "proxy.example.com"}, ... proxy_bypass)
response may be None.
This is intended mostly for HTML-preprocessing.
browser.set_simple_cookie('some_key', 'some_value', '.example.com',
path='/some-page')
Arguments are as for mechanize.HTMLForm.click().
Return value is same as for open().
Unlike set_response(), this updates history rather than replacing the current response.
The remaining arguments are for internal use.
Response objects in mechanize are seek() able file-like objects that support some additional methods, depending on the protocol used for the connection. The documentation below is for HTTP(s) responses, as these are the most common.
Additional methods present for HTTP responses:
Forms in HTML documents are represented by mechanize.HTMLForm. Every form is a collection of controls. The different types of controls are represented by the various classes documented below.
Represents a single HTML <form> ... </form> element.
A form consists of a sequence of controls that usually have names, and which can take on various values. The values of the various types of controls represent variously: text, zero-or-one-of-many or many-of-many choices, and files to be uploaded. Some controls can be clicked on to submit the form, and clickable controls' values sometimes include the coordinates of the click.
Forms can be filled in with data to be returned to the server, and then submitted, using the click method to generate a request object suitable for passing to mechanize.urlopen() (or the click_request_data or click_pairs methods for integration with third-party code).
Usually, HTMLForm instances are not created directly. Instead, they are automatically created when visting a page with a mechanize Browser. If you do construct HTMLForm objects yourself, however, note that an HTMLForm instance is only properly initialised after the fixup method has been called. See mechanize.ListControl for the reason this is required.
Indexing a form (form["control_name"]) returns the named Control's value attribute. Assignment to a form index (form["control_name"] = something) is equivalent to assignment to the named Control's value attribute. If you need to be more specific than just supplying the control's name, use the set_value and get_value methods.
ListControl values are lists of item names (specifically, the names of the items that are selected and not disabled, and hence are "successful" -- ie. cause data to be returned to the server). The list item's name is the value of the corresponding HTML element's"value" attribute.
Example:
<INPUT type="CHECKBOX" name="cheeses" value="leicester"></INPUT> <INPUT type="CHECKBOX" name="cheeses" value="cheddar"></INPUT>
defines a CHECKBOX control with name "cheeses" which has two items, named "leicester" and "cheddar".
Another example:
<SELECT name="more_cheeses">
<OPTION>1</OPTION>
<OPTION value="2" label="CHEDDAR">cheddar</OPTION> </SELECT>
defines a SELECT control with name "more_cheeses" which has two items, named "1" and "2" (because the OPTION element's value HTML attribute defaults to the element contents -- see mechanize.SelectControl for more on these defaulting rules).
To select, deselect or otherwise manipulate individual list items, use the mechanize.HTMLForm.find_control() and mechanize.ListControl.get() methods. To set the whole value, do as for any other control: use indexing or the set_value/get_value methods.
Example:
# select *only* the item named "cheddar" form["cheeses"] = ["cheddar"] # select "cheddar", leave other items unaffected form.find_control("cheeses").get("cheddar").selected = True
Some controls (RADIO and SELECT without the multiple attribute) can only have zero or one items selected at a time. Some controls (CHECKBOX and SELECT with the multiple attribute) can have multiple items selected at a time. To set the whole value of a ListControl, assign a sequence to a form index:
form["cheeses"] = ["cheddar", "leicester"]
If the ListControl is not multiple-selection, the assigned list must be of length one.
To check if a control has an item, if an item is selected, or if an item is successful (selected and not disabled), respectively:
"cheddar" in [item.name for item in form.find_control("cheeses").items] "cheddar" in [item.name for item in form.find_control("cheeses").items
and item.selected] "cheddar" in form["cheeses"] # or "cheddar" in form.get_value("cheeses")
Note that some list items may be disabled (see below).
Note the following mistake:
form[control_name] = control_value assert form[control_name] == control_value # not necessarily true
The reason for this is that form[control_name] always gives the list items in the order they were listed in the HTML.
List items (hence list values, too) can be referred to in terms of list item labels rather than list item names using the appropriate label arguments. Note that each item may have several labels.
The question of default values of OPTION contents, labels and values is somewhat complicated: see mechanize.SelectControl and mechanize.ListControl.get_item_attrs() if you think you need to know.
Controls can be disabled or readonly. In either case, the control's value cannot be changed until you clear those flags (see example below). Disabled is the state typically represented by browsers by 'greying out' a control. Disabled controls are not 'successful' -- they don't cause data to get returned to the server. Readonly controls usually appear in browsers as read-only text boxes. Readonly controls are successful. List items can also be disabled. Attempts to select or deselect disabled items fail with AttributeError.
If a lot of controls are readonly, it can be useful to do this:
form.set_all_readonly(False)
To clear a control's value attribute, so that it is not successful (until a value is subsequently set):
form.clear("cheeses")
More examples:
control = form.find_control("cheeses") control.disabled = False control.readonly = False control.get("gruyere").disabled = True control.items[0].selected = True
See the various Control classes for further documentation. Many methods take name, type, kind, id, label and nr arguments to specify the control to be operated on: see mechanize.HTMLForm.find_control().
ControlNotFoundError (subclass of ValueError) is raised if the specified control can't be found. This includes occasions where a non-ListControl is found, but the method (set, for example) requires a ListControl. ItemNotFoundError (subclass of ValueError) is raised if a list item can't be found. ItemCountError (subclass of ValueError) is raised if an attempt is made to select more than one item and the control doesn't allow that, or set/get_single are called and the control contains more than one item. AttributeError is raised if a control or item is readonly or disabled and an attempt is made to alter its value.
Security note: Remember that any passwords you store in HTMLForm instances will be saved to disk in the clear if you pickle them (directly or indirectly). The simplest solution to this is to avoid pickling HTMLForm objects. You could also pickle before filling in any password, or just set the password to "" before pickling.
Public attributes:
Methods for form filling:
Most of the these methods have very similar arguments. See mechanize.HTMLForm.find_control() for details of the name, type, kind, label and nr arguments.
def find_control(self,
name=None, type=None, kind=None, id=None,
predicate=None, nr=None, label=None) get_value(name=None, type=None, kind=None, id=None, nr=None,
by_label=False, # by_label is deprecated
label=None) set_value(value,
name=None, type=None, kind=None, id=None, nr=None,
by_label=False, # by_label is deprecated
label=None) clear_all() clear(name=None, type=None, kind=None, id=None, nr=None, label=None) set_all_readonly(readonly)
Method applying only to FileControls:
add_file(file_object,
content_type="application/octet-stream", filename=None,
name=None, id=None, nr=None, label=None)
Methods applying only to clickable controls:
click(name=None, type=None, id=None, nr=0, coord=(1,1), label=None) click_request_data(name=None, type=None, id=None, nr=0, coord=(1,1),
label=None) click_pairs(name=None, type=None, id=None, nr=0, coord=(1,1),
label=None)
If filename is None, no filename is sent to the server.
If content_type is None, the content type is guessed based on the filename and the data from read from the file object.
At the moment, guessed content type is always application/octet-stream.
Note the following useful HTML attributes of file upload controls (see HTML 4.01 spec, section 17):
As a result, the affected control will not be successful until a value is subsequently set. AttributeError is raised on readonly controls.
See mechanize.HTMLForm.clear()
The request object is a mechanize.Request instance, which you can pass to mechanize.urlopen.
Only some control types (INPUT/SUBMIT & BUTTON/SUBMIT buttons and IMAGEs) can be clicked.
Will click on the first clickable control, subject to the name, type and nr arguments (as for find_control). If no name, type, id or number is specified and there are no clickable controls, a request will be returned for the form in its current, un-clicked, state.
IndexError is raised if any of name, type, id or nr is specified but no matching control is found. ValueError is raised if the HTMLForm has an enctype attribute that is not recognised.
You can optionally specify a coordinate to click at, which only makes a difference if you clicked on an image.
You can use this list as an argument to urllib.urlencode. This is usually only useful if you're using httplib or urllib rather than mechanize. It may also be useful if you want to manually tweak the keys and/or values, but this should not be necessary. Otherwise, use the click method.
Note that this method is only useful for forms of MIME type x-www-form-urlencoded. In particular, it does not return the information required for file upload. If you need file upload and are not using mechanize, use click_request_data.
You can use this data to send a request to the server. This is useful if you're using httplib or urllib rather than mechanize. Otherwise, use the click method.
At least one of the name, type, kind, predicate and nr arguments must be supplied. If no matching control is found, ControlNotFoundError is raised.
If name is specified, then the control must have the indicated name.
If type is specified then the control must have the specified type (in addition to the types possible for <input> HTML tags: "text", "password", "hidden", "submit", "image", "button", "radio", "checkbox", "file" we also have "reset", "buttonbutton", "submitbutton", "resetbutton", "textarea", "select").
If kind is specified, then the control must fall into the specified group, each of which satisfies a particular interface. The types are "text", "list", "multilist", "singlelist", "clickable" and "file".
If id is specified, then the control must have the indicated id.
If predicate is specified, then the control must match that function. The predicate function is passed the control as its single argument, and should return a boolean value indicating whether the control matched.
nr, if supplied, is the sequence number of the control (where 0 is the first). Note that control 0 is the first control matching all the other arguments (if supplied); it is not necessarily the first control in the form. If no nr is supplied, AmbiguityError is raised if multiple controls match the other arguments.
If label is specified, then the control must have this label. Note that radio controls and checkboxes never have labels: their items do.
This is usually called by ParseFile and ParseResponse. Don't call it youself unless you're building your own Control instances.
This method should only be called once, after all controls have been added to the form.
If only name and value arguments are supplied, equivalent to
form[name]
This is usually called by mechanize. Don't call it yourself unless you're building your own Control instances.
Note that controls representing lists of items are built up from controls holding only a single list item. See mechanize.ListControl for further information.
If the control has multiple list items, ItemCountError is raised.
This is just a convenience method, so you don't need to know the item's name -- the item name in these single-item controls is usually something meaningless like "1" or "on".
For example, if a checkbox has a single item named "on", the following two calls are equivalent:
control.toggle("on") control.toggle_single()
If only name and value arguments are supplied, equivalent to
form[name] = value
The rest is as for mechanize.HTMLForm.set_single()
An HTML form control.
An HTMLForm contains a sequence of Controls. The Controls in an HTMLForm are accessed using the HTMLForm.find_control method or the HTMLForm.controls attribute.
Control instances are usually constructed using the ParseFile / ParseResponse functions. If you use those functions, you can ignore the rest of this paragraph. A Control is only properly initialised after the fixup method has been called. In fact, this is only strictly necessary for ListControl instances. This is necessary because ListControls are built up from ListControls each containing only a single item, and their initial value(s) can only be known after the sequence is complete.
The types and values that are acceptable for assignment to the value attribute are defined by subclasses.
If the disabled attribute is true, this represents the state typically represented by browsers by 'greying out' a control. If the disabled attribute is true, the Control will raise AttributeError if an attempt is made to change its value. In addition, the control will not be considered 'successful' as defined by the W3C HTML 4 standard -- ie. it will contribute no data to the return value of the HTMLForm.click* methods. To enable a control, set the disabled attribute to a false value.
If the readonly attribute is true, the Control will raise AttributeError if an attempt is made to change its value. To make a control writable, set the readonly attribute to a false value.
All controls have the disabled and readonly attributes, not only those that may have the HTML attributes of the same names.
On assignment to the value attribute, the following exceptions are raised: TypeError, AttributeError (if the value attribute should not be assigned to, because the control is disabled, for example) and ValueError.
If the name or value attributes are None, or the value is an empty list, or if the control is disabled, the control is not successful.
Public attributes:
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
Control whose value is not restricted to one of a prescribed set.
Some ScalarControls don't accept any value attribute. Otherwise, takes a single value, which must be string-like.
Additional read-only public attribute:
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
Textual input control.
Covers HTML elements: INPUT/TEXT, INPUT/PASSWORD, INPUT/HIDDEN, TEXTAREA
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
File upload with INPUT TYPE=FILE.
The value attribute of a FileControl is always None. Use add_file instead.
Additional public method: add_file()
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
Control that we're not interested in.
Covers html elements: INPUT/RESET, BUTTON/RESET, INPUT/BUTTON, BUTTON/BUTTON
These controls are always unsuccessful, in the terminology of HTML 4 (ie. they never require any information to be returned to the server).
BUTTON/BUTTON is used to generate events for script embedded in HTML.
The value attribute of IgnoreControl is always None.
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
Control representing a sequence of items.
The value attribute of a ListControl represents the successful list items in the control. The successful list items are those that are selected and not disabled.
ListControl implements both list controls that take a length-1 value (single-selection) and those that take length >1 values (multiple-selection).
ListControls accept sequence values only. Some controls only accept sequences of length 0 or 1 (RADIO, and single-selection SELECT). In those cases, ItemCountError is raised if len(sequence) > 1. CHECKBOXes and multiple-selection SELECTs (those having the "multiple" HTML attribute) accept sequences of any length.
Note the following mistake:
control.value = some_value assert control.value == some_value # not necessarily true
The reason for this is that the value attribute always gives the list items in the order they were listed in the HTML.
ListControl items can also be referred to by their labels instead of names. Use the label argument to .get(), and the .set_value_by_label(), .get_value_by_label() methods.
Note that, rather confusingly, though SELECT controls are represented in HTML by SELECT elements (which contain OPTION elements, representing individual list items), CHECKBOXes and RADIOs are not represented by any element. Instead, those controls are represented by a collection of INPUT elements. For example, this is a SELECT control, named "control1":
<select name="control1"> <option>foo</option> <option value="1">bar</option> </select>
and this is a CHECKBOX control, named "control2":
<input type="checkbox" name="control2" value="foo" id="cbe1"> <input type="checkbox" name="control2" value="bar" id="cbe2">
The id attribute of a CHECKBOX or RADIO ListControl is always that of its first element (for example, "cbe1" above).
Additional read-only public attribute: multiple.
All arguments must be passed by name, with the exception of 'name', which may be used as a positional argument.
If name is specified, then the item must have the indicated name.
If label is specified, then the item must have a label whose whitespace-compressed, stripped, text substring-matches the indicated label string (e.g. label="please choose" will match " Do please choose an item ").
If id is specified, then the item must have the indicated id.
nr is an optional 0-based index of the items matching the query.
If nr is the default None value and more than item is found, raises AmbiguityError.
If no item is found, or if items are found but nr is specified and not found, raises ItemNotFoundError.
Optionally excludes disabled items.
The HTML element types that describe list items are: OPTION for SELECT controls, INPUT for the rest. These elements have HTML attributes that you may occasionally want to know about -- for example, the "alt" HTML attribute gives a text string describing the item (graphical browsers usually display this as a tooltip).
The returned dictionary maps HTML attribute names to values. The names and values are taken from the original HTML.
For argument docs, see the docstring for .get()
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
Includes disabled items, which may be misleading for some use cases.
Selecting items follows the behavior described in the docstring of the 'get' method.
if the item is disabled, or this control is disabled or readonly, raise AttributeError.
Raises ItemCountError if the control does not contain only one item.
by_label argument is ignored, and included only for backwards compatibility.
value is expected to be an iterable of strings that are substrings of the item labels that should be selected. Before substring matching is performed, the original label text is whitespace-compressed (consecutive whitespace characters are converted to a single space character) and leading and trailing whitespace is stripped. Ambiguous labels: it will not complain as long as all ambiguous labels share the same item name (e.g. OPTION value).
Selecting items follows the behavior described in the docstring of the 'get' method.
if the item is disabled, or this control is disabled or readonly, raise AttributeError.
Raises ItemCountError if the control does not contain only one item.
by_label argument is ignored, and included only for backwards compatibility.
Covers:
INPUT/RADIO
All arguments must be passed by name, with the exception of 'name', which may be used as a positional argument.
If name is specified, then the item must have the indicated name.
If label is specified, then the item must have a label whose whitespace-compressed, stripped, text substring-matches the indicated label string (e.g. label="please choose" will match " Do please choose an item ").
If id is specified, then the item must have the indicated id.
nr is an optional 0-based index of the items matching the query.
If nr is the default None value and more than item is found, raises AmbiguityError.
If no item is found, or if items are found but nr is specified and not found, raises ItemNotFoundError.
Optionally excludes disabled items.
The HTML element types that describe list items are: OPTION for SELECT controls, INPUT for the rest. These elements have HTML attributes that you may occasionally want to know about -- for example, the "alt" HTML attribute gives a text string describing the item (graphical browsers usually display this as a tooltip).
The returned dictionary maps HTML attribute names to values. The names and values are taken from the original HTML.
For argument docs, see the docstring for .get()
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
Includes disabled items, which may be misleading for some use cases.
Selecting items follows the behavior described in the docstring of the 'get' method.
if the item is disabled, or this control is disabled or readonly, raise AttributeError.
Raises ItemCountError if the control does not contain only one item.
by_label argument is ignored, and included only for backwards compatibility.
value is expected to be an iterable of strings that are substrings of the item labels that should be selected. Before substring matching is performed, the original label text is whitespace-compressed (consecutive whitespace characters are converted to a single space character) and leading and trailing whitespace is stripped. Ambiguous labels: it will not complain as long as all ambiguous labels share the same item name (e.g. OPTION value).
Selecting items follows the behavior described in the docstring of the 'get' method.
if the item is disabled, or this control is disabled or readonly, raise AttributeError.
Raises ItemCountError if the control does not contain only one item.
by_label argument is ignored, and included only for backwards compatibility.
Covers:
INPUT/CHECKBOX
All arguments must be passed by name, with the exception of 'name', which may be used as a positional argument.
If name is specified, then the item must have the indicated name.
If label is specified, then the item must have a label whose whitespace-compressed, stripped, text substring-matches the indicated label string (e.g. label="please choose" will match " Do please choose an item ").
If id is specified, then the item must have the indicated id.
nr is an optional 0-based index of the items matching the query.
If nr is the default None value and more than item is found, raises AmbiguityError.
If no item is found, or if items are found but nr is specified and not found, raises ItemNotFoundError.
Optionally excludes disabled items.
The HTML element types that describe list items are: OPTION for SELECT controls, INPUT for the rest. These elements have HTML attributes that you may occasionally want to know about -- for example, the "alt" HTML attribute gives a text string describing the item (graphical browsers usually display this as a tooltip).
The returned dictionary maps HTML attribute names to values. The names and values are taken from the original HTML.
For argument docs, see the docstring for .get()
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
Includes disabled items, which may be misleading for some use cases.
Selecting items follows the behavior described in the docstring of the 'get' method.
if the item is disabled, or this control is disabled or readonly, raise AttributeError.
Raises ItemCountError if the control does not contain only one item.
by_label argument is ignored, and included only for backwards compatibility.
value is expected to be an iterable of strings that are substrings of the item labels that should be selected. Before substring matching is performed, the original label text is whitespace-compressed (consecutive whitespace characters are converted to a single space character) and leading and trailing whitespace is stripped. Ambiguous labels: it will not complain as long as all ambiguous labels share the same item name (e.g. OPTION value).
Selecting items follows the behavior described in the docstring of the 'get' method.
if the item is disabled, or this control is disabled or readonly, raise AttributeError.
Raises ItemCountError if the control does not contain only one item.
by_label argument is ignored, and included only for backwards compatibility.
Covers:
SELECT (and OPTION)
OPTION 'values', in HTML parlance, are Item 'names' in mechanize parlance.
SELECT control values and labels are subject to some messy defaulting rules. For example, if the HTML representation of the control is:
<SELECT name=year>
<OPTION value=0 label="2002">current year</OPTION>
<OPTION value=1>2001</OPTION>
<OPTION>2000</OPTION> </SELECT>
The items, in order, have labels "2002", "2001" and "2000", whereas their names (the OPTION values) are "0", "1" and "2000" respectively. Note that the value of the last OPTION in this example defaults to its contents, as specified by RFC 1866, as do the labels of the second and third OPTIONs.
The OPTION labels are sometimes more meaningful than the OPTION values, which can make for more maintainable code.
Additional read-only public attribute: attrs
The attrs attribute is a dictionary of the original HTML attributes of the SELECT element. Other ListControls do not have this attribute, because in other cases the control as a whole does not correspond to any single HTML element. control.get(...).attrs may be used as usual to get at the HTML attributes of the HTML elements corresponding to individual list items (for SELECT controls, these are OPTION elements).
Another special case is that the Item.attrs dictionaries have a special key "contents" which does not correspond to any real HTML attribute, but rather contains the contents of the OPTION element:
<OPTION>this bit</OPTION>
All arguments must be passed by name, with the exception of 'name', which may be used as a positional argument.
If name is specified, then the item must have the indicated name.
If label is specified, then the item must have a label whose whitespace-compressed, stripped, text substring-matches the indicated label string (e.g. label="please choose" will match " Do please choose an item ").
If id is specified, then the item must have the indicated id.
nr is an optional 0-based index of the items matching the query.
If nr is the default None value and more than item is found, raises AmbiguityError.
If no item is found, or if items are found but nr is specified and not found, raises ItemNotFoundError.
Optionally excludes disabled items.
The HTML element types that describe list items are: OPTION for SELECT controls, INPUT for the rest. These elements have HTML attributes that you may occasionally want to know about -- for example, the "alt" HTML attribute gives a text string describing the item (graphical browsers usually display this as a tooltip).
The returned dictionary maps HTML attribute names to values. The names and values are taken from the original HTML.
For argument docs, see the docstring for .get()
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
Includes disabled items, which may be misleading for some use cases.
Selecting items follows the behavior described in the docstring of the 'get' method.
if the item is disabled, or this control is disabled or readonly, raise AttributeError.
Raises ItemCountError if the control does not contain only one item.
by_label argument is ignored, and included only for backwards compatibility.
value is expected to be an iterable of strings that are substrings of the item labels that should be selected. Before substring matching is performed, the original label text is whitespace-compressed (consecutive whitespace characters are converted to a single space character) and leading and trailing whitespace is stripped. Ambiguous labels: it will not complain as long as all ambiguous labels share the same item name (e.g. OPTION value).
Selecting items follows the behavior described in the docstring of the 'get' method.
if the item is disabled, or this control is disabled or readonly, raise AttributeError.
Raises ItemCountError if the control does not contain only one item.
by_label argument is ignored, and included only for backwards compatibility.
INPUT/SUBMIT BUTTON/SUBMIT
Covers:
INPUT/IMAGE
Coordinates are specified using one of the HTMLForm.click* methods.
If the control was surrounded by a <label> tag, that will be the first label; all other labels, connected by 'for' and 'id', are in the order that appear in the HTML.
The global mechanize.urlopen() and mechanize.urlretrieve() functions are thread safe. However, mechanize browser instances are not thread safe. If you want to use a mechanize Browser instance in multiple threads, clone it, using copy.copy(browser_object) method. The clone will share the same, thread safe cookie jar, and have the same settings/handlers as the original, but all other state is not shared, making the clone safe to use in a different thread.
mechanize supports the same mechanism for using custom CA certificates as python >= 2.7.9. To change the certificates a mechanize browser instance uses, call the mechanize.Browser.set_ca_data() method on it.
Hints for debugging programs that use mechanize.
A common mistake is to use mechanize.urlopen(), and the .extract_cookies() and .add_cookie_header() methods on a cookie object themselves. If you use mechanize.urlopen() (or OpenerDirector.open()), the module handles extraction and adding of cookies by itself, so you should not call .extract_cookies() or .add_cookie_header().
Are you sure the server is sending you any cookies in the first place? Maybe the server is keeping track of state in some other way (HIDDEN HTML form entries (possibly in a separate page referenced by a frame), URL-encoded session keys, IP address, HTTP Referer headers)? Perhaps some embedded script in the HTML is setting cookies (see below)? Turn on Logging.
When you .save() to or .load()/.revert() from a file, single-session cookies will expire unless you explicitly request otherwise with the ignore_discard argument. This may be your problem if you find cookies are going away after saving and loading.
import mechanize cj = mechanize.LWPCookieJar() opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cj)) mechanize.install_opener(opener) r = mechanize.urlopen("http://foobar.com/") cj.save("/some/file", ignore_discard=True, ignore_expires=True)
JavaScript code can set cookies; mechanize does not support this. See jsfaq.
Enable Logging.
Sometimes, a server wants particular HTTP headers set to the values it expects. For example, the User-Agent header may need to be set (mechanize.Browser.set_header()) to a value like that of a popular browser.
Check that the browser is able to do manually what you're trying to achieve programmatically. Make sure that what you do manually is exactly the same as what you're trying to do from Python -- you may simply be hitting a server bug that only gets revealed if you view pages in a particular order, for example.
Try comparing the headers and data that your program sends with those that a browser sends. Often this will give you the clue you need. You can use the developer tools in any browser to see exactly what the browser sends and receives.
If nothing is obviously wrong with the requests your program is sending and you're out of ideas, you can reliably locate the problem by copying the headers that a browser sends, and then changing headers until your program stops working again. Temporarily switch to explicitly sending individual HTTP headers (by calling .add_header(), or by using httplib directly). Start by sending exactly the headers that Firefox or Chrome send. You may need to make sure that a valid session ID is sent -- the one you got from your browser may no longer be valid. If that works, you can begin the tedious process of changing your headers and data until they match what your original code was sending. You should end up with a minimal set of changes. If you think that reveals a bug in mechanize, please report it.
To enable logging to stdout:
import sys, logging logger = logging.getLogger("mechanize") logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.DEBUG)
You can reduce the amount of information shown by setting the level to logging.INFO instead of logging.DEBUG, or by only enabling logging for one of the following logger names instead of "mechanize":
An example showing how to enable printing of HTTP headers to stdout, logging of HTTP response bodies, and logging of information about redirections:
import sys, logging import mechanize logger = logging.getLogger("mechanize") logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.DEBUG) browser = mechanize.Browser() browser.set_debug_http(True) browser.set_debug_responses(True) browser.set_debug_redirects(True) response = browser.open("http://python.org/")
Alternatively, you can examine request and response objects to see what's going on. Note that requests may involve "sub-requests" in cases such as redirection, in which case you will not see everything that's going on just by examining the original request and final response.
The examples below are written for a website that does not exist (example.com), so cannot be run.
import re import mechanize br = mechanize.Browser() br.open("http://www.example.com/") # follow second link with element text matching regular expression response1 = br.follow_link(text_regex=r"cheese\s*shop", nr=1) print(br.title()) print(response1.geturl()) print(response1.info()) # headers print(response1.read()) # body br.select_form(name="order") # Browser passes through unknown attributes (including methods) # to the selected HTMLForm. br["cheeses"] = ["mozzarella", "caerphilly"] # (the method here is __setitem__) # Submit current form. Browser calls .close() on the current response on # navigation, so this closes response1 response2 = br.submit() # print currently selected form (don't call .submit() on this, use br.submit()) print(br.form) response3 = br.back() # back to cheese shop (same data as response1) # the history mechanism returns cached response objects # we can still use the response, even though it was .close()d response3.get_data() # like .seek(0) followed by .read() response4 = br.reload() # fetches from server for form in br.forms():
print(form) # .links() optionally accepts the keyword args of .follow_/.find_link() for link in br.links(url_regex="python.org"):
print(link)
br.follow_link(link) # takes EITHER Link instance OR keyword args
br.back()
You may control the browser's policy by using the methods of mechanize.Browser's base class, mechanize.UserAgent. For example:
br = mechanize.Browser() # Explicitly configure proxies (Browser will attempt to set good defaults). # Note the userinfo ("joe:password@") and port number (":3128") are optional. br.set_proxies({"http": "joe:password@myproxy.example.com:3128",
"ftp": "proxy.example.com",
}) # Add HTTP Basic/Digest auth username and password for HTTP proxy access. # (equivalent to using "joe:password@..." form above) br.add_proxy_password("joe", "password") # Add HTTP Basic/Digest auth username and password for website access. br.add_password("http://example.com/protected/", "joe", "password") # Add an extra header to all outgoing requests, you can also # re-order or remove headers in this function. br.finalize_request_headers = lambda request, headers: headers.__setitem__(
'My-Custom-Header', 'Something') # Don't handle HTTP-EQUIV headers (HTTP headers embedded in HTML). br.set_handle_equiv(False) # Ignore robots.txt. Do not do this without thought and consideration. br.set_handle_robots(False) # Don't add Referer (sic) header br.set_handle_referer(False) # Don't handle Refresh redirections br.set_handle_refresh(False) # Don't handle cookies br.set_cookiejar() # Supply your own mechanize.CookieJar (NOTE: cookie handling is ON by # default: no need to do this unless you have some reason to use a # particular cookiejar) br.set_cookiejar(cj) # Tell the browser to send the Accept-Encoding: gzip header to the server # to indicate it supports gzip Content-Encoding br.set_request_gzip(True) # Do not verify SSL certificates import ssl br.set_ca_data(context=ssl._create_unverified_context(cert_reqs=ssl.CERT_NONE) # Log information about HTTP redirects and Refreshes. br.set_debug_redirects(True) # Log HTTP response bodies (i.e. the HTML, most of the time). br.set_debug_responses(True) # Print HTTP headers. br.set_debug_http(True) # To make sure you're seeing all debug output: logger = logging.getLogger("mechanize") logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.INFO) # Sometimes it's useful to process bad headers or bad HTML: response = br.response() # this is a copy of response headers = response.info() # this is a HTTPMessage headers["Content-type"] = "text/html; charset=utf-8" response.set_data(response.get_data().replace("<!---", "<!--")) br.set_response(response)
mechanize exports the complete interface of urllib2:
import mechanize response = mechanize.urlopen("http://www.example.com/") print(response.read())
When using mechanize, anything you would normally import from urllib2 should be imported from mechanize instead.
Kovid Goyal
2020, Kovid Goyal
January 17, 2020 | 0.4.5 |