import requests
import pandas as pd
import csv
#百度地图API搜索
def baidu_search(querys, region):
url = 'http://api.map.baidu.com/place/v2/search?'
output = 'json'
ak = 'vLyZjPkryKy5Mn2LG1fp6ColMGFfFFiu'
place_data={}
village_value=[]
lng_value=[]
lat_value=[]
count=0
for query in querys:
uri = url + 'query=' + query + '®ion='+region+'&output=' + output + '&ak=' + ak
r = requests.get(uri)
response_dict = r.json()
results = response_dict["results"]
#place_data['village']=query
#village_value.append(query)
for adr in results:
try:
location= adr['location']
results = response_dict["results"]
except KeyError:
continue
else:
count+=1
print(count)
name = adr['name']
location= adr['location']
lng = float(location['lng'])
lat = float(location['lat'])
lng_value.append(lng)
lat_value.append(lat)
village_value.append(query)
place_data['village']=village_value
place_data['lng']=lng_value
place_data['lat']=lat_value
#place_data['latitude']=lat
#place_data['longitude']=lng
print(place_data)
place_data=pd.DataFrame(place_data)
place_data.to_csv('month_6_2000_back.csv')
baidu_search(arrs,'上海')