How to Map China’s Qingming Rainfall Using Free Weather APIs and Python

This guide shows how to collect nationwide weather data for the Qingming Festival via the free QWeather API, process it with Python to generate a CSV file, and visualize the rainfall distribution across China using a desktop data‑visualization tool.

FunTester
FunTester
FunTester
How to Map China’s Qingming Rainfall Using Free Weather APIs and Python

During the Qingming Festival many people wonder whether it rains across all of China. This article demonstrates a practical method to retrieve weather data for the whole country on the festival day, store it locally, and visualize the rain distribution.

Data acquisition

The free QWeather service provides two essential endpoints: one for obtaining city IDs by province and another for fetching three‑day weather forecasts. The relevant URLs are:

https://geoapi.qweather.com/v2/city/lookup?location={province}&key=YOUR_KEY
https://devapi.qweather.com/v7/weather/3d?location={city_id}&key=YOUR_KEY

After registering for an API key (one account allows up to 10,000 calls per day), these endpoints can be used to collect the needed data.

Python script

The script performs three main steps:

Scrape the list of provinces from the China Weather website.

For each province, request city IDs, filter out non‑Chinese entries, then request the three‑day forecast and extract the record for 2023‑04‑05 (the Qingming day).

Write the province, city name, daytime description, minimum and maximum temperatures, and precipitation to a CSV file.

Key code snippets:

import requests
from parsel import Selector
import json

# Get province list
def getProvinces():
    province = []
    url_province = 'http://www.weather.com.cn/weathern/101070201.shtml'
    re = requests.get(url=url_province)
    select = Selector(re.content.decode('utf-8'))
    result_province = select.xpath("/html/body/div[4]/div[1]/div/div/a")
    for url_privince in result_province:
        url = url_privince.xpath('@href').extract_first()
        text = url_privince.xpath('text()').extract_first()
        province.append(text)
    return province

# Get weather data for each city
def getWeather(provinces, key):
    for province in provinces:
        url_citys = f'https://geoapi.qweather.com/v2/city/lookup?location={province}&key={key}'
        res_city = requests.get(url_citys)
        data_city = json.loads(res_city.text)
        for city in data_city['location']:
            if city['country'] == '中国' and province in city['adm1']:
                url_weather = f"https://devapi.qweather.com/v7/weather/3d?location={city['id']}&key={key}"
                res_weather = requests.get(url_weather)
                data_city = json.loads(res_weather.text)
                try:
                    for day in data_city['daily']:
                        if day['fxDate'] == '2023-04-05':
                            textDay = day['textDay']
                            tempMax = day['tempMax']
                            tempMin = day['tempMin']
                            precip = day['precip']
                            path = r'C:\Users\edz\Desktop\excel\weather.csv'
                            with open(path, 'a+') as f:
                                line = province + ',' + city['name'] + ',' + textDay + ',' + tempMin + ',' + tempMax + ',' + precip + '
'
                                f.write(line)
                except Exception as e:
                    print('error: %s' % e)

if __name__ == '__main__':
    key = '73d20be1a74a43fea3894f72d8fbc845'
    provinces = getProvinces()
    getWeather(provinces, key)

Running the script creates a weather.csv file containing rows such as:

Province,City,Weather,TempMin,TempMax,Precipitation

Visualization

The CSV can be imported into the free Yonghong Desktop data‑visualization tool. After uploading the file, a map chart can be generated to display the rainfall distribution across China, clearly showing which regions experienced rain on Qingming.

This end‑to‑end workflow provides a reproducible way to analyze and visualize nationwide weather patterns for any specific date.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonCSVData visualizationWeb ScrapingWeather APIQWeather
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.