0

我从这个网站https://api.projectnimbus.org/neaodataservice.svc/NowcastSet使用 beautifulsoup 提取了这段代码?

显示所有位置后,如何将其漂亮地打印到密钥对值?喜欢的位置:宏茂桥纬度:1.3546846 经度:103.564132?

from BeautifulSoup import BeautifulStoneSoup #Using bs3
import urllib2

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
4

2 回答 2

0

soup.find仅返回第一个匹配项,因此您需要soup.findAll. 至于漂亮的打印,您可以使用pprint.

于 2013-07-15T07:53:46.800 回答
0

修改的:

import urllib2
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)

for prop in prop_list:
    print "Area: %(area)s\nLat: %(latitude)s\nLong: %(longitude)s\n" % prop
于 2013-07-15T08:38:45.150 回答