2016-12-09 7 views
0

google places APIからビジネスにリンクしているユーザーレビューを抽出しようとしています。ネストされたオブジェクトの解析Python 3.4

私は、リクエスト・ライブラリを使用しています:

import requests 

r = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJlc_6_jM4DW0RQUUtaQj2_lk&key=AIzaSyBuS4meH_HW3FO1cpUaCm6jbqzRCWe7mjc') 
json_data_dic = r.json() 

print(json_data_dic) 

だから私は私のユーザー評価&レビューを解析して抽出するJSONオブジェクトはPythonオブジェクトに変換し得ます。

テキストの「塊」が戻ってきます(下記参照)。 Python /コーディングの新しい人として、私が見るのはネストされた辞書とリストの絡みです。入れ子がある状況では、「評価」やレビューの「テキスト」などの項目をどのように参照しますか?

ガイダンスをいただければ幸いです。

{ 
    "html_attributions": [], 
    "result": { 
    "reviews": [ 
     { 
     "relative_time_description": "in the last week", 
     "profile_photo_url": "//lh6.googleusercontent.com/-06-3qCU8jEg/AAAAAAAAAAI/AAAAAAAAACc/a1z-ga9rOhs/photo.jpg", 
     "rating": 1, 
     "time": 1481050128, 
     "author_name": "Tass Wilson", 
     "text": "Worse company I've had the pleasure of dealing with. They don't follow through on what they say, their technical support team knows jack all. They are unreliable and lie. The only good part about their entire team is the sales team and thats to get you in the door, signed up and committed to a 12 month contract so they can then screw you over many times without taking you out to dinner first\n\nI would literally rather go back to using smoke signals and frikkin carrier pigeons then use Orcon again as an internet provider", 
     "aspects": [ 
      { 
      "rating": 0, 
      "type": "overall" 
      } 
     ], 
     "language": "en", 
     "author_url": "https://www.google.com/maps/contrib/116566965301711692941/reviews" 
     }, 
     { 
     "relative_time_description": "3 weeks ago", 
     "rating": 1, 
     "time": 1479258972, 
     "author_name": "Anne-Marie Petersen", 
     "text": "I have experienced nothing but pain with them - from the start to (almost the end) in fact so I could skip the 5 days without internet I have had my other provider set up my fibre and just cut the loss of the extra month as the final bad money I will ever have to pay them. I called them to ask why I hadn't been offered an upgrade to fibre - I was told I wasn't eligible for fibre due to bad credit rating. I flipped out. Namely because I have a good credit score - it's something I check regularly. So I said well how do you know - they gave me the number of the company they use so I could call them. I hung up, called the number - it was the YELLOW PAGES number. I call back, I get given the same number (same person answered) I am seeing RED by this point, so I say just give me the name of the company. I find the number myself - I then call them only to be told they don't even work with Orcon. Then the guy offers to do a quick scan of the system to see if my name is in then. Doesn't even appear. Round and round the mulberry bush - I called another company and finally have had my fibre installed and everything ago. I still have no idea how to use the extra remote they've given me but the internet is fabulous. Oh - and I also got sick of every time something was wrong it was always MY fault even though I knew they would go offline and fix something. I used to WORK at a telco company let me tell you I get the system. Finally I have to send the modem back but I've already been advised to take it into their head office and take a photo of myself handing it in because they have on numerous occasions said to people that they've never received the modem even though they had.... why the hell are they still even a company????\n\nUPDATE>> got sent two cancellation notices - neither of which were for accounts that I had power over apparently. Have taken to twitter to have a public forum so they can't get me on not returning my modem.", 
     "aspects": [ 
      { 
      "rating": 0, 
      "type": "overall" 
      } 
     ], 
     "language": "en", 
     "author_url": "https://www.google.com/maps/contrib/113533966859873380749/reviews" 
     }, 
     { 
     "relative_time_description": "4 months ago", 
     "profile_photo_url": "//lh4.googleusercontent.com/-lAEJbEHaIoE/AAAAAAAAAAI/AAAAAAAAEFo/IATRvjK2Oak/photo.jpg", 
     "rating": 1, 
     "time": 1469481312, 
     "author_name": "Keith Rankine", 
     "text": "Everything works well until you try to cancel your account. Do not be fooled into thinking you cannot give them notice to cancel within your contract term. They will try everything in their power to squeeze an extra month from you. \nI had a 12 month contract and informed them of my wish to cancel on the anniversary on that sign up date. All their emails were carefully worded to imply that this could not be done. If read carefully, and you argue, they will agree to it.", 
     "aspects": [ 
      { 
      "rating": 0, 
      "type": "overall" 
      } 
     ], 
     "language": "en", 
     "author_url": "https://www.google.com/maps/contrib/115729563512218272075/reviews" 
     }, 
     { 
     "relative_time_description": "a month ago", 
     "rating": 1, 
     "time": 1476082876, 
     "author_name": "Wayne Furlong", 
     "text": "Completely useless. Dishonest, lazy and downright incompetent. Corporate bullies. I'm so much happier with Bigpipe.", 
     "aspects": [ 
      { 
      "rating": 0, 
      "type": "overall" 
      } 
     ], 
     "language": "en", 
     "author_url": "https://www.google.com/maps/contrib/113527219809275504416/reviews" 
     }, 
     { 
     "relative_time_description": "3 months ago", 
     "rating": 1, 
     "time": 1471292986, 
     "author_name": "Shaun b", 
     "text": "Recently upgraded to \"unlimited\" Fibre with Orcon. Most mornings (5-9) we have a limited wired or wireless connection. Too often (as is the case this morning) we have no internet (so while at home we have to use phone data). This is on Wellington's cbd area. Their customer service is such that a reply could take upwards of 4 weeks. I intend to change provider.", 
     "aspects": [ 
      { 
      "rating": 0, 
      "type": "overall" 
      } 
     ], 
     "language": "en", 
     "author_url": "https://www.google.com/maps/contrib/101110905108291593535/reviews" 
     } 
    ], 
    "utc_offset": 780, 
    "adr_address": "<span class=\"street-address\">1 The Strand</span>, <span class=\"extended-address\">Takapuna</span>, <span class=\"locality\">Auckland</span> <span class=\"postal-code\">0622</span>, <span class=\"country-name\">New Zealand</span>", 
    "photos": [ 
     { 
     "html_attributions": [ 
      "<a href=\"https://maps.google.com/maps/contrib/108721869035424717950/photos\">Orcon</a>" 
     ], 
     "height": 877, 
     "photo_reference": "CoQBdwAAAO0RRplNcUkeQUxtJLTNk3uAOTadHfKZQ8g2NMa6XLRmGX2oKdUHItfnKZP0CG2WwIj198PwzfDRJpZIw4M1wSENCEOD9mFjITSwWTMjHkw1PzHb9teT6vuuROxcCdH-fwCYp0tkeBc75R8RHb2drPbTk-NN_5q88jkJTfNwdZQDEhB-25Az9550mGd00B-zK-LRGhQpTusm33tZBFXA1952txiuAUsgQA", 
     "width": 878 
     } 
    ], 
    "id": "a7c161a7081101d8897c2dd2fb41fa94b812b050", 
    "scope": "GOOGLE", 
    "vicinity": "1 The Strand, Takapuna, Auckland", 
    "international_phone_number": "+64 800 131 415", 
    "url": "https://maps.google.com/?cid=6484891029444838721", 
    "types": [ 
     "point_of_interest", 
     "establishment" 
    ], 
    "name": "Orcon", 
    "rating": 1.8, 
    "geometry": { 
     "viewport": { 
     "northeast": { 
      "lat": -36.7889585, 
      "lng": 174.77310355 
     }, 
     "southwest": { 
      "lat": -36.7890697, 
      "lng": 174.77302975 
     } 
     }, 
     "location": { 
     "lat": -36.78901820000001, 
     "lng": 174.7730694 
     } 
    }, 
    "place_id": "ChIJlc_6_jM4DW0RQUUtaQj2_lk", 
    "formatted_address": "1 The Strand, Takapuna, Auckland 0622, New Zealand", 
    "reference": "CmRRAAAAD0loSIVYDAuRKLbv5Cp6ZM_jxKHbzJ7EOrDLakY1PAlmq5YDTJ82A4qzWje0ILFv3lsEdaUpCtkuVHuOxXW6so5yqxDSfkgEnXbzd84jtfItuxis7Izu-y87vwkD7JO4EhBZB6aIdHSchBT6_USM5B5VGhTTRgmnDKndDt6amWnPkXw-57-Pww", 
    "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", 
    "website": "https://www.orcon.net.nz/", 
    "formatted_phone_number": "0800 131 415", 
    "address_components": [ 
     { 
     "short_name": "1", 
     "types": [ 
      "street_number" 
     ], 
     "long_name": "1" 
     }, 
     { 
     "short_name": "The Strand", 
     "types": [ 
      "route" 
     ], 
     "long_name": "The Strand" 
     }, 
     { 
     "short_name": "Takapuna", 
     "types": [ 
      "sublocality_level_1", 
      "sublocality", 
      "political" 
     ], 
     "long_name": "Takapuna" 
     }, 
     { 
     "short_name": "Auckland", 
     "types": [ 
      "locality", 
      "political" 
     ], 
     "long_name": "Auckland" 
     }, 
     { 
     "short_name": "Auckland", 
     "types": [ 
      "administrative_area_level_1", 
      "political" 
     ], 
     "long_name": "Auckland" 
     }, 
     { 
     "short_name": "NZ", 
     "types": [ 
      "country", 
      "political" 
     ], 
     "long_name": "New Zealand" 
     }, 
     { 
     "short_name": "0622", 
     "types": [ 
      "postal_code" 
     ], 
     "long_name": "0622" 
     } 
    ] 
    }, 
    "status": "OK" 
} 
+0

JSONを構造体を見やすくフォーマットしました。したがって、おそらく 'json_data_dic ['result']例えば、 'review' 'はあなたが繰り返し処理できるレビューのリストを提供し、 'webbrowser(json_data_dic [' result '] [' website '])'はその場所のホームページを開きます。 –

答えて

2

json_data_dic.get("result").get("reviews")またはjson_data_dic['result']['reviews']はあなたのレビュー

json_data_dic.get("result").get("reviews")[0].get("text")のリストを与えるあなたは各レビューを取得する必要がある場合は最初のレビュー

のテキストを返します。一般的には

for review in json_data_dic.get("result").get("reviews"): 
    print review.get("text") 

を、 .get(KEY)または[KEY]をキーで辞書項目にアクセスし、を使用してください[INDEX]インデックスでリスト内の項目にアクセスします(0から始まる)

+0

ありがとうございます。「.get(KEY)」は、私のパズルの欠けていた部分でした。 – Steve

関連する問題