Lxml html from string

Package html

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 also allowed, as are multiple elements as result of the parsing.

Passing a base_url will set the document’s base_url attribute (and the tree’s docinfo.URL).

fragments_fromstring ( html , no_leading_text = False , base_url = None , parser = None , **kw )

Parses several HTML elements, returning a list of elements.

The first item in the list may be a string. 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).

fromstring ( html , base_url = None , parser = None , **kw )

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 )

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.

Читайте также:  PHP File create/write Example

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 )

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.

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 ‘ , with_tail = True , doctype = None )

Return an HTML string representation of the document.

Note: if include_meta_content_type is true this will create a tag in the head; regardless of the value of include_meta_content_type any existing tag will be removed

The encoding argument controls the output encoding (defaults to ASCII, with &#. ; character references for any characters outside of ASCII). Note that you can pass the name 'unicode' as encoding argument to serialise to a Unicode string.

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.

To leave out the tail text of the top-level element that is being serialised, pass with_tail=False.

The doctype option allows passing in a plain string that will be serialised before the XML tree. Note that passing in non well-formed content here will make the XML output non well-formed. Also, an existing doctype in the document tree will not be removed when serialising an ElementTree instance.

>>> from lxml import html >>> root = html.fragment_fromstring('

Hello
world!

') >>> html.tostring(root) '

Hello
world!

' >>> html.tostring(root, method='html') '

Hello
world!

' >>> html.tostring(root, method='xml') '

Hello
world!

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

Hello
world!

TAIL
') >>> html.tostring(root[0], method='text', encoding='unicode') u'Helloworld!TAIL' >>> html.tostring(root[0], method='text', encoding='unicode', with_tail=False) u'Helloworld!' >>> doc = html.document_fromstring('

Hello
world!

') >>> html.tostring(doc, method='html', encoding='unicode') u'

Hello
world!

' >>> print(html.tostring(doc, method='html', encoding='unicode', . doctype=''))

Hello
world!

_class_xpath

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

_forms_xpath

descendant-or-self::form|descendant-or-self::x:form

_options_xpath

descendant-or-self::option|descendant-or-self::x:option
descendant-or-self::a[@rel]|descendant-or-self::x:a[@rel]

Источник

Оцените статью