解析weath的脚本

2024-09-30 12:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我在学Python。我唯一的经验是写应用程序,不太容易学。。不管怎样,到目前为止。在

我试图解析一个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 

Tags: fromimporturlparseresponselocalxmlurllib2

热门问题