python抓取json数据_javascript-如何使用python从json提取特定数据

我想提取以下给定json中“ address_components”中存在的“ long_name”数据.面临的挑战是,我只需要“类型”为“ administrative_area_level_2”的“ long_name”数据.请让我知道如何最好仅在python中提取特定数据.谢谢

编辑:请注意js [“ results”] [0] [“ address_components”] [2] [“ long_name”]不是我要提取其值的方式,因为[“ address_components”] [2]对于不同的位置会有所不同作为输入给出.我的主要标准是提取“类型”始终为“ administrative_area_level_2”的“ long_name”数据

{

"status": "OK",

"results": [

{

"geometry": {

"location_type": "APPROXIMATE",

"bounds": {

"northeast": {

"lat": 12.9177876,

"lng": 80.24104

},

"southwest": {

"lat": 12.875989,

"lng": 80.20860669999999

}

},

"viewport": {

"northeast": {

"lat": 12.9177876,

"lng": 80.24104

},

"southwest": {

"lat": 12.875989,

"lng": 80.20860669999999

}

},

"location": {

"lat": 12.9009877,

"lng": 80.2279301

}

},

"formatted_address": "Sholinganallur, Chennai, Tamil Nadu, India",

"place_id": "ChIJGzh_3nlbUjoRGz_-itQtu_8",

"address_components": [

{

"long_name": "Sholinganallur",

"types": [

"sublocality_level_1",

"sublocality",

"political"

],

"short_name": "Sholinganallur"

},

{

"long_name": "Chennai",

"types": [

"locality",

"political"

],

"short_name": "Chennai"

},

{

"long_name": "Kanchipuram",

"types": [

"administrative_area_level_2",

"political"

],

"short_name": "Kanchipuram"

},

{

"long_name": "Tamil Nadu",

"types": [

"administrative_area_level_1",

"political"

],

"short_name": "TN"

},

{

"long_name": "India",

"types": [

"country",

"political"

],

"short_name": "IN"

}

],

"partial_match": true,

"types": [

"sublocality_level_1",

"sublocality",

"political"

]

}

]

}

下面是我正在尝试的代码,但是我只能输出到位置.让我知道我需要在代码中进行哪些更改.

import urllib

import json

apiurl= 'http://maps.googleapis.com/maps/api/geocode/json?'

while True:

address = raw_input('Enter location: ')

if len(address) < 1 : break

url = apiurl + urllib.urlencode({'sensor':'false', 'address': address})

print 'Retrieving', url

uh = urllib.urlopen(url).read()

print 'Retrieved',len(uh),'characters'

js = json.loads(str(uh))

print json.dumps(js, indent=4)

jayz = js["results"][0]

lat = jayz["geometry"]["location"]["lat"]

lng = jayz["geometry"]["location"]["lng"]

print 'lat',lat,'lng',lng

location = jayz['formatted_address']

print location

components = js["results"][0]["address_components"]

names = [component['long_name'] for component in components if 'administrative_area_level_2' in component['types']]

name = names[0]

print "District:", name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值