1.购买服务器阿里云:服务器购买地址https://t.aliyun.com/U/PfsP97若失效,可用地址
阿里云:
服务器购买地址
https://t.aliyun.com/U/PfsP97
若失效,可用地址
https://www.aliyun.com/activity/wuying/dj?source=5176.29345612&userCode=49hts92d
腾讯云:
https://curl.qcloud.com/wJpWmSfU
若失效,可用地址
https://cloud.tencent.com/act/cps/redirect?redirect=2446&cps_key=ad201ee2ef3b771157f72ee5464b1fea&from=console
华为云
https://activity.huaweicloud.com/cps.html?fromacct=64b5cf7cc11b4840bb4ed2ea0b2f4468&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905
2.部署教程
3.代码如下
import csv
import time
from DrissionPage import ChromiumPage
from DrissionPage import ChromiumOptions
def get_weather(date):
# 设置无头模式
options = ChromiumOptions()
options.headless()
# 实例化浏览器对象,打开对应网页并设置窗口最大化
page = ChromiumPage(options)
page.get(f'https://lishi.tianqi.com/shanghai/{date}.html')
page.set.window.max()
# 点击查看更多按钮
page.ele('xpath=//div[@class="lishidesc2"]').click()
time.sleep(1)
# 筛选当月所有的天气标签
weather_list = page.eles('xpath=//ul[@class="thrui"]/li')
# 定义空列表,用来存放每月的天气数据
weather_data = []
# 遍历出当月每天的天气标签
for weather_info in weather_list:
# 定义每天的天气数据字典
day_weather_info = {
# 筛选出当天天气标签中的时间、最高气温、最低气温、天气详情
'date_time': weather_info.ele('xpath=./div[1]').text.split(' ')[0],
'high': weather_info.ele('xpath=./div[2]').text.replace('℃', ''),
'low': weather_info.ele('xpath=./div[3]').text.replace('℃', ''),
'weather': weather_info.ele('xpath=./div[4]').text
}
# 再将每天的数据字典放入每月数据列表中
weather_data.append(day_weather_info)
print(f'{date}天气抓取完毕')
# 返回每个月的天气数据列表
return weather_data
def get_year_weather():
# 全年的天气数据列表 大概长这样子 :[ [ {},{},.. ],[{},{},..],[{},{},..], ]
# 外层列表:年数据,里层列表:月数据,里层列表中的每个字典:这个月中每天的气候数据
weather_list = []
# for循环生成有顺序的1-12
for month in range(1, 13):
# 找url规律 进行拼接 -- 拿的是某一月里的所有数据
# 获取对应月份的天气信息
weather_time = f'2024{month:02}'
# 爬虫获取这个月的天气信息
weather = get_weather(weather_time)
# print(weather)
# 存到列表中
weather_list.append(weather)
print('全年天气为:', weather_list)
return weather_list
def writer_csv(weathers):
# 数据写入(一次性写入)
with open("weather.csv", "w", newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
# 先写入列名:columns_name
writer.writerow(["日期", "最高气温", "最低气温", '天气'])
# 遍历出每个月的天气信息
for month_weather in weathers:
# 遍历出每天的天气信息
for day_weather_dict in month_weather:
# 将每天的天气信息写入csv文件
writer.writerow(list(day_weather_dict.values()))
print('全天天气保存到csv文件中')
if __name__ == '__main__':
# 调用get_year_weather函数获取全年天气
weather_list = get_year_weather()
# 调用writer_csv函数将获取的全年天气写入到csv文件中
writer_csv(weather_list)
解析
该脚本为天气数据采集自动化爬虫脚本,主要作用为
使用 DrissionPage + Chromium 去访问 历史天气网,按月份抓取指定城市(脚本里是上海)的历史天气数据(日期、最高气温、最低气温、天气情况),并最终把全年数据写入到 weather.csv
文件里。
主要方法
1. get_weather(date)
功能:获取某个月的天气数据。
流程:
日期 (
date_time
)最高气温 (
high
)最低气温 (
low
)天气状况 (
weather
)启动无头浏览器,打开对应月份的历史天气页面。
点击"查看更多"按钮,确保页面加载完整。
抓取页面中所有日期的天气条目。
解析出每天的:
返回该月的天气数据列表。
2. get_year_weather()
功能:遍历全年,收集 1-12 月的天气数据。
流程:
循环 1-12 生成月份字符串(如
"202401"
~"202412"
)。调用
get_weather()
抓取每个月的数据。把所有月份的数据存入一个大列表。
返回全年数据。
3. writer_csv(weathers)
功能:把全年天气数据写入 CSV 文件。
流程:
打开(或新建)
weather.csv
文件。写入表头:
["日期", "最高气温", "最低气温", "天气"]
。遍历全年数据 → 每个月数据 → 每天数据,逐行写入。
保存并关闭文件。
4. if __name__ == '__main__':
脚本入口:
调用
get_year_weather()
获取全年天气。调用
writer_csv()
保存为 CSV。
注意:
本文部分变量已做脱敏处理,仅用于测试和学习研究,禁止用于商业用途,不能保证其合法性,准确性,完整性和有效性,请根据情况自行判断。技术层面需要提供帮助,可以通过打赏的方式进行探讨。
没有评论:
发表评论