为什么要复制元素lxml.html.to字符串返回转义字符

2024-04-19 09:03:10 发布

您现在位置:Python中文网/ 问答频道 /正文

例如:

In [1]: from lxml.html import fromstring, tostring

In [2]: import copy

In [3]: node = fromstring('<div>asdf\r\nasdf</div>')

In [4]: tostring(node, encoding='unicode')
Out[4]: '<div>asdf\r\nasdf</div>'

In [5]: tostring(copy.deepcopy(node), encoding='unicode')
Out[5]: '<div>asdf&#13;\nasdf</div>'

实际上有from html import unescape来解决这个问题。 有更好的方法同时使用copytostring吗?你知道吗


Tags: infromimportdivnodehtmlunicodeout