def get_tags_by_city(tags, city): map_api = baidu.MapApi() file_name = get_file_name_by_city(city) kml_name = get_kml_file_by_city(city) fold = KML.Folder() with open(kml_name, 'w', encoding='utf-8') as kml_f: with open(file_name, 'w', encoding='utf-8', newline='\n') as f: print("Start...") f.write("{},{},{},{}\n".format('name', 'lat', 'lng', 'tags')) for tag in tags: sub_folder = KML.Folder() sub_folder.append(KML.name(tag)) data = map_api.place_api.get_place_all(tag, city) for item in data: try: name = item['name'] lng = float(item['location']['lng']) lat = float(item['location']['lat']) [g_lng, g_lat] = Transfer.bd09_to_wgs84(lng, lat) # Write data in CSV f.write("{},{},{},{}\n".format(name, g_lng, g_lat, tag)) # Write data in kml sub_folder.append( KML.Placemark( KML.name(name), KML.Point( KML.coordinates( str(g_lng) + "," + str(g_lat))))) except: logging.exception('Error: Parsing json file ') fold.append(sub_folder) f.close() output = etree.tostring(fold, pretty_print=True) kml_f.write(output.decode('utf-8')) kml_f.close() print("sucess") pass
# -*- coding: utf-8 -*- """ 抓取广东省主要城市大中型超市, 便利店, 粤菜分布 """ from xpinyin import Pinyin from mapapi import baidu import json import logging import re malls = [u'家乐福', u'沃尔玛', u'好又多'] cities = [u'广州', u'深圳', u'佛山', u'东莞', u'惠州', u'中山', u'湛江', u'茂名', u'江门'] piny = Pinyin() map_api = baidu.MapApi() def get_file_name_by_city(city): return 'data/%s.json' % (piny.get_pinyin(city, '')) def get_malls_by_city(malls, city, filter=None): """ :param malls: :param city: :param filter: 过滤函数func(item, mall, city), 返回值为False的item将会被过滤掉 """ file_name = get_file_name_by_city(city) with open(file_name, 'w') as f:
#!/usr/bin/env python # -*- coding: utf-8 -*- import json import pymongo from mapapi import baidu import sys reload(sys) sys.setdefaultencoding('utf-8') map_api = baidu.MapApi(['百度地图API Key']) def dealData(): keyword=['酒店','公寓','宾馆'] city=['东莞'] for i in keyword: for j in city: ret = map_api.place_api.get_place_all(i,j) for data in ret: client = pymongo.MongoClient('localhost', 27017) client.gzhotelinfo.authenticate('用户名','密码') dghotelinfo= client['dghotelinfo'] dghotel = dghotelinfo['dghotel'] dghotel.save(data) if __name__=='__main__': dealData()