我有一个解析网页,现在我想浏览标签,或显示一个图表。我怎样才能得到图表?或者在树中导航。显示第一步,然后是其他步骤,等等,并了解树是如何构建的。
import urllib
from lxml import etree
import StringIO
resultado=urllib.urlopen('trozo.html')
html = resultado.read()
parser= etree.HTMLParser()
tree=etree.parse(StringIO.StringIO(html),parser)我只想检查节点!一个图表将会很酷,但我只想研究它!
发布于 2011-10-28 20:23:31
您已经实现了解析,如果您执行以下操作,则可以看到:
>>> tree
<lxml.etree._ElementTree object at 0x0148AF08>现在,您可以使用lxml._ElementTree函数遍历此元素,此处介绍了这些函数:http://lxml.de/tutorial.html
以下是我从本地网络中获得的一个简单文件的一些基本信息:
>>> tree.getroot()
<Element html at 147aae0>
>>> tree.getroot().tag
'html'
>>> tree.getroot().text
>>> for child in tree.getroot().getchildren():
print child.tag, child.getchildren()
head
body
>>> for child in tree.getroot().getchildren():
print child.tag, [sub_child.tag for sub_child in child.getchildren()]
head ['title']
body ['h1', 'p', 'hr', 'address']https://stackoverflow.com/questions/7918240
复制相似问题