0

我使用 lxml.etree.Element 方法创建 xml 结构

import lxml.etree
import lxml.html

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

我需要执行以下查询:

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

但我收到以下错误消息:

回溯(最近一次调用最后):文件“C:\VINT\OPENSERVER\OpenServer\domains\localhost\python\parse_html\6_first_store_names_cat_full_xml_nested\q.py”,第 9 行,在 doc = lxml.html.document_fromstring(parent) 文件中C:\Python33\lib\site-packages\lxml\html__init__.py”,第 600 行,在 document_fromstring value = etree.fromstring(html, parser, **kw) 文件“lxml.etree.pyx”,第 3003 行,在lxml.etree.fromstring (src\lxml\lxml.etree.c:67277) 文件“parser.pxi”,第 1784 行,在 lxml.etree._parseMemoryDocument (src\lxml\lxml.etree.c:101615) ValueError: can只解析字符串

>

请帮帮我

4

1 回答 1

0

lxml.html.document_fromstring()接受一个字符串,而不是Element你传入的字符串。尝试传入lxml.etree.tostring(parent)

s = lxml.etree.tostring(parent)
doc = lxml.html.document_fromstring(s)
于 2014-02-27T18:49:07.450 回答