Fundamentals 24 min read

Python Weather Data Scraping, CSV Export, and Visualization with Matplotlib

This article demonstrates how to use Python's requests and BeautifulSoup libraries to scrape current and 14‑day weather data from China Weather Net, store the results in CSV files, and visualize temperature, humidity, air quality, and wind patterns using Matplotlib, NumPy, and Pandas.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Weather Data Scraping, CSV Export, and Visualization with Matplotlib

The article explains a complete workflow for obtaining weather information from the China Weather website using Python. It first requests the 7‑day and 15‑day pages, parses the HTML with BeautifulSoup , and extracts timestamps, temperature, humidity, wind direction, wind level, precipitation, and air‑quality index.

Two main functions are provided: getHTMLtext(url) fetches the raw page content, while get_content(html) processes the 7‑day data and get_content2(html) handles the 15‑day forecast. The extracted values are stored in Python lists and then written to CSV files ( weather1.csv for the current day and weather14.csv for the next 14 days) using the csv module.

After data collection, the script loads the CSV files with pandas.read_csv and creates several visualizations:

A 24‑hour temperature curve with maximum, minimum, and average lines.

A 24‑hour relative humidity curve with similar annotations.

An air‑quality (AQI) bar chart that colors each hour according to the AQI level.

A polar radar chart showing wind direction distribution and average wind speed.

A scatter plot of temperature versus humidity with a calculated correlation coefficient.

A 14‑day high/low temperature line chart and a pie chart of weather condition frequencies.

All visualizations are built with matplotlib.pyplot , and helper functions such as calc_corr compute statistical measures. The code also includes utilities for converting Chinese wind direction strings to degrees for the radar plot.

The article concludes with observations: temperature and humidity are strongly negatively correlated; air quality is influenced by temperature inversions and diurnal cycles; wind patterns show no dominant direction over the 14‑day period; and the 14‑day forecast indicates a cooling trend after day 8.

Finally, the full source code is provided, organized into three modules: weather.py (data crawling and CSV export), data1_analysis.py (visualization of the current day's data), and data14_analysis.py (visualization of the 14‑day forecast). The code snippets are reproduced verbatim within ... blocks to preserve formatting.

pythonCSVData VisualizationWeb ScrapingMatplotlibpandas
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login 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.