# Help with parsing Air Quality Index from airnow.gov

**URL:** <https://forum.inductiveautomation.com/t/help-with-parsing-air-quality-index-from-airnow-gov/15655>\
**Category:** Ignition\
**Created:** [October 12, 2017, 4:56pm UTC](https://forum.inductiveautomation.com/t/help-with-parsing-air-quality-index-from-airnow-gov/15655 "2017-10-12T16:56:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![namesdontmatter](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@namesdontmatter](https://forum.inductiveautomation.com/u/namesdontmatter)\
**Post date:** [October 12, 2017, 4:56pm UTC](https://forum.inductiveautomation.com/t/help-with-parsing-air-quality-index-from-airnow-gov/15655/1 "2017-10-12T16:56:23Z")

</div>

I’m trying to get air quality index data from the [airnow.gov](http://airnow.gov) website.

This one isn’t so simple though. My output contains everything up to the second description element, which contains the only data I really need - Air Quality Index. It’s like it’s skipping the remaining data.

Any help would be appreciated!

My Code:

```auto
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:

```auto
<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>
airnowdmc@sonomatech.com (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>

```

My OUTPUT:

San Francisco, CA - Current Air Quality  
[http://www.airnow.gov/](http://www.airnow.gov/)  
EnviroFlash RSS Feed  
en-us  
[airnowdmc@sonomatech.com](mailto:airnowdmc@sonomatech.com) (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](http://feeds.enviroflash.info/rss/realtime/133.xml?id=AC9AF12B-02F4-5A9E-BD504999C6EF606E)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 12, 2017, 5:29pm UTC](https://forum.inductiveautomation.com/t/help-with-parsing-air-quality-index-from-airnow-gov/15655/2 "2017-10-12T17:29:38Z")

</div>

Simplest change:

```auto
for tag in dom.getElementsByTagName("*"):
	print tag.lastChild.data

```

`tag.firstChild.data` seems to be interpreting the commented `<!-- Format data output -->` as an element, but not actually displaying it. `lastChild` of the `description` element has the full `description` value.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 12, 2017, 5:37pm UTC](https://forum.inductiveautomation.com/t/help-with-parsing-air-quality-index-from-airnow-gov/15655/3 "2017-10-12T17:37:41Z")

</div>

Here’s another approach:

```python
import re
import system
import xml.etree.ElementTree as ET

url = "http://feeds.enviroflash.info/rss/realtime/133.xml"
response = system.net.httpGet(url)

# get the inner HTML description from the XML
root = ET.fromstring(response)
inner = root.find('.//item/description')
inner_html = inner.text

# probably fragile regex to get the AQI values
aqi_particle = re.search("(\d+) AQI - Particle Pollution", inner_html).group(1)
aqi_ozone = re.search("(\d+) AQI - Ozone", inner_html).group(1)

print "AQI (Particle): ", aqi_particle
print "AQI (Ozone): ", aqi_ozone

```

---

<div class="post-metadata">

**Author:** ![cmisztur2](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmisztur2/32/4243_2.png) [@cmisztur2](https://forum.inductiveautomation.com/u/cmisztur2)\
**Post date:** [October 12, 2017, 5:43pm UTC](https://forum.inductiveautomation.com/t/help-with-parsing-air-quality-index-from-airnow-gov/15655/4 "2017-10-12T17:43:01Z")

</div>

There is no JSON feed?

---

<div class="post-metadata">

**Author:** ![namesdontmatter](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@namesdontmatter](https://forum.inductiveautomation.com/u/namesdontmatter)\
**Post date:** [October 12, 2017, 6:44pm UTC](https://forum.inductiveautomation.com/t/help-with-parsing-air-quality-index-from-airnow-gov/15655/5 "2017-10-12T18:44:02Z")

</div>

That helped thanks! Is there any way to write a gateway script to feed it to a tag in case I want an alarm email sent out?

Gateway script I tried, but doesn’t work. Runs in the script editor, but if I try to make it a gateway script it throws up IOError: connect timed out

```auto
import re
import system
import xml.etree.ElementTree as ET

# the URL to pull data from
url = "http://feeds.enviroflash.info/rss/realtime/134.xml"
	
response = system.net.httpGet(url)
		
root = ET.fromstring(response)
inner = root.find('.//item/description')
inner_html = inner.text
	
# probably fragile regex to get the AQI values
aqi_particle = re.search("(\d+) AQI - Particle Pollution", inner_html).group(1)
currentData = aqi_particle
	
# push data to tag
system.tag.write("Global Tags/Air Quality Index", currentData)

```

---

<div class="post-metadata">

**Author:** ![namesdontmatter](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@namesdontmatter](https://forum.inductiveautomation.com/u/namesdontmatter)\
**Post date:** [October 12, 2017, 6:47pm UTC](https://forum.inductiveautomation.com/t/help-with-parsing-air-quality-index-from-airnow-gov/15655/6 "2017-10-12T18:47:36Z")

</div>

That approach worked. I didn’t realize comments were being considered, thanks!

The output of that still contains the \< div \> displaying, so I went with Kevin’s approach and send it to a value in a window so the guys can see when it’s too smoky outside (wildfires up north).

Thanks again!

---

<div class="post-metadata">

**Author:** ![namesdontmatter](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@namesdontmatter](https://forum.inductiveautomation.com/u/namesdontmatter)\
**Post date:** [October 12, 2017, 7:42pm UTC](https://forum.inductiveautomation.com/t/help-with-parsing-air-quality-index-from-airnow-gov/15655/7 "2017-10-12T19:42:22Z")

</div>

This script should work, I think the server just doesn’t have access to the internet.
