Looping through HTML tags using BeautifulSoup
<p>As mentioned in the previous questions, I am using Beautiful soup with python to retrieve weather data from a website.</p>

<p>Here's how the website looks like:</p>

<pre><code>&lt;channel&gt;
&lt;title&gt;2 Hour Forecast&lt;/title&gt;
&lt;source&gt;Meteorological Services Singapore&lt;/source&gt;
&lt;description&gt;2 Hour Forecast&lt;/description&gt;
&lt;item&gt;
&lt;title&gt;Nowcast Table&lt;/title&gt;
&lt;category&gt;Singapore Weather Conditions&lt;/category&gt;
&lt;forecastIssue date="18-07-2016" time="03:30 PM"/&gt;
&lt;validTime&gt;3.30 pm to 5.30 pm&lt;/validTime&gt;
&lt;weatherForecast&gt;
&lt;area forecast="TL" lat="1.37500000" lon="103.83900000" name="Ang Mo Kio"/&gt;
&lt;area forecast="SH" lat="1.32100000" lon="103.92400000" name="Bedok"/&gt;
&lt;area forecast="TL" lat="1.35077200" lon="103.83900000" name="Bishan"/&gt;
&lt;area forecast="CL" lat="1.30400000" lon="103.70100000" name="Boon Lay"/&gt;
&lt;area forecast="CL" lat="1.35300000" lon="103.75400000" name="Bukit Batok"/&gt;
&lt;area forecast="CL" lat="1.27700000" lon="103.81900000" name="Bukit Merah"/&gt;` 
..
..
&lt;area forecast="PC" lat="1.41800000" lon="103.83900000" name="Yishun"/&gt;
&lt;channel&gt;
</code></pre>

<p>I managed to retrieve the information I need using these codes :</p>

<pre><code>import requests
from bs4 import BeautifulSoup
import urllib3
import csv
import sys
import json

#getting the Validtime

area_attrs_li = []

r = requests.get('http://www.nea.gov.sg/api/WebAPI/?    
dataset=2hr_nowcast&amp;keyref=781CF461BB6606AD907750DFD1D07667C6E7C5141804F45D')
soup = BeautifulSoup(r.content, "xml")
time = soup.find('validTime').string
print "validTime: " + time

#getting the date

for currentdate in soup.find_all('item'):
 element = currentdate.find('forecastIssue')
 print "date: " + element['date']

#getting the time

for currentdate in soup.find_all('item'):
 element = currentdate.find('forecastIssue')
 print "time: " + element['time'] 

#print area

for area in soup.select('area'): 
 area_attrs_li.append(area)
 print area

#print area name

areas = soup.select('area')
for data in areas:
    name = (data.get('name'))
    print name

f = open("C:\\scripts\\testing\\testingnea.csv" , 'wt')

try:
  for area in area_attrs_li:
   #print str(area) + "\n"
   writer = csv.writer(f)
   writer.writerow( (time, element['date'], element['time'], area, name))

finally:
  f.close()

print open("C:/scripts/testing/testingnea.csv", 'rt').read()
</code></pre>

<p>I managed to get the data in a CSV, however when I run this part of the codes:</p>

<pre><code>#print area name

areas = soup.select('area')
for data in areas:
    name = (data.get('name'))
    print name
</code></pre>

<p>This is the result:</p>

<p><a href="https://i.stack.imgur.com/8T1gg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8T1gg.png" alt="This is what I got"></a></p>

<p>Apparently, my loop is not working as it keeps printing the last area of the last record over and over again. </p>

<p><strong>EDIT</strong>: I tried looping through the data for area in the list :</p>

<pre><code>for area in area_attrs_li:
    name = (area.get('name'))
    print name
</code></pre>

<p>However, its still not looping.</p>

<p>I'm not sure where did the codes go wrong :/</p>