python爬蟲爬取天氣數據並圖形化顯示

前言
使用python進行網頁數據的爬取現在已經很常見了,而對天氣數據的爬取更是入門級的新手操作,很多人學習爬蟲都從天氣開始,本文便是介紹了從中國天氣網爬取天氣數據,能夠實現輸入想要查詢的城市,返回該城市未來一週的天氣情況,保存爲csv文件,並對數據圖形化展示分析。最後附完整代碼。

1、使用模塊
Python3。主要使用到了csv、sys、urllib.request和BeautifulSoup4模塊,其中csv模塊是爲了對csv文件的處理,urllib.request可以構造http請求,BeautifulSoup4可以解析頁面信息。在使用這些模塊之前,如果不存在需要進行安裝,可打開cmd使用pip進行安裝。當然,還需要一個城市名與城市code對應的文件,便於我們輸入城市後找到對應的code進行相應的天氣信息提取。這裏點擊文件內容cityinfo,可以查看到整理好的城市代碼,將該頁面內容複製保存爲.py文件,然後放入同路徑導入即可。

2、根據輸入城市從城市代碼文件提取到相應的城市代碼

cityname = input("請輸入你想要查詢天氣的城市:")
	if cityname in cityinfo.city:
   		 citycode = cityinfo.city[cityname]
	else:
   		 sys.exit()

3、製作請求頭,得到請求的應答內容,即頁面信息

url = 'http://www.weather.com.cn/weather/' + citycode + '.shtml'
header = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36")  # 設置頭部信息
http_handler = urllib.request.HTTPHandler()
opener = urllib.request.build_opener(http_handler)  # 修改頭部信息
opener.addheaders = [header]
request = urllib.request.Request(url)  # 製作請求
response = opener.open(request)  # 得到應答包
html = response.read()  # 讀取應答包
html = html.decode('utf-8')  # 設置編碼,否則會亂碼

其中,設置頭部信息header是爲了防止某些網站設置了反爬蟲,在chrome瀏覽器下,頭部信息header可在瀏覽器中按f12然後點擊network,找到一個請求流,點擊請求流後可以看到相應的頭部信息。
在這裏插入圖片描述
4、根據返回的頁面進行數據的篩選

final = []  # 初始化一個列表保存數據
bs = BeautifulSoup(html, "html.parser")  # 創建BeautifulSoup對象
body = bs.body   # 獲取body部分數據
data = body.find('div', {'id': '7d'}) 
ul = data.find('ul')
li = ul.find_all('li')

#所有的標籤獲取內容都根據在頁面的所在位置進行篩選,如圖所示,我們要查找的未來七天的天氣情況都包含在id爲7d的div標籤中,七天的天氣又在這個div的ul中,該div僅有一個ul,因此可使用find方法,每一天的天氣又在該ul的li中,且有多個li,則必須使用find_all()方法,找到所有的li,不能使用find方法。
在這裏插入圖片描述

5、爬取數據

i = 0  # 控制爬取的天數
lows = []  # 保存低溫
highs = []  # 保存高溫
for day in li:  # 便利找到的每一個li
    if i < 7:
        temp = []
        date = day.find('h1').string  # 得到日期
        temp.append(date)

        inf = day.find_all('p')  # 獲取天氣,遍歷li下面的p標籤 有多個p需要使用find_all 而不是find
        temp.append(inf[0].string)

        temlow = inf[1].find('i').string  # 最低氣溫
        if inf[1].find('span') is None:  # 天氣預報有時候可能沒有最高氣溫,需要做一個判斷
            temhigh = None
            temperate = temlow
        else:
            temhigh = inf[1].find('span').string  # 最高氣溫
            temhigh = temhigh.replace('℃', '')
            temperate = temhigh + '/' + temlow
            temp.append(temperate)
        final.append(temp)
        i = i + 1

這裏是從每個li中獲取到每天的天氣情況,控制在7天,通過li標籤下面的各數據位置進行相應的提取,要注意提取標籤的數量,如果在當前標籤下有多個相同的提取標籤,要使用find_all()而不是find,然後用[n]進行相應的數據提取
在這裏插入圖片描述
在提取溫度時要注意一個問題,中國天氣網一般都會顯示最高氣溫和最低氣溫,但有時候只會顯示一個溫度,無最高氣溫,這時就要做一個判斷,否則腳本會出錯。然後將天氣拼接成一個字符串,和其他數據一起放入final 列表中
6、寫入csv文件

with open('weather.csv', 'a', errors='ignore', newline='') as f:
    f_csv = csv.writer(f)
    f_csv.writerows([cityname])
    f_csv.writerows(final)

最後看到的csv文件中存儲的天氣數據如下圖所示:
在這裏插入圖片描述
7、使用pygal繪圖,使用該模塊前需先安裝pip install pygal,然後導入import pygal

bar = pygal.Line()  # 創建折線圖
bar.add('最低氣溫', lows)   #添加兩線的數據序列
bar.add('最高氣溫', highs)  #注意lows和highs是int型的列表

bar.x_labels = daytimes
bar.x_labels_major = daytimes[::30]
bar.x_label_rotation = 45

bar.title = cityname+'未來七天氣溫走向圖'   #設置圖形標題
bar.x_title = '日期'   #x軸標題
bar.y_title = '氣溫(攝氏度)'  #  y軸標題

bar.legend_at_bottom = True

bar.show_x_guides = False
bar.show_y_guides = True

bar.render_to_file('temperate1.svg')  # 將圖像保存爲SVG文件,可通過瀏覽器查看

最終生成的圖形如下圖所示,直觀的顯示了天氣情況:
在這裏插入圖片描述
8、完整代碼

import csv
import sys
import urllib.request
from bs4 import BeautifulSoup  # 解析頁面模塊
import pygal
import cityinfo


cityname = input("請輸入你想要查詢天氣的城市:")
if cityname in cityinfo.city:
    citycode = cityinfo.city[cityname]
else:
    sys.exit()

url = 'http://www.weather.com.cn/weather/' + citycode + '.shtml'
header = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36")  # 設置頭部信息
http_handler = urllib.request.HTTPHandler()
opener = urllib.request.build_opener(http_handler)  # 修改頭部信息
opener.addheaders = [header]
request = urllib.request.Request(url)  # 製作請求
response = opener.open(request)  # 得到應答包
html = response.read()  # 讀取應答包
html = html.decode('utf-8')  # 設置編碼,否則會亂碼

# 根據得到的頁面信息進行初步篩選過濾
final = []  # 初始化一個列表保存數據
bs = BeautifulSoup(html, "html.parser")  # 創建BeautifulSoup對象
body = bs.body
data = body.find('div', {'id': '7d'})
print(type(data))
ul = data.find('ul')
li = ul.find_all('li')

# 爬取自己需要的數據
i = 0  # 控制爬取的天數
lows = []  # 保存低溫
highs = []  # 保存高溫
daytimes = []  # 保存日期
weathers = []  # 保存天氣
for day in li:  # 便利找到的每一個li
    if i < 7:
        temp = []  # 臨時存放每天的數據
        date = day.find('h1').string  # 得到日期
        #print(date)
        temp.append(date)
        daytimes.append(date)
        inf = day.find_all('p')  # 遍歷li下面的p標籤 有多個p需要使用find_all 而不是find

        #print(inf[0].string)  # 提取第一個p標籤的值,即天氣
        temp.append(inf[0].string)
        weathers.append(inf[0].string)
        temlow = inf[1].find('i').string  # 最低氣溫
        if inf[1].find('span') is None:  # 天氣預報可能沒有最高氣溫
            temhigh = None
            temperate = temlow
        else:
            temhigh = inf[1].find('span').string  # 最高氣溫
            temhigh = temhigh.replace('℃', '')
            temperate = temhigh + '/' + temlow
        # temp.append(temhigh)
        # temp.append(temlow)
        lowStr = ""
        lowStr = lowStr.join(temlow.string)
        lows.append(int(lowStr[:-1]))  # 以上三行將低溫NavigableString轉成int類型並存入低溫列表
        if temhigh is None:
            highs.append(int(lowStr[:-1]))
        else:
            highStr = ""
            highStr = highStr.join(temhigh)
            highs.append(int(highStr))  # 以上三行將高溫NavigableString轉成int類型並存入高溫列表
        temp.append(temperate)
        final.append(temp)
        i = i + 1

# 將最終的獲取的天氣寫入csv文件
with open('weather.csv', 'a', errors='ignore', newline='') as f:
    f_csv = csv.writer(f)
    f_csv.writerows([cityname])
    f_csv.writerows(final)

# 繪圖
bar = pygal.Line()  # 創建折線圖
bar.add('最低氣溫', lows)
bar.add('最高氣溫', highs)

bar.x_labels = daytimes
bar.x_labels_major = daytimes[::30]
# bar.show_minor_x_labels = False  # 不顯示X軸最小刻度
bar.x_label_rotation = 45

bar.title = cityname+'未來七天氣溫走向圖'
bar.x_title = '日期'
bar.y_title = '氣溫(攝氏度)'

bar.legend_at_bottom = True

bar.show_x_guides = False
bar.show_y_guides = True

bar.render_to_file('temperate.svg')

(注:部分資料來源於網絡,如有侵權,請聯繫作者)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章