0

私は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 
4

1 に答える 1

0

XML は構造化データであるため、BeautifulSoup はタグのツリーを返します。ドキュメントには、そのツリーを検索してナビゲートする方法に関する広範な情報が含まれています。

于 2012-04-19T19:57:19.527 に答える