如何使用python过滤json数据

2024-04-29 02:39:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个函数来过滤从GooglePlacesAPI提取的json数据。如果名称包含字符串“Body Shop”,并且类型为['car_repair'、'point_of_interest'、'Establish'],我希望它返回企业名称和类型值,否则我希望它拒绝结果。这是到目前为止我的代码。我试了又试,但似乎找不到一种方法来存储某些条件以使搜索更容易

import googlemaps
import pprint
import time
import urllib.request  






API_KEY = 'YOUR_API_KEY'
lat, lng = 40.35003, -111.95206

#define our Client
gmaps = googlemaps.Client(key = API_KEY)
#Define our Search
places_result = gmaps.places_nearby(location= "40.35003,-111.95206", radius= 40000,open_now= False,type= ['car_repair','point_of_interest','establishment'])
#pprint.pprint(places_result['results'])
time.sleep(3)
places_result_2 = gmaps.places_nearby(page_token = 
places_result['next_page_token'])
pprint.pprint(places_result_2['results'])

places_result_2 = gmaps.places_nearby(page_token = 
places_result['next_page_token'])
    
types = place_details['result']['types']
name = place_details['result']['name']
def match(types,name):
  for val in types: 
      'car_repair','point_of_interest','establishment' in val and "Body Shop" in name
print(name,types)

Tags: ofnameimporttokenapipageresultcar
1条回答
网友
1楼 · 发布于 2024-04-29 02:39:05

试试这个:

import googlemaps
import pprint
import time
import urllib.request  




API_KEY = 'YOUR_API_KEY'
lat, lng = 40.35003, -111.95206

#define our Client
gmaps = googlemaps.Client(key = API_KEY)
#Define our Search

places_result = gmaps.places_nearby(location= "40.35003,-111.95206", radius= 40000,open_now= False,type= ['car_repair','point_of_interest','establishment'])


#heres how to retrieve the name of the first result
example_of_name = places_result['results'][0]['name']
print(example_of_name)

#gets places name and type for all the results
for place in places_result['results']:
  print("Name of Place:")
  print(place['name'])
  print("Type of the place:")
  print(place['types'], "\n")

相关问题 更多 >