Module grab.document

The Document class is the result of network request made with Grab instance.

class grab.document.Document(grab=None)[source]
Document (in most cases it is a network response
i.e. result of network request)

Save response in temporary file and open it in GUI browser.

choose_form(number=None, xpath=None, name=None, **kwargs)[source]

Set the default form.

  • number – number of form (starting from zero)
  • id – value of “id” attribute
  • name – value of “name” attribute
  • xpath – XPath query

DataNotFound if form not found


GrabMisuseError if method is called without parameters

Selected form will be available via form attribute of Grab instance. All form methods will work with default form.


# Select second form

# Select by id

# Select by name

# Select by xpath
g.choose_form(xpath='//form[contains(@action, "/submit")]')

Clone the Response object.


Detect charset of the response.

Try following methods: * meta[name=”Http-Equiv”] * XML declaration * HTTP Content-Type header

Ignore unknown charsets.

Use utf-8 as fallback charset.


This attribute points to default form.

If form was not selected manually then select the form which has the biggest number of input elements.

The form value is just an lxml.html form element.


g.go('some URL')
# Choose form automatically
print g.form

# And now choose form manually
print g.form

Return fields of default form.

Fill some fields with reasonable values.


Return response body deserialized into JSON object.

parse(charset=None, headers=None)[source]

Parse headers.

This method is called after Grab instance performs network request.


Returns pyquery handler.


Return value of parameter in query string.

rex_assert(rex, byte=False)[source]

If rex expression is not found then raise DataNotFound exception.

Search the regular expression in response body.

Parameters:byte – if False then search is performed in

response.unicode_body() else the rex is searched in response.body.

Note: if you use default non-byte mode than do not forget to build your regular expression with re.U flag.

Return found match object or None

rex_text(regexp, flags=0, byte=False, default=<object object>)[source]

Search regular expression in response body and return content of first matching group.

Parameters:byte – if False then search is performed in

response.unicode_body() else the rex is searched in response.body.


Save response body to file.

save_hash(location, basedir, ext=None)[source]

Save response body into file with special path builded from hash. That allows to lower number of files per directory.

  • location – URL of file or something else. It is used to build the SHA1 hash.
  • basedir – base directory to save the file. Note that file will not be saved directly to this directory but to some sub-directory of basedir
  • ext – extension which should be appended to file name. The dot is inserted automatically between filename and extension.

path to saved file relative to basedir


>>> url = ''
>>> g.go(url)
>>> g.response.save_hash(url, 'some_dir', ext='png')
# the file was saved to $PWD/some_dir/e8/dc/...

TODO: replace basedir with two options: root and save_to. And returns save_to + path

set_input(name, value)[source]

Set the value of form element by its name attribute.

  • name – name of element
  • value – value which should be set to element

To check/uncheck the checkbox pass boolean value.


g.set_input('sex', 'male')

# Check the checkbox
g.set_input('accept', True)
set_input_by_id(_id, value)[source]

Set the value of form element by its id attribute.

  • _id – id of element
  • value – value which should be set to element
set_input_by_number(number, value)[source]

Set the value of form element by its number in the form

  • number – number of element
  • value – value which should be set to element
set_input_by_xpath(xpath, value)[source]

Set the value of form element by xpath

  • xpath – xpath path
  • value – value which should be set to element
submit(submit_name=None, make_request=True, url=None, extra_post=None, remove_from_post=None)[source]

Submit default form.

  • submit_name – name of button which should be “clicked” to submit form
  • make_request – if False then grab instance will be configured with form post data but request will not be performed
  • url – explicitly specify form action url
  • extra_post – (dict or list of pairs) additional form data which will override data automatically extracted from the form.
  • remove_from_post – list of keys to remove from the submitted data

Following input elements are automatically processed:

  • input[type=”hidden”] - default value
  • select: value of last option
  • radio - ???
  • checkbox - ???

Multipart forms are correctly recognized by grab library.


# Assume that we going to some page with some form
g.go('some url')
# Fill some fields
g.set_input('username', 'bob')
g.set_input('pwd', '123')
# Submit the form

# or we can just fill the form
# and do manual submission
g.set_input('foo', 'bar')

# for multipart forms we can specify files
from grab import UploadFile
g.set_input('img', UploadFile('/path/to/image.png'))
text_assert(anchor, byte=False)[source]

If anchor is not found then raise DataNotFound exception.

text_assert_any(anchors, byte=False)[source]

If no anchors were found then raise DataNotFound exception.

Search the substring in response body.

  • anchor – string to search
  • byte – if False then anchor should be the unicode string, and search will be performed in response.unicode_body() else anchor should be the byte-string and search will be performed in response.body

If substring is found return True else False.


Return DOM tree of the document built with HTML DOM builder.

unicode_body(ignore_errors=True, fix_special_entities=True)[source]

Return response body as unicode string.


Return result of urlsplit function applied to response url.


Return DOM-tree of the document built with XML DOM builder.


Read the byte order mark in the text, if present, and return the encoding represented by the BOM and the BOM.

If no BOM can be detected, (None, None) is returned.