0
votes

I create xml structure using the methods lxml.etree.Element

import lxml.etree
import lxml.html

parent = lxml.etree.Element('root')
child = lxml.etree.Element('sub')
child.text = 'text'
parent.append(child)

I need to do the following query:

doc = lxml.html.document_fromstring(parent)
text = doc.xpath('sub/text()')
print(text)

but I get the following error message:

Traceback (most recent call last): File "C:\VINT\OPENSERVER\OpenServer\domains\localhost\python\parse_html\6_first_store_names_cat_full_xml_nested\q.py", line 9, in doc = lxml.html.document_fromstring(parent) File "C:\Python33\lib\site-packages\lxml\html__init__.py", line 600, in document_fromstring value = etree.fromstring(html, parser, **kw) File "lxml.etree.pyx", line 3003, in lxml.etree.fromstring (src\lxml\lxml.etree.c:67277) File "parser.pxi", line 1784, in lxml.etree._parseMemoryDocument (src\lxml\lxml.etree.c:101615) ValueError: can only parse strings

>

help my please

1

1 Answers

0
votes

lxml.html.document_fromstring() accepts a string, not an Element as you are passing in. Try passing in lxml.etree.tostring(parent):

s = lxml.etree.tostring(parent)
doc = lxml.html.document_fromstring(s)