python爬虫——爬取整年气候数据并做可视化分析 [复制链接]
发表于 2025-11-18 13:58:35 | 显示全部楼层 |阅读模式
一、主题页面的结构与特性分析

1.主题页面的结构与特性分析

目的内容界面:
2. Htmls 页面剖析


3.节点查找方法与遍历方法

查找方法:find(): 查找第一个匹配到的节点。find_all(): 查找全部匹配到的节点,并返回一个列表。
遍历方法:contents: 返回当前节点的直接子节点列表。 children: 返回当前节点的直接子节点的迭代器。descendants: 返回当前节点的全部子孙节点的迭代器。
parent: 返回当前节点的父节点。parents: 返回当前节点的全部先人节点的迭代器。
二、网络爬虫步伐计划

1.数据爬取与收罗

数据源:https://lishi.tianqi.com/quanzhou/
所用到的库有
  1. 1 import requests # 模拟浏览器进行网络请求
  2. 2 from lxml import etree # 进行数据预处理
  3. 3 import csv # 进行写入csv文件
复制代码

利用requests中的get方法对网站发出哀求,并吸收相应数据,
1 resp = requests.get(url, headers=headers)
我们便得到了网页的源代码数据,

2.对数据举行洗濯和处置惩罚

然后对爬取的网站源代码举行预处置惩罚
1 resp_html = etree.HTML(resp.text)

利用xpath工具提取我们所须要的数据
1 resp_list = resp_html.xpath(“//ul[@class=‘thrui’]/li”)

创建一个字典,并利用for循环将我们所提取的数据,放入字典中
  1. 1 for li in resp_list: 2 day_weather_info = {} 3 # 日期
  2. 4 day_weather_info['date_time'] = li.xpath("./div[1]/text()")[0].split(' ')[0]
  3. 5 # 最高气温 (包含摄氏度符号)
  4. 6 high = li.xpath("./div[2]/text()")[0]
  5. 7 day_weather_info['high'] = high[:high.find('℃')]
  6. 8 # 最低气温
  7. 9 low = li.xpath("./div[3]/text()")[0]
  8. 10 day_weather_info['low'] = low[:low.find('℃')]
  9. 11 # 天气
  10. 12 day_weather_info['weather'] = li.xpath("./div[4]/text()")[0]
  11. 13 weather_info.append(day_weather_info)
  12. 14 return weather_info
复制代码

然后我们便得到了我们所须要的数据

接着爬取我们这个月的气候信息,存入列表中,然一次性写入我们的csv文件中,如许我们就得到了一个存有泉州2022整年气候环境的文件
  1. # for循环生成有顺序的1-12
  2. for month in range(1, 13):
  3.     # 获取某一月的天气信息
  4.     # 三元表达式
  5.     weather_time = '2022' + ('0' + str(month) if month < 10 else str(month))
  6.     print(weather_time)
  7.     url = f'https://lishi.tianqi.com/quanzhou/{weather_time}.html'
  8.     # 爬虫获取这个月的天气信息
  9.     weather = getWeather(url)
  10.     # 存到列表中
  11.     weathers.append(weather)
  12. print(weathers)
  13. # 数据写入(一次性写入)
  14. with open("weather.csv", "w",newline='') as csvfile:
  15.     writer = csv.writer(csvfile)
  16.     # 先写入列名:columns_name 日期 最高气温 最低气温  天气
  17.     writer.writerow(["日期", "最高气温", "最低气温", '天气'])
  18.     # 一次写入多行用writerows(写入的数据类型是列表,一个列表对应一行)
  19.     writer.writerows([list(day_weather_dict.values()) for month_weather in weathers for day_weather_dict in month_weather])
  20. import sqlite3
复制代码

文件如下:

3.对我们的数据举行一下词云处置惩罚

所用到的库
  1. 1 import requests
  2. 2 from lxml import etree
  3. 3 import csv
  4. 4 from wordcloud import WordCloud
  5. 5 import matplotlib.pyplot as plt
复制代码
然后对数据在举行一次爬取与清算
  1. 1 # 从URL获取天气信息的函数
  2. 2 def getWeather(url): 3     weather_info = []  # 存储天气信息的列表
  3. 4     headers = { 5         'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1'
  4. 6     }
  5. 7     resp = requests.get(url, headers=headers)  # 发送GET请求到指定的URL
  6. 8     resp_html = etree.HTML(resp.text)  # 解析响应的HTML
  7. 9     resp_list = resp_html.xpath("//ul[@class='thrui']/li")  # 使用XPath选择器提取天气信息列表
  8. 10     for li in resp_list:
  9. 11         day_weather_info = {}  # 存储每天天气信息的字典
  10. 12         day_weather_info['date_time'] = li.xpath("./div[1]/text()")[0].split(' ')[0]  # 提取日期时间并存入字典
  11. 13         high = li.xpath("./div[2]/text()")[0]  # 提取最高温度
  12. 14         day_weather_info['high'] = high[:high.find('℃')]  # 去除温度单位并存入字典
  13. 15         low = li.xpath("./div[3]/text()")[0]  # 提取最低温度
  14. 16         day_weather_info['low'] = low[:low.find('℃')]  # 去除温度单位并存入字典
  15. 17         day_weather_info['weather'] = li.xpath("./div[4]/text()")[0]  # 提取天气情况并存入字典
  16. 18         weather_info.append(day_weather_info)  # 将每天天气信息字典添加到天气信息列表中
  17. 19     return weather_info
  18. 20 def main():
  19. 21     w
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表