2017-10-12 1 views
0

Je suis nouveau sur python et xml. J'essaie d'obtenir des données sur l'indice de la qualité de l'air à partir du site Web airnow.gov. J'utilise le logiciel Ignition d'Inductive Automation pour afficher cette information. Quand j'ai fait cela pour la météo, le site du gouvernement que j'ai utilisé avait ses données dans un format facile à analyser.Analyse des données de Python en utilisant xml airnow.gov

Celui-ci n'est pas si simple cependant. Ma sortie contient tout jusqu'à la deuxième élément de description, qui contient les seules données dont j'ai vraiment besoin - Air Quality Index. C'est comme si on ignorait les données restantes.

Toute aide serait appréciée!


Mon code:

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 

DONNÉES:

<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> 

Ma SORTIE:

 
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

Le premier nœud enfant de 'description' est le commentaire. Tu veux le 2ème enfant. Exemple: 'tag = dom.getElementsByTagName (" description ") [1] print (tag.childNodes [2] .data)' –

Répondre

0

First HTML est pas XML. Alors s'il vous plaît envisager d'utiliser BeautifulSoup pour faire exactement la même chose et d'une manière similaire. A titre d'exemple, <br> est une balise valide sans balise de fermeture correspondante en html. Mais un analyseur XML va lancer une erreur.

Cela dit voir ci-dessous: -

#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 

Espoir vous pouvez comprendre comment obtenir Location: San Francisco, CA au lieu de San Francisco, CA Location: