爬取天气预报数据通常涉及以下步骤:
确定目标网站和URL
选择一个可靠的天气预报网站,如中国气象局、Weather.com、AccuWeather等。
查找并记录下包含天气数据的网页URL。
发起HTTP请求
使用Python的`requests`库发起GET请求,并设置请求头,如`User-Agent`。
解析HTML页面
使用`BeautifulSoup`或Python内置的`re`库解析HTML页面,提取天气数据。
分析网页结构,找到包含天气信息的HTML标签和属性。
数据处理和存储
对提取到的天气数据进行处理和分析。
根据需要将数据保存到文件,如CSV或JSON格式。
import requests
from bs4 import BeautifulSoup
构造目标URL
url = '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.string
tagToday = soup.find('p', class_='tem')
try:
temperatureHigh = tagToday.span.string
except AttributeError as e:
temperatureHigh = tagToday.find_next('p', class_='tem').span.string
temperatureLow = tagToday.i.string
weather = soup.find('p', class_='wea').string
tagWind = soup.find('p', class_='win')
winL = tagWind.i.string
打印天气信息
print('今天是:', dates)
print('最高温度:', temperatureHigh)
print('最低温度:', temperatureLow)
print('天气状况:', weather)
print('风力:', winL)
请注意,实际使用时,需要根据目标网站的具体HTML结构来调整解析代码。同时,考虑到网站的更新和变化,可能需要定期检查和更新爬虫代码以适应这些变化。