gpt4 book ai didi

python - 如何修复 lxml 断言错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:34:54 26 4
gpt4 key购买 nike

我有一台运行 python.2.7.6 的 ubuntu 机器。当我尝试使用已使用 pip 安装的 lxml 时,出现以下错误:

Traceback (most recent call last):
File "./export.py", line 44, in fetch_item
root.append(elem)
File "lxml.etree.pyx", line 742, in lxml.etree._Element.append (src/lxml/lxml.etree.c:44339)
File "apihelpers.pxi", line 24, in lxml.etree._assertValidNode (src/lxml/lxml.etree.c:14127)
AssertionError: invalid Element proxy at 140443984439416

这是什么意思,我应该如何解决这个问题?

最佳答案

我在 multiprocessing 上下文中遇到了同样的问题。可以用下面的片段来说明:

from multiprocessing import Pool

import lxml.html


def process(html):
tree = lxml.html.fromstring(html)
body = tree.find('.//body')
print(body)
return body


def main():
pool = Pool()
result = pool.apply(process, ('<html><body/></html>',))
print(type(result))
print(result)


if __name__ == '__main__':
main()

运行结果如下:

<Element body at 0x7f9f690461d8>
<class 'lxml.html.HtmlElement'>
Traceback (most recent call last):
File "test.py", line 18, in <module>
main()
File "test.py", line 14, in main
print(result)
File "src/lxml/lxml.etree.pyx", line 1142, in lxml.etree._Element.__repr__ (src/lxml/lxml.etree.c:54748)
File "src/lxml/lxml.etree.pyx", line 992, in lxml.etree._Element.tag.__get__ (src/lxml/lxml.etree.c:53182)
File "src/lxml/apihelpers.pxi", line 19, in lxml.etree._assertValidNode (src/lxml/lxml.etree.c:16856)
AssertionError: invalid Element proxy at 139697870845496

因此,考虑到 __repr__ 从工作进程工作并且返回值可用于调用进程,最明显的解释是反序列化问题。例如,可以通过返回 lxml.html.tostring(body) 或任何其他 pickle 对象来解决。

关于python - 如何修复 lxml 断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29570715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com