我正在使用 xml.etree.ElementTree.tostring() 将 etree 元素转换为字符串。但有时我有问题:
xpath = "..."
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
result = tree.xpath(xpath)
xml.etree.ElementTree.tostring(result[0], encoding='utf-8')
错误是:
File "../abc.py", line 165, in abc
results.append(xml.etree.ElementTree.tostring(result[0], encoding='utf-8'))
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1127, in tostring
ElementTree(element).write(file, encoding, method=method)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 818, in write
self._root, encoding, default_namespace
File "C:\Python27\lib\xml\etree\ElementTree.py", line 887, in _namespaces
_raise_serialization_error(tag)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1053, in _raise_serialization_error
"cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize <built-in function Comment> (type builtin_function_or_method)
我该如何解决?