lxml:cssselect():AttributeError:'lxml.etree._Element'对象没有属性'cssselect' [英] lxml: cssselect(): AttributeError: 'lxml.etree._Element' object has no attribute 'cssselect'
本文介绍了lxml:cssselect():AttributeError:'lxml.etree._Element'对象没有属性'cssselect'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以解释为什么第一次调用 root.cssselect()
有效,而第二个失败?
Can someone explain why the first call to root.cssselect()
works, while the second fails?
from lxml.html import fromstring
from lxml import etree
html='<html><a href="http://example.com">example</a></html'
root = fromstring(html)
print 'via fromstring', repr(root) # via fromstring <Element html at 0x...>
print root.cssselect("a")
root2 = etree.HTML(html)
print 'via etree.HTML()', repr(root2) # via etree.HTML() <Element html at 0x...>
root2.cssselect("a") # --> Exception
我得到:
Traceback (most recent call last):
File "/home/foo_eins_d/src/foo.py", line 11, in <module>
root2.cssselect("a")
AttributeError: 'lxml.etree._Element' object has no attribute 'cssselect'
版本: lxml == 3.4.4
推荐答案
区别在于元素的类型。示例 -
The difference is in the type of element. Example -
In [12]: root = etree.HTML(html)
In [13]: root = fromstring(html)
In [14]: root2 = etree.HTML(html)
In [15]: type(root)
Out[15]: lxml.html.HtmlElement
In [16]: type(root2)
Out[16]: lxml.etree._Element
lxml.html.HTMLElement
有方法 cssselect()
。此外, HTMLElement
是 etree._Element
的子类。
lxml.html.HTMLElement
has the method cssselect()
. Also, HTMLElement
is a subclass of etree._Element
.
但 lxml.etree._Element
没有该方法。
But the lxml.etree._Element
does not have that method.
如果你想解析html,你应该使用 lxml.html
。
If you want to parse html, you should use lxml.html
.
这篇关于lxml:cssselect():AttributeError:'lxml.etree._Element'对象没有属性'cssselect'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文