爬取天气网的动态信息并存为csv文件,7天最高、低温画图

该代码段实现了一个从指定城市网址抓取天气预报数据的爬虫,包括日期、星期、最高温和最低温等信息,并将数据保存到CSV文件中。之后,读取CSV文件并使用Matplotlib库绘制最高温和最低温度的折线图进行可视化展示。
摘要由CSDN通过智能技术生成
引入
import numpy as np
import pandas as pd
import requests
import re
import csv
import matplotlib.pyplot as plt
import matplotlib
# csv文件的编码格式是gb2312
url = "自己所在城市的网址"
#head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) #Gecko/20100101 Firefox/94.0"}
resp = requests.get(url, headers=head)
resp.encoding = resp.apparent_encoding
# print(resp.text)
obj = re.compile(
    r'<div class="day-item">(?P<week>.*?)<br>(?P<date>.*?)</div>.*?<div class="high">(?P<high>.*?)</div>.*?'
    r'<div class="low">(?P<low>.*?)</div>.*?<div class="day-item">.*?<div class="day-item">'
    r'.*?<div class="day-item">', re.S)
time = re.compile(r'<table class="hour-table" id="hourTable_0" style="">.*?<td>(?P<time1>.*?)</td>'
                  r'.*?<td>(?P<time2>.*?)</td>.*?<td>(?P<time3>.*?)</td>.*?<td>(?P<time4>.*?)</td>'
                  r'.*?<td>(?P<time5>.*?)</td>.*?<td>(?P<time6>.*?)</td>.*?<td>(?P<time7>.*?)</td>'
                  r'.*?<td>(?P<time8>.*?)</td>.*?</tr>', re.S)
temp = re.compile(r'<table class="hour-table" id="hourTable_0" style="">.*?'
                  r'<td style="background-color:#eee;"><i class="iconfont icon-temp"></i> 气温</td>.*?<td>(?P<temp1>.*?)</td>'
                  r'.*?<td>(?P<temp2>.*?)</td>.*?<td>(?P<temp3>.*?)</td>.*?<td>(?P<temp4>.*?)</td>.*?<td>(?P<temp5>.*?)'
                  r'</td>.*?<td>(?P<temp6>.*?)</td>.*?<td>(?P<temp7>.*?)</td>.*?<td>(?P<temp8>.*?)</td>.*?</tr>', re.S)
jsl = re.compile(r'<table class="hour-table" id="hourTable_0" style="">.*?'
                 r'<td style="background-color:#eee;"><i class="iconfont icon-raindrops"></i> 降水</td>.*?<td>(?P<js1>.*?)'
                 r'</td>.*?<td>(?P<js2>.*?)</td>.*?<td>(?P<js3>.*?)</td>.*?<td>(?P<js4>.*?)</td>.*?<td>(?P<js5>.*?)</td>'
                 r'.*?<td>(?P<js6>.*?)</td>.*?<td>(?P<js7>.*?)</td>.*?<td>(?P<js8>.*?)</td>.*?</tr>', re.S)
fs = re.compile(r'<table class="hour-table" id="hourTable_0" style="">.*?'
                r'<td style="background-color:#eee;"><i class="iconfont icon-Windpower"></i> 风速</td>.*?<td>(?P<fs1>.*?)'
                r'</td>.*?<td>(?P<fs2>.*?)</td>.*?<td>(?P<fs3>.*?)</td>.*?<td>(?P<fs4>.*?)</td>.*?<td>(?P<fs5>.*?)</td>'
                r'.*?<td>(?P<fs6>.*?)</td>.*?<td>(?P<fs7>.*?)</td>.*?<td>(?P<fs8>.*?)</td>.*?</tr>', re.S)
fx = re.compile(r'<table class="hour-table" id="hourTable_0" style="">.*?'
                r'<td style="background-color:#eee;"><i class="iconfont icon-fengxiang"></i> 风向</td>.*?<td>(?P<fx1>.*?)'
                r'</td>.*?<td>(?P<fx2>.*?)</td>.*?<td>(?P<fx3>.*?)</td>.*?<td>(?P<fx4>.*?)</td>.*?<td>(?P<fx5>.*?)</td>'
                r'.*?<td>(?P<fx6>.*?)</td>.*?<td>(?P<fx7>.*?)</td>.*?<td>(?P<fx8>.*?)</td>.*?</tr>', re.S)
qy = re.compile(r'<table class="hour-table" id="hourTable_0" style="">.*?'
                r'<td style="background-color:#eee;"><i class="iconfont icon-qiya"></i> 气压</td>.*?<td>(?P<qy1>.*?)'
                r'</td>.*?<td>(?P<qy2>.*?)</td>.*?<td>(?P<qy3>.*?)</td>.*?<td>(?P<qy4>.*?)</td>.*?<td>(?P<qy5>.*?)</td>'
                r'.*?<td>(?P<qy6>.*?)</td>.*?<td>(?P<qy7>.*?)</td>.*?<td>(?P<qy8>.*?)</td>.*?</tr>', re.S)
sd = re.compile(r'<table class="hour-table" id="hourTable_0" style="">.*?'
                r'<td style="background-color:#eee;"><i class="iconfont icon-humidity"></i> 湿度</td>.*?<td>(?P<sd1>.*?)'
                r'</td>.*?<td>(?P<sd2>.*?)</td>.*?<td>(?P<sd3>.*?)</td>.*?<td>(?P<sd4>.*?)</td>.*?<td>(?P<sd5>.*?)</td>'
                r'.*?<td>(?P<sd6>.*?)</td>.*?<td>(?P<sd7>.*?)</td>.*?<td>(?P<sd8>.*?)</td>.*?</tr>', re.S)
yl = re.compile(r'<table class="hour-table" id="hourTable_0" style="">.*?'
                r'<td style="background-color:#eee;"><i class="iconfont icon-yunliang_huabanfuben"></i> 云量</td>.*?<td>(?P<yl1>.*?)'
                r'</td>.*?<td>(?P<yl2>.*?)</td>.*?<td>(?P<yl3>.*?)</td>.*?<td>(?P<yl4>.*?)</td>.*?<td>(?P<yl5>.*?)</td>'
                r'.*?<td>(?P<yl6>.*?)</td>.*?<td>(?P<yl7>.*?)</td>.*?<td>(?P<yl8>.*?)</td>.*?</tr>', re.S)

result = obj.finditer(resp.text)
for item in result:
    dic = item.groupdict()
    dic['high'] = dic['high'].strip()
    dic['low'] = dic['low'].strip()
    dic['week'] = dic['week'].strip()
    dic['date'] = dic['date'].strip()
    f = open('weatherdata.csv', 'a', newline='')
    w = csv.writer(f)
    w.writerow(dic.values())
    f.close()

for item in time.finditer(resp.text):
    dic = item.groupdict()
    f = open('weather-time.csv', 'a', newline='')
    w = csv.writer(f)
    w.writerow(dic.values())
    f.close()

for item in temp.finditer(resp.text):
    dic = item.groupdict()
    f = open('weather-time.csv', 'a', newline='')
    w = csv.writer(f)
    w.writerow(dic.values())
    f.close()

for item in jsl.finditer(resp.text):
    dic = item.groupdict()
    f = open('weather-time.csv', 'a', newline='')
    w = csv.writer(f)
    w.writerow(dic.values())
    f.close()

for item in fs.finditer(resp.text):
    dic = item.groupdict()
    f = open('weather-time.csv', 'a', newline='')
    w = csv.writer(f)
    w.writerow(dic.values())
    f.close()

for item in fx.finditer(resp.text):
    dic = item.groupdict()
    f = open('weather-time.csv', 'a', newline='')
    w = csv.writer(f)
    w.writerow(dic.values())
    f.close()

for item in qy.finditer(resp.text):
    dic = item.groupdict()
    f = open('weather-time.csv', 'a', newline='')
    w = csv.writer(f)
    w.writerow(dic.values())
    f.close()

for item in sd.finditer(resp.text):
    dic = item.groupdict()
    f = open('weather-time.csv', 'a', newline='')
    w = csv.writer(f)
    w.writerow(dic.values())
    f.close()

for item in yl.finditer(resp.text):
    dic = item.groupdict()
    f = open('weather-time.csv', 'a', newline='')
    w = csv.writer(f)
    w.writerow(dic.values())
    f.close()
f = open('weather-time.csv', 'r')
r = csv.reader(f)
for i in r:
    print(i)
header = ["星期", "日期", "最高温", "最低温"]
filename='weatherdata.csv'
df = pd.read_csv(filename, encoding='gb2312', names=header)
tempH = df["最高温"]
tempL = df["最低温"]
H = []
for i in tempH:
    H.append(int(i[:-1]))
H = np.array(H)
L = []
for i in tempL:
    L.append(int(i[:-1]))
L = np.array(L)
matplotlib.rcParams['font.sans-serif']=['SimHei']
plt.plot(H, "r", markersize=5,label='最高温')
plt.plot(L, "b", markersize=5,label='最低温')
plt.ylabel("摄氏度/℃")
plt.legend(loc='lower right')
plt.show()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值