资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

不知道天气咋样?一起用Python爬取天气数据分析告诉你

来源:公司资讯 | 2021.08.17

前语
今日咱们共享一个小案例,获取气候数据,进行可视化剖析,带你直观了解气候状况!

一、中心功能设计
整体来说,咱们需求先对我国气候网中的气候数据进行爬取,保存为csv文件,并将这些数据进行可视化剖析展示。

拆解需求,大致能够整理出咱们需求分为以下几步结束:

通过爬虫获取我国气候网7.20-7.21的降雨数据,包含城市,风力方向,风级,降水量,相对湿度,空气质量。
对获取的气候数据进行预处理,剖析河南的风力等级和风向,制造风向风级雷达图。
依据获取的温度和湿度制造温湿度相关性剖析图,进行温度、湿度对比剖析。
依据获取的各城市的降雨量,可视化近24小时的每小时时段降水状况。
制造各城市24小时的累计降雨量。




依据对数据剖析,回来的json格式数据,不难发现:

101180101便是代表城市编号
7天的气候预告数据信息在div标签中并且id=“7d”
日期、气候、温度、风级等信息都在ul和li标签
网页结构咱们上面现已剖析好了,那么咱们就能够来着手爬取所需求的数据了。获取到一切的数据资源之后,能够把这些数据保存下来。

请求网站:

气候网的网址:http://www.weather.com.cn/weather/101180101.shtml。假如想爬取不同的区域只需修改毕竟的101180101区域编号,前面的weather代表是7天的网页。

def getHTMLtext(url):
"""请求获得网页内容"""
try:
r = requests.get(url, timeout = 30)
r.raise_for_status()
r.encoding = r.apparent_encoding
print("Success")
return r.text
except:
print("Fail")
return" "
1
2
3
4
5
6
7
8
9
10
11


处理数据:

选用BeautifulSoup库对刚刚获取的字符串进行数据提取。获取咱们需求的风力方向,风级,降水量,相对湿度,空气质量等。

def get_content(html,cityname):
"""处理得到有用信息保存数据文件"""
final = []  # 初始化一个列表保存数据
bs = BeautifulSoup(html, "html.parser")  # 创建BeautifulSoup方针
body = bs.body
data = body.find('div', {'id': '7d'})    # 找到div标签且id = 7d
# 下面爬取当天的数据
data2 = body.find_all('div',{'class':'left-div'})
text = data2[2].find('script').string
text = text[text.index('=')+1 :-2] # 移除改var data=将其变为json数据
jd = json.loads(text)
dayone = jd['od']['od2'] # 找到当天的数据
final_day = []      # 寄存当天的数据
count = 0
for i in dayone:
temp = []
if count <=23:
temp.append(i['od21']) # 增加时刻
temp.append(cityname+'市') # 增加城市
temp.append(i['od22']) # 增加当时时刻温度
temp.append(i['od24']) # 增加当时时刻风力方向
temp.append(i['od25']) # 增加当时时刻风级
temp.append(i['od26']) # 增加当时时刻降水量
temp.append(i['od27']) # 增加当时时刻相对湿度
temp.append(i['od28']) # 增加当时时刻操控质量
# print(temp)
final_day.append(temp)
data_all.append(temp)
count = count +1
# 下面爬取24h的数据
ul = data.find('ul')                     # 找到一切的ul标签
li = ul.find_all('li')                   # 找到左右的li标签
i = 0                                    # 操控爬取的天数
for day in li:                          # 遍历找到的每一个li
    if i < 7 and i > 0:
        temp = []                        # 暂时寄存每天的数据
        date = day.find('h1').string     # 得到日期
        date = date[0:date.index('日')]  # 取出日期号
        temp.append(date)
        inf = day.find_all('p')          # 找出li下面的p标签,提取第一个p标签的值,即气候
        temp.append(inf[0].string)

        tem_low = inf[1].find('i').string  # 找到最低气温

        if inf[1].find('span') is None:  # 气候预告或许没有最高气温
            tem_high = None
        else:
            tem_high = inf[1].find('span').string  # 找到最高气温
        temp.append(tem_low[:-1])
        if tem_high[-1] == '℃':
        temp.append(tem_high[:-1])
        else:
        temp.append(tem_high)

        wind = inf[2].find_all('span') # 找到风向
        for j in wind:
        temp.append(j['title'])

        wind_scale = inf[2].find('i').string # 找到风级
        index1 = wind_scale.index('级')
        temp.append(int(wind_scale[index1-1:index1]))
        final.append(temp)
    i = i + 1
return final_day,final

—— 灵通云微信公众号 ——

热门标签

上一条———————

下一条———————

十七年 建站经验

多一份参考,总有益处

联系灵通云,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-688-6062 / 大客户专线   南通:15818561755