通过 API 从空气质量指数 (AQI) 网站检索数据,只收到小号。站数

Retrieving data from the Air Quality Index (AQI) website through the API and only recieving small nr. of stations

我正在做一个个人项目,我正在尝试使用 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 个站点,而 many more available on the actual site. In the API documentation 我看到的唯一限制是“每秒 1,000(一千)个请求”,所以为什么我不能获取更多站点?

此外,我尝试修改经纬度值并设法获得更多站点,但它们在我感兴趣的城市之外。

Here 是我在嵌入代码中使用的实际周长的视图。

如果您对我如何解决这个问题有任何建议,我很乐意阅读您的想法。谢谢!

尝试通过 aqicn 使用 waqi... 不是很干净 API 但我发现它工作得很好

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

查询香港站,returns19

df[df['station_name'].str.contains('HongKong')]