我有一个程序,我想从Google Places API收集单个数据。我能够获得我想要的大部分属性,但是,当我尝试获取' html_attributions"时,我收到错误:
TypeError: list indices must be integers, not str
我使用了与此行相同的格式,因此我不理解它为什么会抛出此错误。
这是我的代码:
for n in range(len(data['results'])):
location = []
address = data['results'][n]['vicinity']
address = address[: address.rfind(", ")]
coords_lat = str(data['results'][n]['geometry']['location']['lat'])
coords_lng = str(data['results'][n]['geometry']['location']['lng'])
url_href = ''
if str(data['results'][n]['photos']['html_attributions']):
url_href = str(data['results'][n]['photos']['html_attributions'])
location = [address, coords_lat, coords_lng, url_href]
return location
这是JSON数据:
{
"geometry" : {
"location" : {
"lat" : 43.3151516,
"lng" : -79.9131046
},
"viewport" : {
"northeast" : {
"lat" : 43.31669727989272,
"lng" : -79.91206752010727
},
"southwest" : {
"lat" : 43.31399762010727,
"lng" : -79.91476717989272
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
"id" : "d66fce15477e5e481e618cdf7483809c6581060c",
"name" : "Two Men & A Truck",
"opening_hours" : {
"open_now" : true,
"weekday_text" : []
},
"photos" : [
{
"height" : 667,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116914873550616940396/photos\"\u003eTwo Men and A Truck\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAANR4eDG2XMLwt16x9WXQNPH45QExLrJOmBUaHYF2lNXejPRGm6G1D8WT6EhVDruj8SHkNjRwmE_BcwavNs0Olq2PJveNVFG9GFKtXoBzF1w9JLjTKZiEFxmKZho-mvTPJEhCXKgV1x1fFgzcOYasfGnjzGhQ9gezZ8T16Qyogf9CTS4OiSrpK1g",
"width" : 1024
}
],
"place_id" : "ChIJ510ZCi6dLIgRYNowStOOqxQ",
"rating" : 2,
"reference" : "CmRbAAAAU36QBuW3xN3We55B8WvA_6OAFkMJx-dS2u0REb8xFjdqxkRjQe4kIlSwx1lvu9DL8GIckgpT_pspgN618vJF06m4BZZ-pwG8gIllEHHmjhCSXX44PmeOXKeBirjeHHSJEhBKlLGuJ3HrpwKj1DnYqWpSGhSLQhCw8j3ercmdZS-c_bHNZ-BnOA",
"scope" : "GOOGLE",
"types" : [ "car_repair", "store", "point_of_interest", "establishment" ],
"vicinity" : "50 Dundas St E, Dundas"
}
答案 0 :(得分:0)
从查看有效负载,获取" html_attributions",假设您的解析数据正确['] [n](因为您没有显示整个json payload),你必须做类似
的事情data['results'][n]['photos'][0]['html_attributions'][0]