def get_data(city, keyword, coord): # 1. 获取城市边界的最大、最小经纬度 amap_key = buffer_keys[0] # 总是获取队列中的第一个密钥 max_lng, min_lng, max_lat, min_lat = area_boundary.getlnglat( city, amap_key) print('当前城市:', city, "max_lng, min_lng, max_lat, min_lat:", max_lng, min_lng, max_lat, min_lat) # 2. 生成网格切片格式: grids_lib = city_grid.generate_grids(min_lng, max_lat, max_lng, min_lat, pology_split_distance) print('划分后的网格数:', len(grids_lib)) print(grids_lib) all_data = [] begin_time = time.time() print( '==========================正式开始爬取啦!!!!!!!!!!!================================' ) for grid in grids_lib: # grid格式:[112.23, 23.23, 112.24, 23.22] one_pology_data = getpois(grid, keyword) print('===================================当前矩形范围:', grid, '总共:', str(len(one_pology_data)) + "条数据.............................") all_data.extend(one_pology_data) end_time = time.time() print('全部:', str(len(grids_lib)) + '个矩形范围', '总的', str(len(all_data)), '条数据, 耗时:', str(end_time - begin_time), '正在写入CSV文件中') file_folder, file_name = write_to_csv(all_data, city, keyword, coord) # 写入shp if file_folder is not None: trans_point_to_shp(file_folder, file_name, 0, 1, pology_split_distance, keyword)
def get_data(city, keyword): ''' 根据城市名以及POI类型爬取数据 :param city: :param keyword: :return: ''' isNeedAreas = True if isNeedAreas: area = get_areas(city) all_pois = [] if area != None and area != "": area_list = str(area).split(",") if area_list == 0: area_list = str(area).split(",") for area in area_list: pois_area = getpois(area, keyword) print('当前城区:' + str(area) + ', 分类:' + str(keyword) + ", 总的有" + str(len(pois_area)) + "条数据") all_pois.extend(pois_area) print("所有城区的数据汇总,总数为:" + str(len(all_pois))) if data_file_format == 2: #写入CSV file_folder, file_name = write_to_csv(all_pois, city, keyword) #写入SHP trans_point_to_shp(file_folder, file_name, 0, 1) return return write_to_excel(all_pois, city, keyword) else: pois_area = getpois(area, keyword) if data_file_format == 2: # 写入CSV file_folder, file_name = write_to_csv(all_pois, city, keyword) # 写入SHP trans_point_to_shp(file_folder, file_name, 0, 1) return return write_to_excel(pois_area, city, keyword) return None
if __name__ == '__main__': # 函数运行的入口,直接print('hello, world')也可以运行。 #初始化密钥队列 init_queen() one_pro_type_poi_list = [] for pro in province: # 循环获取每个省份的POI print('=====================当前省:', pro, '爬取开始...............') for type in keyword: # 循环获取每个编码的POI print('=====================当前POI类型:', type, '爬取开始...............') #一个省一个类型的数据总和 #try: one_pro_type_poi_list = get_data(pro, type) # 最先要读,和最先调用的函数 print('当前省:', pro, '分类:', type, '爬取完成,总共', len(one_pro_type_poi_list), '条数据') # 写入文件中 file_folder, file_name = write_to_csv(one_pro_type_poi_list, pro, type) # 写入shp trans_point_to_shp(file_folder, file_name, 0, 1) #except Exception as e: #print('===================================爬取失败,当前省:', pro, '分类:', type, '...........................') #print(e) print('总的', len(province), '个省份, ', len(keyword), '个分类数据全部爬取完成!') # 最后打印