用Python加载XML数据
我有这段代码正在运行,但如何加载项值而不是对象本身并将其记录到数组中呢用Python加载XML数据,python,xml,Python,Xml,我有这段代码正在运行,但如何加载项值而不是对象本身并将其记录到数组中呢 import urllib import xml.etree.ElementTree as xml tree = xml.parse(urllib.urlopen('http://lazhalazha.livejournal.com/data/rss')) rootElement = tree.getroot() for a in rootElement.findall('channel/item/guid'): p
import urllib
import xml.etree.ElementTree as xml
tree = xml.parse(urllib.urlopen('http://lazhalazha.livejournal.com/data/rss'))
rootElement = tree.getroot()
for a in rootElement.findall('channel/item/guid'):
print a
输出
<Element 'guid' at 0xb75316ec>
<Element 'guid' at 0xb7531a0c>
<Element 'guid' at 0xb7531c8c>
<Element 'guid' at 0xb7531f0c>
<Element 'guid' at 0xb753a22c>
<Element 'guid' at 0xb753a4ec>
<Element 'guid' at 0xb753a7ac>
<Element 'guid' at 0xb753aa6c>
<Element 'guid' at 0xb753ad2c>
也许你想要a.text
如果guid
元素可能包含具有自己文本的子元素,并且您希望整个内容的文本序列化,请使用etree.tostring(a,method='text')
这是一个非常基本的问题。通读元素
类上的。也许您想要a.text
如果guid
元素可能包含具有自己文本的子元素,并且您希望整个内容的文本序列化,请使用etree.tostring(a,method='text')
这是一个非常基本的问题。通读元素
类上的。查看属性文本:
)
以及查找文本的方法:
但是如果你想解析RSS,你应该考虑使用FoePals::/P>
查看属性文本:
)
以及查找文本的方法:
但如果您想解析RSS,则应该绝对考虑使用FoePalsServer:
<代码> URLLIB < /Cord>是不必要的。code>xml.parse(URL)
可以正常工作。urllib
是不必要的xml.parse(URL)
可以正常工作。