首页 » 网站推广 » appenconnphp技巧_高德地图经由进程Python获取并保存地舆位置经纬度等数据

appenconnphp技巧_高德地图经由进程Python获取并保存地舆位置经纬度等数据

访客 2024-12-19 0

扫一扫用手机浏览

文章目录 [+]

这篇文章紧张讲解“Python获取并保存高德舆图平台地理位置、经纬度等数据”,即通过python调用高德舆图api进行关键字搜索,从而获取去高德舆图平台经纬度等数据。

本篇文章紧张分为3部分内容:

appenconnphp技巧_高德地图经由进程Python获取并保存地舆位置经纬度等数据

1、注册高德开放平台账号并申请Key,作为位置数据调用的密匙

appenconnphp技巧_高德地图经由进程Python获取并保存地舆位置经纬度等数据
(图片来自网络侵删)

2、通过python获取高德舆图平台经纬度数据

3、搜索结果数据保存

(1)数据预处理:将json格式搜索结果数据转换为dataframe

(2)将预处理后的数据dataframe保存到本地目录

(3)将预处理后的数据dataframe保存到数据库

一、注册高德开放平台账号并申请Key,作为位置数据调用的密匙

1、高德开放平台--网址

高德开放平台网址:https://console.amap.com/dev/index

2、高德开放平台--注册账号

打开高德开放平台网址,按哀求输入手机号注册即可

高德开放平台--用户注册

3、高德开放平台--申请Key

(1)、登录平台账号,在【运用管理】-【我的运用】界面,点击【创建新运用】

高德开放平台--我的运用

(2)、自命名填写【运用名称】,选择【运用类型】,然后点击【新建】按钮

高德开放平台--创建新运用

(3)、新建运用后,点击【添加】

高德开放平台-为运用添加 Key

(4)、自命名填写【key名称】,选择【web做事】,【ip白名单】留空,然后点击【提交】

高德开放平台--填写完善key信息

(5)、天生的key如下图,key名称=testkey,key值=‘8d4621’

高德开放平台--key值

二、通过python获取高德舆图平台经纬度数据

1、得到key往后,咱们就可以布局API数据要求函数:fun_gaode_search(key_word, city, page),Python代码如下:

"""调用高德API,POI关键字查询--关键字搜索"""def fun_gaode_search(key_word, city, page): ''' key_word: 搜索关键字 city: 城市名称 page: 搜索返回结果的第几页 ''' print('高德舆图') import requests import json # 输入API问号前固定不变的部分 url = 'https://restapi.amap.com/v3/place/text' # 将两个参数放入字典 params = {'key': '8d4621', # 高德开放平台key值 'keywords': key_word, 'city': city, 'page': page, 'offset': 1000 } result_res = requests.get(url, params) res = json.loads(result_res.text) return res['pois']

如搜索珠海有多少个高档院校:fun_gaode_search('高档院校', '珠海', 1)

res = fun_gaode_search('高档院校', '珠海', 1) # 返回搜索结果第一页 print('珠海高档院校 搜索结果如下:') pprint(res)

2、上述脚本运行结果如下:

搜索结果包括:address详细地址、adname区县名称、cityname地市名称、location地理坐标(经度,纬度),name地点名称等信息。

运行结果--json格式数据

三、保存搜索结果数据

将json格式数据转换为dataframe,然后就可以通过df.to_sql()或df.to_csv()函数保存搜索结果数据。

数据保存办法:

1、保存到本地目录:df.to_csv()

2、保存到数据库:df.to_sql()

(1)数据预处理:将json格式数据转换为dataframe

json格式数据可能存在部分字段缺失落,故需对短缺字段进行处理。
如字典元素中不存在某个字段,则dataframe中该字段标记为None.

'''高德搜索结果--json格式数据转换为dataframe格式数据'''def fun_gaode_result_deal(result_search): import pandas as pd df_result = pd.DataFrame(columns=['name', # 地点名称 'pname', # 省份 'cityname', # 城市名称 'adname', # 区域名称 'type', 'address', # 地址 'location', # 经纬度 'tel', # 电话 'typecode', 'id', 'shopinfo' ] ) for i in range(len(result_search)): dict_keys = result_search[i].keys() df_result.at[i, 'name'] = result_search[i]['name'] if 'name' in dict_keys else None df_result.at[i, 'pname'] = result_search[i]['pname'] if 'pname' in dict_keys else None df_result.at[i, 'cityname'] = result_search[i]['cityname'] if 'cityname' in dict_keys else None df_result.at[i, 'adname'] = result_search[i]['adname'] if 'adname' in dict_keys else None df_result.at[i, 'type'] = result_search[i]['type'] if 'type' in dict_keys else None df_result.at[i, 'address'] = result_search[i]['address'] if 'address' in dict_keys else None df_result.at[i, 'location'] = result_search[i]['location'] if 'location' in dict_keys else None df_result.at[i, 'tel'] = result_search[i]['tel'] if 'tel' in dict_keys and len(result_search[i]['tel']) > 6 else None # df_result.at[i, 'tel'] = result_search[i]['tel'] if 'tel' in dict_keys else None df_result.at[i, 'typecode'] = result_search[i]['typecode'] if 'typecode' in dict_keys else None df_result.at[i, 'id'] = result_search[i]['id'] if 'id' in dict_keys else None df_result.at[i, 'shopinfo'] = result_search[i]['shopinfo'] if 'shopinfo' in dict_keys else None return df_result

(2)将预处理后的数据保存到本地目录

布局函数 fun_df_to_csv(save_filename, df_result, bl_append),脚本如下

参数解释:

1、save_filename:是指保存到本地目录的文件名,如 save_filename=‘D:\\a.txt’

2、df_result:搜索结果预处理后的数据dataframe

3、bl_append:是否追加写入save_filename文件

bl_append=True,则在文件末端开始写入记录

bl_append=False,则打消文本所有内容后重新开始写入文本

'''搜索结果另存为文件'''def fun_df_to_csv(save_filename, df_result, bl_append): print('搜索结果另存为文件') pprint(sys.path) if bl_append is True: df_result.to_csv(path_or_buf=save_filename, index=False, sep=",", mode='a' # 追加 ) else: df_result.to_csv(path_or_buf=save_filename, index=False, sep="," )

(3)将预处理后的数据保存到数据库

布局函数 fun_df_to_sql(df_result, db_table, if_exists)来保存数据到数据库,脚本如下:

参数解释:

1、df_result:搜索结果预处理后的数据dataframe

2、db_table:数据库表名

库表的格式需跟df格式对应,如库表不存在,df.to_sql()会自动根据df的列名进行自动建表

3、if_exists:操作办法,有append、fail、replace

append:如果表存在,则将数据添加到这个表的后面

fail:如果表存在就不操作

replace:如果存在表,删了,重修

# 导入sqlalchemy中的create_engine模块,用于连接数据库from sqlalchemy import create_enginedef fun_df_to_sql(df_result, db_table, if_exists): try: engine = create_engine('mysql+pymysql://{username}:{password}@{host}:{port}/{database}'.format(host=self.host,username=self.user_id,password=self.pwd,port=self.port,database=self.db_name)) conn = engine.connect() # if_exists=['append','fail','replace'] # 导入到mysql数据库 df_result.to_sql(name=db_table, con=conn, index=False, if_exists=if_exists # if_exists='append' ) print('数据导入数据库--OK') except Exception as err: print('数据导入数据库--缺点: '+str(err))

综上,Python取高德舆图平台经纬度等数据全部代码如下:

from pprint import pprint from sqlalchemy import create_engine"""调用高德API,POI关键字查询--关键字搜索"""def fun_gaode_search(key_word, city, page): ''' key_word: 搜索关键字 city: 城市名称 page: 搜索返回结果的第几页 ''' print('高德舆图') import requests import json # 输入API问号前固定不变的部分 url = 'https://restapi.amap.com/v3/place/text' # 将两个参数放入字典 params = {'key': '8d4621', # 高德开放平台key值 'keywords': key_word, 'city': city, 'page': page, 'offset': 1000 } result_res = requests.get(url, params) res = json.loads(result_res.text) return res['pois']'''高德搜索结果--json格式数据转换为dataframe格式数据'''def fun_gaode_result_deal(result_search): import pandas as pd df_result = pd.DataFrame(columns=['name', # 地点名称 'pname', # 省份 'cityname', # 城市名称 'adname', # 区域名称 'type', 'address', # 地址 'location', # 经纬度 'tel', # 电话 'typecode', 'id', 'shopinfo' ] ) for i in range(len(result_search)): dict_keys = result_search[i].keys() df_result.at[i, 'name'] = result_search[i]['name'] if 'name' in dict_keys else None df_result.at[i, 'pname'] = result_search[i]['pname'] if 'pname' in dict_keys else None df_result.at[i, 'cityname'] = result_search[i]['cityname'] if 'cityname' in dict_keys else None df_result.at[i, 'adname'] = result_search[i]['adname'] if 'adname' in dict_keys else None df_result.at[i, 'type'] = result_search[i]['type'] if 'type' in dict_keys else None df_result.at[i, 'address'] = result_search[i]['address'] if 'address' in dict_keys else None df_result.at[i, 'location'] = result_search[i]['location'] if 'location' in dict_keys else None df_result.at[i, 'tel'] = result_search[i]['tel'] if 'tel' in dict_keys and len(result_search[i]['tel']) > 6 else None # df_result.at[i, 'tel'] = result_search[i]['tel'] if 'tel' in dict_keys else None df_result.at[i, 'typecode'] = result_search[i]['typecode'] if 'typecode' in dict_keys else None df_result.at[i, 'id'] = result_search[i]['id'] if 'id' in dict_keys else None df_result.at[i, 'shopinfo'] = result_search[i]['shopinfo'] if 'shopinfo' in dict_keys else None return df_result '''搜索结果另存为文件'''def fun_df_to_csv(save_filename, df_result, bl_append): print('搜索结果另存为文件') pprint(sys.path) if bl_append is True: df_result.to_csv(path_or_buf=save_filename, index=False, sep=",", mode='a' # 追加 ) else: df_result.to_csv(path_or_buf=save_filename, index=False, sep="," )def fun_df_to_sql(df_result, db_table, if_exists): try: engine = create_engine('mysql+pymysql://{username}:{password}@{host}:{port}/{database}'.format(host=self.host,username=self.user_id,password=self.pwd,port=self.port,database=self.db_name)) conn = engine.connect() # if_exists=['append','fail','replace'] # 导入到mysql数据库 df_result.to_sql(name=db_table, con=conn, index=False, if_exists=if_exists # if_exists='append' ) print('数据导入数据库--OK') except Exception as err: print('数据导入数据库--缺点: '+str(err))'''主函数'''if __name__ == "__main__": res = fun_gaode_search('高档院校', '珠海', 1) # 搜索结果 print('珠海高档院校 搜索结果如下:') # pprint(res) df = fun_gaode_result_deal(res) pprint(df) # 打印查看预处理后的数据df save_filename='d:\\a.txt' df_result=df bl_append=True fun_df_to_csv(save_filename, df_result, bl_append) # 保存数据到本地目录 df_result=df db_table='chen_gaode_result' if_exists = 'append' fun_df_to_sql(df_result, db_table, if_exists) # 保存数据到数据库

标签:

相关文章

php常量率低技巧_PHP 常量详解教程

PHP 常量常量是单个值的标识符(名称)。在脚本中无法改变该值。有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号)。注释...

网站推广 2024-12-19 阅读0 评论0