我正在开展一个个人项目,我正在尝试使用他们的 API 从 https://aqicn.org 网站检索空气质量数据。
我使用了这段代码,我已将其复制并适应布加勒斯特市,如下所示:
import pandas as pd
import folium
import requests
# GET data from AQI website through the API
base_url = "https://api.waqi.info"
path_to_file = "~/path"
# Got token from:- https://aqicn.org/data-platform/token/#/
with open(path_to_file) as f:
contents = f.readlines()
key = contents[0]
# (lat, long)-> bottom left, (lat, lon)-> top right
latlngbox = "44.300264,25.920181,44.566991,26.297836" # For Bucharest
trail_url=f"/map/bounds/?token={key}&latlng={latlngbox}" #
my_data = pd.read_json(base_url + trail_url) # Joined parts of URL
print('columns->', my_data.columns) #2 cols ‘status’ and ‘data’ JSON
### Built a dataframe from the json file
all_rows = []
for each_row in my_data['data']:
all_rows.append([each_row['station']['name'],
each_row['lat'],
each_row['lon'],
each_row['aqi']])
df = pd.DataFrame(all_rows, columns=['station_name', 'lat', 'lon', 'aqi'])
# Cleaned the DataFrame
df['aqi'] = pd.to_numeric(df.aqi, errors='coerce') # Invalid parsing to NaN
# Remove NaN entries in col
df1 = df.dropna(subset = ['aqi'])
不幸的是,它只检索 4 个电台,而实际站点上还有 更多可用的电台。在 API 文档 中,我看到的唯一限制是“每秒 1,000(一千)个请求”,那么为什么我不能获得更多呢?
此外,我尝试修改经纬度值并设法获得更多站点,但它们位于我感兴趣的城市之外。如果您对如何解决此问题有任何建议,我很乐意阅读您的想法。谢谢!
import pandas as pd
url1 = 'https://api.waqi.info'
# Get token from:- https://aqicn.org/data-platform/token/#/
token = 'XXX'
box = '113.805332,22.148942,114.434299,22.561716' # polygon around HongKong via bboxfinder.com
url2=f'/map/bounds/?latlng={box}&token={token}'
my_data = pd.read_json(url1 + url2)
all_rows = []
for each_row in my_data['data']:
all_rows.append([each_row['station']['name'],each_row['lat'],each_row['lon'],each_row['aqi']])
df = pd.DataFrame(all_rows,columns=['station_name', 'lat', 'lon', 'aqi'])
从那里很容易绘制
df['aqi'] = pd.to_numeric(df.aqi,errors='coerce')
print('with NaN->', df.shape)
df1 = df.dropna(subset = ['aqi'])
df2 = df1[['lat', 'lon', 'aqi']]
init_loc = [22.396428, 114.109497]
max_aqi = int(df1['aqi'].max())
print('max_aqi->', max_aqi)
m = folium.Map(location = init_loc, zoom_start = 5)
heat_aqi = HeatMap(df2, min_opacity = 0.1, max_val = max_aqi,
radius = 60, blur = 20, max_zoom = 2)
m.add_child(heat_aqi)
m
或者这样
centre_point = [22.396428, 114.109497]
m2 = folium.Map(location = centre_point,tiles = 'Stamen Terrain', zoom_start= 6)
for idx, row in df1.iterrows():
lat = row['lat']
lon = row['lon']
station = row['station_name'] + ' AQI=' + str(row['aqi'])
station_aqi = row['aqi']
if station_aqi > 300:
pop_color = 'red'
elif station_aqi > 200:
pop_color = 'orange'
else:
pop_color = 'green'
folium.Marker(location= [lat, lon],
popup = station,
icon = folium.Icon(color = pop_color)).add_to(m2)
m2
查询香港境内的电台,返回19
df[df['station_name'].str.contains('HongKong')]
requests
库的通用方法。第 1 步:API 文档
首先,查找 AQI API 的官方文档,了解其端点、参数和限制。
第2步:API使用
您需要向 API 发送请求以检索数据。 API 可能具有指定站数或其他过滤选项的参数。使用这些参数来获取更多数据。这是一个简化的示例:
```python
import requests
# Replace with the actual API endpoint and API key
API_ENDPOINT = 'https://api.example.com/aqi'
API_KEY = 'your_api_key_here'
# Define parameters for the request
params = {
'api_key': API_KEY,
'limit': 100, # Adjust this limit as needed, it may vary based on the API
}
try:
response = requests.get(API_ENDPOINT, params=params)
response_data = response.json()
# Process the data here
for station in response_data['stations']:
# Process each station's data
print(station)
except requests.exceptions.RequestException as e:
print('Error:', e)
```
第 3 步:分页(如果需要)如果 API 对数据进行分页,您可能需要发出多个请求来检索大量站点的数据。查看 API 文档,了解如何对数据进行分页的信息。
第 4 步:错误处理和速率限制
始终优雅地处理错误并遵守 API 施加的任何速率限制。如果 API 有速率限制,请务必遵守,以免被阻止。
请记住将
API_ENDPOINT
和
API_KEY
替换为您正在使用的 AQI 服务提供的实际 API 详细信息。此外,请注意,这是一个基本示例,实际实现可能会根据 AQI API 的具体情况和要求而有所不同。请务必彻底查看 API 文档。