我遇到了一个python问题,我从XML中读取数据,并设置了两个扩展函数;一个查找位置,另一个查找第一个中的位置,并返回信息。我的问题是,我需要它继续下一页,并找到每一个的其他匹配项。我不确定这是不是一个很好的解释,所以下面是代码:
def findEntryTag(webPage):
start= webPage.find("<entry>") +7
end= webPage.find("</entry>")
slicedString=webPage[start:end]
return slicedString
def findEarthquake(webPage):
slicedString=findEntryTag(webPage)
start= slicedString.find("<title>") +7
end= slicedString.find("</title>")
eq= slicedString[start:end]
return eq
my Earthquake= findEarthquake(text)
print (myEarthquake)因此,需要它再次执行这些函数,以获得另一次地震,并打印出它们的空洞列表。请帮帮我!谢谢
发布于 2011-03-02 04:10:45
lxml.etree让这一切变得很好用。
对于结构如下的XML文档:
<entry>
<title>story 1</title>
<text>this is the first earthquake story</text>
<title>story 2</title>
<text>this is the second earthquake story</text>
<title>story 3</title>
<text>this is the third earthquake story</text>
</entry>您可以像这样使用lxml.etree来解析它:
from lxml import etree
root = etree.parse("test.xml")
for element in root.iter("title"):
print("%s - %s" % (element.tag, element.text))(来自http://lxml.de/tutorial.html的示例)
结果如下所示:
title - story 1
title - story 2
title - story 3品尝季节!
发布于 2011-03-02 03:31:32
不要试图手动解析XML。有很多好的方法可以做到这一点,包括在标准库中使用ElementTree。
https://stackoverflow.com/questions/5159238
复制相似问题