一、主题页面的结构与特性分析
1.主题页面的结构与特性分析
目的内容界面:
2. Htmls 页面剖析
3.节点查找方法与遍历方法
查找方法:find(): 查找第一个匹配到的节点。find_all(): 查找全部匹配到的节点,并返回一个列表。
遍历方法:contents: 返回当前节点的直接子节点列表。 children: 返回当前节点的直接子节点的迭代器。descendants: 返回当前节点的全部子孙节点的迭代器。
parent: 返回当前节点的父节点。parents: 返回当前节点的全部先人节点的迭代器。
二、网络爬虫步伐计划
1.数据爬取与收罗
数据源:https://lishi.tianqi.com/quanzhou/
所用到的库有
- 1 import requests # 模拟浏览器进行网络请求
- 2 from lxml import etree # 进行数据预处理
- 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 for li in resp_list: 2 day_weather_info = {} 3 # 日期
- 4 day_weather_info['date_time'] = li.xpath("./div[1]/text()")[0].split(' ')[0]
- 5 # 最高气温 (包含摄氏度符号)
- 6 high = li.xpath("./div[2]/text()")[0]
- 7 day_weather_info['high'] = high[:high.find('℃')]
- 8 # 最低气温
- 9 low = li.xpath("./div[3]/text()")[0]
- 10 day_weather_info['low'] = low[:low.find('℃')]
- 11 # 天气
- 12 day_weather_info['weather'] = li.xpath("./div[4]/text()")[0]
- 13 weather_info.append(day_weather_info)
- 14 return weather_info
复制代码
然后我们便得到了我们所须要的数据

接着爬取我们这个月的气候信息,存入列表中,然一次性写入我们的csv文件中,如许我们就得到了一个存有泉州2022整年气候环境的文件
- # for循环生成有顺序的1-12
- for month in range(1, 13):
- # 获取某一月的天气信息
- # 三元表达式
- weather_time = '2022' + ('0' + str(month) if month < 10 else str(month))
- print(weather_time)
- url = f'https://lishi.tianqi.com/quanzhou/{weather_time}.html'
- # 爬虫获取这个月的天气信息
- weather = getWeather(url)
- # 存到列表中
- weathers.append(weather)
- print(weathers)
- # 数据写入(一次性写入)
- with open("weather.csv", "w",newline='') as csvfile:
- writer = csv.writer(csvfile)
- # 先写入列名:columns_name 日期 最高气温 最低气温 天气
- writer.writerow(["日期", "最高气温", "最低气温", '天气'])
- # 一次写入多行用writerows(写入的数据类型是列表,一个列表对应一行)
- writer.writerows([list(day_weather_dict.values()) for month_weather in weathers for day_weather_dict in month_weather])
- import sqlite3
复制代码
文件如下:
3.对我们的数据举行一下词云处置惩罚
所用到的库
- 1 import requests
- 2 from lxml import etree
- 3 import csv
- 4 from wordcloud import WordCloud
- 5 import matplotlib.pyplot as plt
复制代码 然后对数据在举行一次爬取与清算
- 1 # 从URL获取天气信息的函数
- 2 def getWeather(url): 3 weather_info = [] # 存储天气信息的列表
- 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'
- 6 }
- 7 resp = requests.get(url, headers=headers) # 发送GET请求到指定的URL
- 8 resp_html = etree.HTML(resp.text) # 解析响应的HTML
- 9 resp_list = resp_html.xpath("//ul[@class='thrui']/li") # 使用XPath选择器提取天气信息列表
- 10 for li in resp_list:
- 11 day_weather_info = {} # 存储每天天气信息的字典
- 12 day_weather_info['date_time'] = li.xpath("./div[1]/text()")[0].split(' ')[0] # 提取日期时间并存入字典
- 13 high = li.xpath("./div[2]/text()")[0] # 提取最高温度
- 14 day_weather_info['high'] = high[:high.find('℃')] # 去除温度单位并存入字典
- 15 low = li.xpath("./div[3]/text()")[0] # 提取最低温度
- 16 day_weather_info['low'] = low[:low.find('℃')] # 去除温度单位并存入字典
- 17 day_weather_info['weather'] = li.xpath("./div[4]/text()")[0] # 提取天气情况并存入字典
- 18 weather_info.append(day_weather_info) # 将每天天气信息字典添加到天气信息列表中
- 19 return weather_info
- 20 def main():
- 21 w
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |