我正在尝试使用elt.itertext()(v3.5.0b1)遍历子树的文本内容,如下所示:

import lxml.html.soupparser as soupparser
import requests

doc = requests.get("http://f10.5post.com/forums/showthread.php?t=1142017").content
tree = soupparser.fromstring(doc)

nodes = tree.getchildren()

for elt in nodes:
    for t in elt.itertext():
         print t

但我一直在说一个错误
 File "src/lxml/iterparse.pxi", line 248, in lxml.etree.iterwalk.__init__ (src/lxml/lxml.etree.c:134032)
 File "src/lxml/apihelpers.pxi", line 67, in lxml.etree._rootNodeOrRaise (src/lxml/lxml.etree.c:15220)
ValueError: Input object has no element: HtmlComment

有没有一种方法可以跳过所有HTML注释?另外,此错误实际上是什么意思?

谢谢

最佳答案

这是正常的。

>>> from lxml import etree
>>> doc = '''
... <html><!-- PAGENAV POPUP -->
...     <div class="vbmenu_popup" id="pagenav_menu" style="display:none">
...             <table cellpadding="4" cellspacing="1" border="0">
...             <tr>
...                     <td class="thead" nowrap="nowrap">Go to Page...</td>
...             </tr>
...             <tr>
...                     <td class="vbmenu_option" title="nohilite">
...                     <form action="index.php" method="get" onsubmit="return this.gotopage()" id="pagenav_form">
...                             <input type="text" class="bginput" id="pagenav_itxt" style="font-size:11px" size="4" />
...                             <input type="button" class="button" id="pagenav_ibtn" value="Go" />
...                     </form>
...                     </td>
...             </tr>
...             </table>
...     </div>
... <!-- / PAGENAV POPUP -->
... </html>'''
>>> root = etree.fromstring(doc)
>>> nodes = root.getchildren()
>>> nodes
[<!-- PAGENAV POPUP -->, <Element div at 0x10367f290>, <!-- / PAGENAV POPUP -->]
>>> for elt in nodes:
...     for t in elt.itertext():
...         print t
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "lxml.etree.pyx", line 1406, in lxml.etree._Element.itertext (src/lxml/lxml.etree.c:48845)
  File "lxml.etree.pyx", line 2763, in lxml.etree.ElementTextIterator.__cinit__ (src/lxml/lxml.etree.c:64747)
  File "iterparse.pxi", line 219, in lxml.etree.iterwalk.__init__ (src/lxml/lxml.etree.c:125303)
  File "apihelpers.pxi", line 72, in lxml.etree._rootNodeOrRaise (src/lxml/lxml.etree.c:13689)
ValueError: Input object has no element: lxml.etree._Comment

正如你在上面看到的
>>> nodes
[<!-- PAGENAV POPUP -->, <Element div at 0x10367f290>, <!-- / PAGENAV POPUP -->]

注意:不赞成使用getchildren。您可以使用列表。
>>> list(root)
[<!-- PAGENAV POPUP -->, <Element div at 0x10367f290>, <!-- / PAGENAV POPUP -->]

节点是元素注释的列表。如果您检查itertext()的工作方式,请执行以下操作:



另一方面,如果不是在列表上进行迭代,则直接使用以下命令在根元素上进行迭代:
>>> for t in root.itertext():
...     print t
...

我得到了所有的文字和很多空格。 :)

如果您仍要迭代节点列表。您可以推断自然
>>> [item.tag for item in nodes]
[<built-in function Comment>, 'div', <built-in function Comment>]

你也可以
>>> [item.__class__ for item in nodes]
[<type 'lxml.etree._Comment'>, <type 'lxml.etree._Element'>, <type 'lxml.etree._Comment'>]

关于python - .itertext() "ValueError: Input object has no element: HtmlComment"的lxml错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31059786/

10-10 11:40