>>> from lxml.html import usedoctest >>> from lxml.html import fromstring, tostring >>> h = fromstring(''' ...
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... linksys ...
... ''', base_url='http://example.org/form.html') >>> h.base_url u'http://example.org/form.html' >>> f = h.forms[0] >>> f.action u'http://example.org/test' >>> f.method 'GET' >>> f.inputs # doctest:+NOPARSE_MARKUP >>> hidden = f.inputs['hidden_field'] >>> hidden.checkable False >>> hidden.value 'hidden_value' >>> hidden.value = 'new value' >>> tostring(hidden, with_tail=False) b'' >>> checkbox = f.inputs['single_checkbox'] >>> checkbox.checkable True >>> checkbox.type 'checkbox' >>> checkbox.checked False >>> print(checkbox.value) None >>> checkbox.checked = True >>> checkbox.value 'on' >>> tostring(checkbox, with_tail=False) b'' >>> checkbox2 = f.inputs['single_checkbox2'] >>> checkbox2.checked = True >>> checkbox2.value 'good' >>> group = f.inputs['check_group'] >>> group.value # doctest:+NOPARSE_MARKUP >>> group.value.add('1') >>> group.value # doctest:+NOPARSE_MARKUP >>> tostring(group[0], with_tail=False) b'' >>> group.value_options ['1', '2', '3', '4'] >>> group.value.add('doesnotexist') Traceback (most recent call last): ... KeyError: "No checkbox with value 'doesnotexist'" >>> textarea = f.inputs['textarea_field'] >>> textarea.value 'some text' >>> radios = f.inputs['radios'] >>> radios[0].label.text 'value 1' >>> radios.value 'value3' >>> radios.value = 'value1' >>> radios.value 'value1' >>> tostring(radios[0], with_tail=False) b'' >>> radios.value = None >>> tostring(radios[0], with_tail=False) b'' >>> radios.value_options ['value1', 'value2', 'value3'] >>> select = f.inputs['select1'] >>> print(select.value) None >>> select.value = "" >>> select.value '' >>> select.value = 'asdf' Traceback (most recent call last): ... ValueError: There is no option with the value of 'asdf' >>> select.value_options ['No value', '', '1'] >>> select.value = 'No value' >>> select.value 'No value' >>> select = f.inputs['select2'] >>> select.value # doctest:+NOPARSE_MARKUP >>> select.value.update(['2', '3']) >>> select.value # doctest:+NOPARSE_MARKUP >>> select.value.remove('3') >>> select.value.add('asdf') Traceback (most recent call last): ... ValueError: There is no option with the value 'asdf' >>> select.value.add('number 4') >>> select.value # doctest:+NOPARSE_MARKUP >>> select.value.remove('number 4') >>> select.value_options ['1', '2', '3', 'number 4'] >>> try: from urllib import urlencode ... except ImportError: from urllib.parse import urlencode >>> print(urlencode(f.form_values())) hidden_field=new+value&text_field=text_value&single_checkbox=on&single_checkbox2=good&check_group=1&check_group=2&check_group=3&textarea_field=some+text&select1=No+value&select2=2 >>> fields = f.fields >>> fields # doctest:+NOPARSE_MARKUP >>> for name, value in sorted(fields.items()): ... print('%s: %r' % (name, value)) check_group: hidden_field: 'new value' radios: None reset1: None select1: 'No value' select2: single_checkbox: 'on' single_checkbox2: 'good' submit1: 'submit' submit2: 'submit' text_field: 'text_value' textarea_field: 'some text' >>> import lxml.html >>> tree = lxml.html.fromstring(''' ... ...
... ... ...
... ... ''') >>> tree # doctest: +ELLIPSIS >>> tree.forms[0] # doctest: +ELLIPSIS >>> tree.forms[0].fields # doctest: +NOPARSE_MARKUP >>> list(tree.forms[0].fields.keys()) ['foo'] >>> list(tree.forms[0].fields.items()) [('foo', 'bar')] >>> list(tree.forms[0].fields.values()) ['bar'] >>> tree = lxml.html.fromstring(''' ... ...
... ...
... ... ''') >>> list(tree.forms[0].fields.keys()) ['foo'] >>> ta = tree.forms[0].inputs['foo'] >>> print(ta.value) some text
content
with tags >>> ta.value = 'abc
def' >>> print(ta.value) abc
def >>> len(ta) 0