欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

爬取12个月天气数据/csv数据去除符号并转存为新的csv/绘出最高气温与最低气温折线图并显示在一个表里面

程序员文章站 2022-07-14 16:50:54
...

纪念一下搞出来的一个东西,不过都是参考网上的,只懂一些表面的,不会深层的东西,欢迎批评指正

1.爬取2019年7月到2020年6月阜阳地区的天气:

#爬取气温代码
months = []
for year in (2019,):
    for month in range(6,12):
        months.append("%d%02d"%(year, month+1))
for year in (2020,):
    for month in range(0,6):
        months.append("%d%02d"%(year, month+1))
todo_urls = [
    f"http://tianqi.2345.com/t/wea_history/js/{month}/58203_{month}.js"

    for month in months
]
import requests

datas = []
for url in todo_urls:
    r = requests.get(url)
    if r.status_code!=200:
        raise Exception()
    # 去除javascript前后的字符串,得到一个js格式的JSON
    data = r.text.lstrip("var weather_str=").rstrip(";")
    datas.append(data)
import demjson
demjson.decode(datas[0])
tqInfos = demjson.decode(datas[0])["tqInfo"]
# 解析所有月份的数据
all_datas = []

for data in datas:
    tqInfos = demjson.decode(data)["tqInfo"]
    all_datas.extend([x for x in tqInfos if len(x)>0])
len(all_datas)
import csv

with open('./fy_tianqi_2019-2020.csv', 'w', newline='', encoding='utf-8') as csv_file:
    writer = csv.writer(csv_file)

    columns = ['ymd', 'bWendu', 'yWendu', 'tianqi', 'fengxiang', 'fengli', 'aqi', 'aqiInfo', 'aqiLevel']
    writer.writerow(columns)

    for data in all_datas:
        writer.writerow([data[column] for column in columns])

爬出来的数据示例:

爬取12个月天气数据/csv数据去除符号并转存为新的csv/绘出最高气温与最低气温折线图并显示在一个表里面

2.去除上csv表中的摄氏度符号,并转存为新的csv:

#去除摄氏度符号,再转存为datadatadata.csv
import re
#正则去标
import csv
import codecs
import json
import pandas as pd
import numpy
"""
def etl(content):
    content = re.sub("[℃]+", " ", content)
    content = re.sub(r" {2,}", " ", content)
    return content
"""
list = []
with open('fy_tianqi_2019-2020.csv', 'rt',encoding='UTF-8') as f:
    reader = csv.reader(f)
    header_row = next(reader)
    for row in reader:
        for word in row:
            str2 = word.replace('℃', '')
            list.append(str2)
            #print(str2)
#print(list)
#将一维列表转置为二维列表,方便后面转存
b=numpy.array(list).reshape(344,9)
print(b)
print(numpy.shape(b))
#给新表添加表头
name = ["ymd","bWendu","yWendu","tianqi","fengxiang","fengli","aqi","aqiInfo","aqiLevel"]
csv_list = pd.DataFrame(columns=name, data=b)
#转存为新的csv表
csv_list.to_csv('datadatadata1.csv')

如下,转存好的已经去除了摄氏度符号:

爬取12个月天气数据/csv数据去除符号并转存为新的csv/绘出最高气温与最低气温折线图并显示在一个表里面

3.根据上面的数据表可视化出12个月的最高气温和最低气温变化图,并将两个折线图显示在一个表里面

#画出最高气温和最低气温的天气折线图
import csv
with open('datadatadata.csv', 'rt',encoding="utf-8") as f:
    reader = csv.reader(f)
    header_row = next(reader)
    #for row in reader:
      #  print(row)
    date = []
    max_temperature = []
    low_temperature = []
    for row in reader:
        #date.append(row[1])
        max_temperature.append(int(row[2]))
        low_temperature.append(int(row[3]))
print(max_temperature)
import matplotlib.pyplot as plt

plt.plot(max_temperature,c='red')
plt.plot(low_temperature,c='blue')
plt.title("The Daily high and low temperstures of FuYang -2019_7——2020_6",fontsize=24)
plt.show()

爬取12个月天气数据/csv数据去除符号并转存为新的csv/绘出最高气温与最低气温折线图并显示在一个表里面

相关标签: 代码