Question

Can anyone teach me how to extract the latitude and longitude values? How can I do that?

url="https://api.projectnimbus.org/neaodataservice.svc/NowcastSet"
request = urllib2.Request(url)
request.add_header("accept", "*/*")
request.add_header('AccountKey', "OSJeROQjTg4v7Ec3kiecjw==")
request.add_header('UniqueUserID', "00000000000000000000000000000001")
result = urllib2.urlopen(request)
for key in result.readlines():
    print key

Maybe storing into dict, funct, for loop ?

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://api.projectnimbus.org/NEAoDataService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">NowcastSet</title>
  <id>https://api.projectnimbus.org/neaodataservice.svc/NowcastSet</id>
  <updated>2013-07-15T04:29:33Z</updated>
  <link rel="self" title="NowcastSet" href="NowcastSet" />
  <entry>
    <id>https://api.projectnimbus.org/NEAoDataService.svc/NowcastSet(1322321)</id>
    <title type="text">Ang Mo Kio</title>
    <summary type="text">Cloudy</summary>
    <updated>2013-07-15T04:29:33Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="Nowcast" href="NowcastSet(1322321)" />
    <category term="NEAModel.Nowcast" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">

I only need the values here, such as area, lat and long.

<m:properties>
        <d:NowcastID m:type="Edm.Int32">1322321</d:NowcastID>
        <d:Area>Ang Mo Kio</d:Area>
        <d:Condition>Cloudy</d:Condition>
        <d:Latitude m:type="Edm.Double">1.37315954</d:Latitude>
        <d:Longitude m:type="Edm.Double">103.8523865</d:Longitude>
        <d:Summary>Cloudy</d:Summary>
        <d:Distance m:type="Edm.Double">0</d:Distance>
        <d:CreateDate m:type="Edm.DateTime">2013-07-15T04:00:25.987</d:CreateDate>
      </m:properties>
    </content>
    <geo:lat xmlns:geo="http://www.georss.org/georss">1.37315954</geo:lat>
    <geo:long xmlns:geo="http://www.georss.org/georss">103.8523865</geo:long>
  </entry>
</feed>
Was it helpful?

Solution

BeautifulSoup is your friend.

import urllib2
try:
    from bs4 import BeautifulStoneSoup #Using bs4
except ImportError:
    from BeautifulSoup import BeautifulStoneSoup #Using bs3

url="https://api.projectnimbus.org/neaodataservice.svc/NowcastSet"
request = urllib2.Request(url)
request.add_header("accept", "*/*")
request.add_header('AccountKey', "OSJeROQjTg4v7Ec3kiecjw==")
request.add_header('UniqueUserID', "00000000000000000000000000000001")
result = urllib2.urlopen(request)
xml_str = result.read()

soup = BeautifulStoneSoup(xml_str)

prop_list = []
for content in soup.findAll("m:properties")
    props = {}
    for prop in content.findChildren():
        props[prop.name[2:]] = prop.text
    prop_list.append(props)

print prop_list
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top