[python crawler] Forecast of the Central Meteorological Bureau - static web page image crawling practice

A brief introduction to the forecast of the Central Meteorological Bureau

  • Central Meteorological Observatory is a seven-day precipitation forecast page issued by the China Meteorological Administration (Central Meteorological Observatory). This page provides the precipitation forecast for various regions in the coming week, helping people understand the upcoming precipitation and take corresponding countermeasures. The forecast content on the page usually includes the following points:

    • Regional distribution : The page shows the precipitation forecast for each region in China. Regions may be presented in a map, table, or other format showing the expected spatial distribution of precipitation.

    • Time Frame : Forecasts typically cover the week ahead and usually show daily precipitation forecasts. Users can use this information to predict the precipitation for the next few days.

    • Forecast Values : Forecast values ​​for each region are usually expressed in millimeters (mm) and represent the amount of precipitation expected for a specific time period. This can help people understand the intensity and duration of precipitation.

    • Trend analysis : Some forecast pages may provide a trend analysis of precipitation in the next few days, such as whether it will gradually increase or decrease, and the possible precipitation type (such as rain, snow, freezing rain, etc.).

    • Early warning information : Sometimes, the page may also display early warning information related to precipitation, such as heavy rain, heavy snow and other meteorological disaster warnings, to remind people to take corresponding preventive measures.

Preparatory steps

Python crawls daily forecast results - taking precipitation as an example

  • Python code crawling
import requests
from bs4 import BeautifulSoup as BS

# 获取中央气象局HTML信息
url = requests.get("http://www.nmc.cn/publish/precipitation/1-7day-precipitation.html")
# 解析中央气象局HTML信息
url_soup = BS(url.content, 'html.parser', from_encoding='utf-8')
# 查找网页中的预报图像 —— 查看网页源码,查找相应内容
image_node = url_soup.find('img', id='imgpath')
# 获取预报图像链接
image_url = image_node["src"]

# 获取图像
response = requests.get(image_url)
image_data = response.content
# 保存图像
with open('downloaded_image.jpg', 'wb') as f:
    f.write(image_data)
    print("图片已下载并保存为 downloaded_image.jpg")
  • crawl results
    insert image description here

Guess you like

Origin blog.csdn.net/qq_38734327/article/details/132569772