爬取天气预报数据通常涉及以下步骤:
确定目标网站和URL
选择一个可靠的天气预报网站,如中国气象局、Weather.com、AccuWeather等。
查找并记录下包含天气数据的网页URL。
发起HTTP请求
使用Python的`requests`库发起GET请求,并设置请求头,如`User-Agent`。
解析HTML页面
使用`BeautifulSoup`或Python内置的`re`库解析HTML页面,提取天气数据。
分析网页结构,找到包含天气信息的HTML标签和属性。
数据处理和存储
对提取到的天气数据进行处理和分析。
根据需要将数据保存到文件,如CSV或JSON格式。

import requestsfrom bs4 import BeautifulSoup构造目标URLurl = 'http://www.weather.com.cn/weather/101270101.shtml'发起HTTP请求response = requests.get(url)解析HTML内容soup = BeautifulSoup(response.text, 'html.parser')提取天气信息tagDate = soup.find('ul', class_='t clearfix')dates = tagDate.h1.stringtagToday = soup.find('p', class_='tem')try:temperatureHigh = tagToday.span.stringexcept AttributeError as e:temperatureHigh = tagToday.find_next('p', class_='tem').span.stringtemperatureLow = tagToday.i.stringweather = soup.find('p', class_='wea').stringtagWind = soup.find('p', class_='win')winL = tagWind.i.string打印天气信息print('今天是:', dates)print('最高温度:', temperatureHigh)print('最低温度:', temperatureLow)print('天气状况:', weather)print('风力:', winL)
请注意,实际使用时,需要根据目标网站的具体HTML结构来调整解析代码。同时,考虑到网站的更新和变化,可能需要定期检查和更新爬虫代码以适应这些变化。
