2017-10-12 10 views
0

私はpythonとxmlを使い慣れています。私はairnow.govのウェブサイトから大気質インデックスのデータを取得しようとしています。私はこの情報を表示するために誘導性オートメーションのIgnitionソフトウェアを使用しています。私が天気のためにこれをしたとき、私が使った政府のサイトは、データを簡単な解析形式で持っていました。xmlを使用してPythonでデータを解析するairnow.gov

これはあまり簡単ではありません。私の出力には、第2の記述要素までのすべてが含まれています。これには、私が本当に必要とする唯一のデータ - 大気質インデックスが含まれています。残りのデータをスキップしているようです。

助けていただけたら幸いです!


マイコード:

import system 
import xml.dom.minidom 

url = "http://feeds.enviroflash.info/rss/realtime/133.xml" 

response = system.net.httpGet(url) 

dom = xml.dom.minidom.parseString(response) 

for tag in dom.getElementsByTagName("*"): 
print tag.firstChild.data 

DATA:

<rss version="2.0"> 
<channel> 
<title>San Francisco, CA - Current Air Quality</title> 
<link>http://www.airnow.gov/</link> 
<description>EnviroFlash RSS Feed</description> 
<language>en-us</language> 
<webMaster> 
[email protected] (AIRNow Data Management Center) 
</webMaster> 
<pubDate>Thu, 12 Oct 2017 08:45:10 PDT</pubDate> 
<item> 
<title>San Francisco, CA - Current Air Quality</title> 
<link> 
http://feeds.enviroflash.info/rss/realtime/133.xml?id=AC9AF12B-02F4-5A9E-BD504999C6EF606E 
</link> 
<description> 
<!-- Format data output --> 
<div xmlns="http://www.w3.org/1999/xhtml"> <table style="width: 350px;">  
<tr> <td> <br> </td> </tr> <tr> <td valign="top"> 
<div><b>Location:</b> San Francisco, CA</div><br /> <div> <b>Current 
Air Quality:</b> 10/12/17 8:00 AM PDT<br /><br /> <div> Unhealthy - 
156 AQI - Particle Pollution (2.5 microns)<br /> <br /> Good - 1 AQI - 
Ozone<br /> <br /> </div> </div> <div><b>Agency:</b> San Francisco Bay 
Area AQMD </div><br /> <div><i>Last Update: Thu, 12 Oct 2017 08:45:10 
PDT</i></div> </td> </tr> </table> </div> 
</description> 
</item> 
</channel> 
</rss> 

マイOUTPUT:

 
San Francisco, CA - Current Air Quality 
http://www.airnow.gov/ 
EnviroFlash RSS Feed 
en-us 
[email protected] (AIRNow Data Management Center) 
Thu, 12 Oct 2017 08:45:10 PDT 


San Francisco, CA - Current Air Quality 
http://feeds.enviroflash.info/rss/realtime/133.xml?id=AC9AF12B-02F4-5A9E-BD504999C6EF606E 
+0

最初の子ノードがコメントで取得する方法を見つけ出すことができます願っています。あなたは2番目の子供が欲しい。例: 'tag = dom.getElementsByTagName(" description ")[1] print(tag.childNodes [2] .data)' –

答えて

0

まずHTMLをXMLではありません。ですから、BeautifulSoupを使用して同様のやり方で同様のやり方を検討してください。例として、<br>は有効なタグで、htmlに一致する終了タグはありません。しかし、XMLパーサーはエラーを投げます。下記を参照してくださいと述べ

: -

#Will give you all text in the html, your codes attempt 
for tag in dom.getElementsByTagName("*"): 
    if tag.firstChild and not isinstance(tag.firstChild,xml.dom.minidom.Element) : 
     if(len(tag.firstChild.data.strip())>0): 
      print tag.firstChild.wholeText 
print('\n\n\n') 
#Will give you text from just the second description. 
#I believe all parts here are important like time/place/last-update etc.. 
desc=dom.getElementsByTagName("description")[1] 
for tag in desc.getElementsByTagName("*"): 
    for node in tag.childNodes: 
     if(isinstance(node,xml.dom.minidom.Text) and len(node.data.strip())>0): 
      print node.data 

あなたはdescription` `のLocation: San Francisco, CA代わりにSan Francisco, CA Location:

関連する問題