0

谁能教我如何提取纬度和经度值?我怎样才能做到这一点?

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

也许存储到 dict、func、for 循环中?

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

我只需要这里的值,例如面积、纬度和经度。

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

1 回答 1

0

BeautifulSoup是你的朋友。

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
于 2013-07-15T06:06:43.380 回答