我正在努力学习Python。我唯一的经验是Applescripting,到目前为止,它并不容易学习。
我正在尝试解析xml天气网站,到目前为止,我有我需要的数据,但我无法弄清楚如何将其放入列表中以进一步处理它。有人可以帮忙吗?
from BeautifulSoup import BeautifulSoup
import xml.etree.cElementTree as ET
from xml.etree.cElementTree import parse
import urllib2
url = "http://www.weatheroffice.gc.ca/rss/city/ab-52_e.xml"
response = urllib2.urlopen(url)
local_file = open("\Temp\weather.xml", "w")
local_file.write(response.read())
local_file.close()
invalid_tags = ['b', 'br']
tree = parse("\Temp\weather.xml")
stuff = tree.findall("channel/item/description")
item = stuff[1]
parsewx = BeautifulSoup(stuff[1].text)
for tag in invalid_tags:
for match in parsewx.findAll(tag):
match.replaceWithChildren()
print parsewx
答案 0 :(得分:0)
由于XML是结构化数据,因此BeautifulSoup返回tree个标签。 documentation提供了有关如何在该树中search和navigate的广泛信息。