lxml.html

Package lxml :: Package html |
[hide private]
[frames] | no frames]

# Package html

source code

The `lxml.html` tool set for HTML handling.

Submodules | [hide private]
lxml.html.ElementSoup: Legacy interface to the BeautifulSoup HTML parser. lxml.html._dictmixin lxml.html._setmixin lxml.html.builder: A set of HTML generator tags for building HTML documents. lxml.html.clean: A cleanup tool for HTML. lxml.html.defs lxml.html.diff lxml.html.formfill lxml.html.html5parser: An interface to html5lib that mimics the lxml.html interface. lxml.html.soupparser: External interface to the BeautifulSoup HTML parser. lxml.html.usedoctest: Doctest module for HTML comparison.

Classes | [hide private]
| HtmlMixin
| _MethodFunc An object that represents a method on an element as a function; the function takes either an element or an HTML string. It returns whatever the function normally returns, or if the function works in-place (and so returns None) it returns a serialized form of the resulting document.
| HtmlComment
| HtmlElement
| HtmlProcessingInstruction
| HtmlEntity
| HtmlElementClassLookup A lookup scheme for HTML Element classes.
| FormElement Represents a <form> element.
| FieldsDict
| InputGetter An accessor that represents all the input fields in a form.
| InputMixin Mix-in for all input elements (input, select, and textarea)
| TextareaElement `<textarea>` element. You can get the name with `.name` and get/set the value with `.value`
| SelectElement `<select>` element. You can get the name with `.name`.
| MultipleSelectOptions Represents all the selected options in a `<select multiple>` element.
| RadioGroup This object represents several `<input type=radio>` elements that have the same name.
| CheckboxGroup Represents a group of checkboxes (`<input type=checkbox>`) that have the same name.
| CheckboxValues Represents the values of the checked checkboxes in a group of checkboxes with the same name.
| InputElement Represents an `<input>` element.
| LabelElement Represents a `<label>` element.
| HTMLParser An HTML parser that is configured to return lxml.html Element objects.
| XHTMLParser An XML parser that is configured to return lxml.html Element objects.

Functions | [hide private]
__fix_docstring(s) | source code
_unquote_match(s, pos) | source code
_transform_result(typ, result) Convert the result back into the input type. | source code
_nons(tag) | source code
document_fromstring(html, parser=None, **kw) | source code
fragments_fromstring(html, no_leading_text=False, base_url=None, parser=None, **kw) Parses several HTML elements, returning a list of elements. | source code
fragment_fromstring(html, create_parent=False, base_url=None, parser=None, **kw) Parses a single HTML element; it is an error if there is more than one element, or if anything but whitespace precedes or follows the element. | source code
fromstring(html, base_url=None, parser=None, **kw) Parse the html, returning a single element/document. | source code
parse(filename_or_url, parser=None, base_url=None, **kw) Parse a filename, URL, or file-like object into an HTML document tree. Note: this returns a tree, not an element. Use `parse(...).getroot()` to get the document root. | source code
_contains_block_level_tag(el) | source code
_element_name(el) | source code
submit_form(form, extra_values=None, open_http=None) Helper function to submit a form. Returns a file-like object, as from `urllib.urlopen()`. This object also has a `.geturl()` function, which shows the URL if there were any redirects. | source code
open_http_urllib(method, url, values) | source code
html_to_xhtml(html) Convert all tags in an HTML tree to XHTML by moving them to the XHTML namespace. | source code
xhtml_to_html(xhtml) Convert all tags in an XHTML tree to HTML by removing their XHTML namespace. | source code
__str_replace_meta_content_type(...) sub(repl, string[, count = 0]) --> newstring Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. | source code
__bytes_replace_meta_content_type(...) sub(repl, string[, count = 0]) --> newstring Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. | source code
tostring(doc, pretty_print=False, include_meta_content_type=False, encoding=None, method=`'``html``'`) Return an HTML string representation of the document. | source code
open_in_browser(doc, encoding=None) Open the HTML document in a web browser, saving it to a temporary file to open it. Note that this does not delete the file after use. This is mainly meant for debugging. | source code
Element(*args, **kw) Create a new HTML Element. | source code

Variables | [hide private]
| XHTML_NAMESPACE = ``'``http://www.w3.org/1999/xhtml``'``
| _rel_links_xpath = `descendant-or-self::a[@rel]|descendant-or-s`...``
| _options_xpath = `descendant-or-self::option|descendant-or-self`...``
| _forms_xpath = `descendant-or-self::form|descendant-or-self::x:`...``
| _class_xpath = `descendant-or-self::*[@class and contains(conca`...``
| _id_xpath = `descendant-or-self::*[@id=$id]`
| _collect_string_content = `string()`
| _css_url_re = `re.compile(r'`(?i)`url\(`(`"`[^`"`]``*`"`|`\'`[^`\'`]``*`\'`|``[^`\)`]``*``...``
| _css_import_re = `re.compile(r'@import "`(`.`*?``)`"')`
| _label_xpath = `//label[@for=$id]|//x:label[@for=$id]`
| _archive_re = `re.compile(r'`[^` `]``+`')`
| find_rel_links = `_MethodFunc('find_rel_links', copy= False)`
| find_class = `_MethodFunc('find_class', copy= False)`
| make_links_absolute = `_MethodFunc('make_links_absolute', copy=`...``
| resolve_base_href = `_MethodFunc('resolve_base_href', copy= True)`
| iterlinks = `_MethodFunc('iterlinks', copy= False)`
| rewrite_links = `_MethodFunc('rewrite_links', copy= True)`
| html_parser = `HTMLParser()`
| xhtml_parser = `XHTMLParser()`
| __package__ = ``'``lxml.html``'``

Function Details | [hide private]

### fragments_fromstring(html, no_leading_text=False, base_url=None, parser=None, **kw) | source code
Parses several HTML elements, returning a list of elements. The first item in the list may be a string (though leading whitespace is removed). If no_leading_text is true, then it will be an error if there is leading text, and it will always be a list of only elements. base_url will set the document's base_url attribute (and the tree's docinfo.URL)

### fragment_fromstring(html, create_parent=False, base_url=None, parser=None, **kw) | source code
Parses a single HTML element; it is an error if there is more than one element, or if anything but whitespace precedes or follows the element. If create_parent is true (or is a tag name) then a parent node will be created to encapsulate the HTML in a single element. In this case, leading or trailing text is allowed. base_url will set the document's base_url attribute (and the tree's docinfo.URL)

### fromstring(html, base_url=None, parser=None, **kw) | source code
Parse the html, returning a single element/document. This tries to minimally parse the chunk of text, without knowing if it is a fragment or a document. base_url will set the document's base_url attribute (and the tree's docinfo.URL)

### parse(filename_or_url, parser=None, base_url=None, **kw) | source code
Parse a filename, URL, or file-like object into an HTML document tree. Note: this returns a tree, not an element. Use `parse(...).getroot()` to get the document root. You can override the base URL with the `base_url` keyword. This is most useful when parsing from a file-like object.

### submit_form(form, extra_values=None, open_http=None) | source code
Helper function to submit a form. Returns a file-like object, as from `urllib.urlopen()`. This object also has a `.geturl()` function, which shows the URL if there were any redirects. You can use this like:
    form = doc.forms[0]
    form.inputs['foo'].value = 'bar' # etc
    response = form.submit()
    doc = parse(response)
    doc.make_links_absolute(response.geturl())
To change the HTTP requester, pass a function as `open_http` keyword argument that opens the URL for you. The function must have the following signature:
    open_http(method, URL, values)
The action is one of 'GET' or 'POST', the URL is the target URL as a string, and the values are a sequence of `(name, value)` tuples with the form data.

### tostring(doc, pretty_print=False, include_meta_content_type=False, encoding=None, method=`'``html``'`) | source code
Return an HTML string representation of the document. Note: if include_meta_content_type is true this will create a `<meta http-equiv="Content-Type" ...>` tag in the head; regardless of the value of include_meta_content_type any existing `<meta http-equiv="Content-Type" ...>` tag will be removed The `encoding` argument controls the output encoding (defauts to ASCII, with &#...; character references for any characters outside of ASCII). The `method` argument defines the output method. It defaults to 'html', but can also be 'xml' for xhtml output, or 'text' to serialise to plain text without markup. Note that you can pass the builtin `unicode` type as `encoding` argument to serialise to a unicode string. Example:
    >>> from lxml import html
    >>> root = html.fragment_fromstring('<p>Hello<br>world!</p>')

    >>> html.tostring(root)
    '<p>Hello<br>world!</p>'
    >>> html.tostring(root, method='html')
    '<p>Hello<br>world!</p>'

    >>> html.tostring(root, method='xml')
    '<p>Hello<br/>world!</p>'

    >>> html.tostring(root, method='text')
    'Helloworld!'

    >>> html.tostring(root, method='text', encoding=unicode)
    u'Helloworld!'

### Element(*args, **kw) | source code
Create a new HTML Element. This can also be used for XHTML documents.

Variables Details | [hide private]

  ### _rel_links_xpath Value:
      descendant-or-self::a[@rel]|descendant-or-self::x:a[@rel]

  ### _options_xpath Value:
      descendant-or-self::option|descendant-or-self::x:option

  ### _forms_xpath Value:
      descendant-or-self::form|descendant-or-self::x:form

  ### _class_xpath Value:
      descendant-or-self::*[@class and contains(concat(' ', normalize-space(
      @class), ' '), concat(' ', $class_name, ' '))]

  ### _css_url_re Value:
      re.compile(r'(?i)url\(("[^"]*"|\'[^\']*\'|[^\)]*)\)')

  ### make_links_absolute Value:
      _MethodFunc('make_links_absolute', copy= True)