进阶1·调用高德API获取交通数据(多个URL访问,字典提取,while无限循环下的程序间断运行)

调用高德开放API获取实时交通数据

想法及思路

现在大型网站都提供了好多开放API,根据这些开放API可以做一些比较有趣的东西,本次调用高德开放api实时获取你想知道的道路拥堵状况
工具及技术:
Python,pycharm,爬虫
参考点
多个URL访问,字典提取
思路
1~通过申请高德开发者获取api及key
2~运用爬虫分析出获取的数据
3~提取有用信息
4~数据保存

代码


import requests
import time

a=1
while  not a == 2:
    addresses=['经十路','纬六路','紫薇路','大学路','纬五路','解放路','英雄山路']
    for address in addresses:
        url='https://restapi.amap.com/v3/traffic/status/road?name={address}&adcode=370100&key=473e45d53b24399e81696fa1406113ea'
        response=requests.get(url=url.format(address=address))#将地址填到url中
        response_text=response.text
        #print(type(response_text)) 通过用type打印出格式为字符串,或者用pprint打印可以看出是字符串
        text=eval(response_text)#去掉最外层字符串,使其转换为字典格式
        #字典信息提取方法:
        address_names=text['trafficinfo']['description']
        address_situations=text['trafficinfo']['evaluation']['description']
        situation_percentage=text['trafficinfo']['evaluation']['expedite']
        print(address_names,address_situations,situation_percentage)
        time.sleep(1)
    time.sleep(10)
    
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 好的,我可以帮你写一个高德设施poi重分类的代码。以下是示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE_LENGTH 1024 int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: %s input_file output_file\n", argv[]); return 1; } FILE *input_file = fopen(argv[1], "r"); if (input_file == NULL) { printf("Failed to open input file: %s\n", argv[1]); return 1; } FILE *output_file = fopen(argv[2], "w"); if (output_file == NULL) { printf("Failed to open output file: %s\n", argv[2]); fclose(input_file); return 1; } char line[MAX_LINE_LENGTH]; while (fgets(line, MAX_LINE_LENGTH, input_file) != NULL) { char *category = strtok(line, ","); char *name = strtok(NULL, ","); char *address = strtok(NULL, ","); char *latitude = strtok(NULL, ","); char *longitude = strtok(NULL, ","); char *other_info = strtok(NULL, ""); if (strcmp(category, "餐饮服务") == ) { fprintf(output_file, "餐饮,%s,%s,%s,%s\n", name, address, latitude, longitude); } else if (strcmp(category, "购物服务") == ) { fprintf(output_file, "购物,%s,%s,%s,%s\n", name, address, latitude, longitude); } else if (strcmp(category, "生活服务") == ) { fprintf(output_file, "生活,%s,%s,%s,%s\n", name, address, latitude, longitude); } else if (strcmp(category, "体育休闲服务") == ) { fprintf(output_file, "体育休闲,%s,%s,%s,%s\n", name, address, latitude, longitude); } else if (strcmp(category, "医疗保健服务") == ) { fprintf(output_file, "医疗保健,%s,%s,%s,%s\n", name, address, latitude, longitude); } else if (strcmp(category, "住宿服务") == ) { fprintf(output_file, "住宿,%s,%s,%s,%s\n", name, address, latitude, longitude); } else if (strcmp(category, "汽车服务") == ) { fprintf(output_file, "汽车,%s,%s,%s,%s\n", name, address, latitude, longitude); } else if (strcmp(category, "科教文化服务") == ) { fprintf(output_file, "科教文化,%s,%s,%s,%s\n", name, address, latitude, longitude); } else if (strcmp(category, "公共设施") == ) { fprintf(output_file, "公共,%s,%s,%s,%s\n", name, address, latitude, longitude); } } fclose(input_file); fclose(output_file); return ; } ``` 这个代码可以将高德设施poi数据文件中的分类信息重分类为更加通用的分类,例如将餐饮服务、购物服务、生活服务等分类归为餐饮、购物、生活等更加通用的分类。你可以将输入文件和输出文件作为命令行参数传入,例如: ``` ./reclassify_poi input.csv output.csv ``` 其中,input.csv是原始的高德设施poi数据文件,output.csv是重分类后的数据文件。 ### 回答2: 高德设施POI重分类代码示例: import requests import json def amap_poi_classification(api_key, origin_category, new_category, city): url = 'https://restapi.amap.com/v3/place/text' # 查询原始类别的POI params = { 'key': api_key, 'keywords': origin_category, 'city': city, 'extensions': 'all', 'offset': 50 } response = requests.get(url, params=params) data = response.json() # 提取原始类别的POI,并修改为新的类别 pois = data['pois'] for poi in pois: poi['type'] = new_category # 保存为新的POI分类 new_poi_file = f'{origin_category}_to_{new_category}.json' with open(new_poi_file, 'w') as f: json.dump(pois, f, ensure_ascii=False) print(f'成功将{origin_category}类别的POI重分类为{new_category},并保存为{new_poi_file}文件。') if __name__ == '__main__': api_key = '你的高德API密钥' origin_category = '餐饮' new_category = '美食' city = '北京' amap_poi_classification(api_key, origin_category, new_category, city) 这段代码使用高德地图API实现了将指定原始类别的POI重分类为新的类别,并将结果保存为JSON文件的功能。具体的实现步骤如下: 1. 导入requests和json模块,用于发送HTTP请求和处理JSON数据。 2. 定义一个名为amap_poi_classification的函数,接受四个参数:高德API密钥(api_key)、原始类别(origin_category)、新类别(new_category)以及城市(city)。 3. 构造查询原始类别的POI的请求参数,并调用requests.get()发送GET请求获取数据。 4. 解析返回的JSON数据提取其中的POI信息。 5. 遍历每个POI,将其类型(type)修改为新的类别。 6. 将修改后的POI列表保存为JSON文件,文件名格式为"原始类别_to_新类别.json"。 7. 输出操作完成的提示信息。 在主程序中,可以调用amap_poi_classification函数来完成指定POI类别的重分类操作。你需要提供自己的高德API密钥、原始类别、新类别以及城市信息。 ### 回答3: 高德设施POI重分类的代码可以按照以下步骤编写: 1. 导入必要的模块和库,如高德地图APIPython SDK。 2. 创建一个函数,用于进行POI重分类。可以命名为 "reclassify_poi"。 3. 在函数内部,首先通过高德地图API提供的服务,获取所有的设施POI。可以使用 `place_around` 方法,通过传入经纬度和半径参数,来获取指定范围内的POI数据。 4. 根据获取的POI数据,使用条件判断或正则表达式等方法,将POI进行分类。根据自己的需求,可以按照不同的标准进行分类,如医院、学校、酒店等。 5. 将分类后的POI数据存储到适当的数据结构中,如列表或字典。 6.最后返回分类后的POI数据。 以下是一个简单的高德设施POI重分类的代码示例: ```python from amap import AMap def reclassify_poi(latitude, longitude, radius): amap = AMap(api_key='your_api_key') poi_data = amap.place_around(locations=[(longitude, latitude)], radius=radius)['pois'] classified_pois = { 'hospital': [], 'school': [], 'hotel': [] # 添加其他分类 } # 根据条件判断或正则表达式将POI数据分类 for poi in poi_data: if 'hospital' in poi['type']: classified_pois['hospital'].append(poi) elif 'school' in poi['type']: classified_pois['school'].append(poi) elif 'hotel' in poi['type']: classified_pois['hotel'].append(poi) # 添加其他分类的判断条件 return classified_pois latitude = 39.9087 longitude = 116.3975 radius = 1000 result = reclassify_poi(latitude, longitude, radius) print(result) ``` 在代码中,我们首先导入了高德地图APIPython SDK并创建了一个 `reclassify_poi` 函数。然后使用该函数传入经纬度和半径参数,获取指定范围内的POI数据并进行分类。最后打印分类结果。请注意,在使用此代码之前,您需要替换 `your_api_key` 为您自己的高德地图API密钥。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值