2

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)

どうすれば解決できますか?

4

1 に答える 1