溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python爬蟲如何爬取天氣并數(shù)據(jù)圖形化

發(fā)布時間:2020-09-21 13:58:48 來源:億速云 閱讀:227 作者:Leah 欄目:編程語言

這篇文章將為大家詳細講解有關(guān)Python爬蟲如何爬取天氣并數(shù)據(jù)圖形化,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

前言

使用python進行網(wǎng)頁數(shù)據(jù)的爬取現(xiàn)在已經(jīng)很常見了,而對天氣數(shù)據(jù)的爬取更是入門級的新手操作,很多人學(xué)習(xí)爬蟲都從天氣開始,本文便是介紹了從中國天氣網(wǎng)爬取天氣數(shù)據(jù),能夠?qū)崿F(xiàn)輸入想要查詢的城市,返回該城市未來一周的天氣情況,保存為csv文件,并對數(shù)據(jù)圖形化展示分析。最后附完整代碼。

1、使用模塊

Python3。主要使用到了csv、sys、urllib.request和BeautifulSoup4模塊,其中csv模塊是為了對csv文件的處理,urllib.request可以構(gòu)造http請求,BeautifulSoup4可以解析頁面信息。在使用這些模塊之前,如果不存在需要進行安裝,可打開cmd使用pip進行安裝。當(dāng)然,還需要一個城市名與城市code對應(yīng)的文件,便于我們輸入城市后找到對應(yīng)的code進行相應(yīng)的天氣信息提取。這里點擊文件內(nèi)容cityinfo,可以查看到整理好的城市代碼,將該頁面內(nèi)容復(fù)制保存為.py文件,然后放入同路徑導(dǎo)入即可。

2、根據(jù)輸入城市從城市代碼文件提取到相應(yīng)的城市代碼

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

3、制作請求頭,得到請求的應(yīng)答內(nèi)容,即頁面信息

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")  # 設(shè)置頭部信息
http_handler = urllib.request.HTTPHandler()
opener = urllib.request.build_opener(http_handler)  # 修改頭部信息
opener.addheaders = [header]
request = urllib.request.Request(url)  # 制作請求
response = opener.open(request)  # 得到應(yīng)答包
html = response.read()  # 讀取應(yīng)答包
html = html.decode('utf-8')  # 設(shè)置編碼,否則會亂碼

其中,設(shè)置頭部信息header是為了防止某些網(wǎng)站設(shè)置了反爬蟲,在chrome瀏覽器下,頭部信息header可在瀏覽器中按f12然后點擊network,找到一個請求流,點擊請求流后可以看到相應(yīng)的頭部信息。

Python爬蟲如何爬取天氣并數(shù)據(jù)圖形化

4、根據(jù)返回的頁面進行數(shù)據(jù)的篩選

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

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

Python爬蟲如何爬取天氣并數(shù)據(jù)圖形化

5、爬取數(shù)據(jù)

i = 0  # 控制爬取的天數(shù)
lows = []  # 保存低溫
highs = []  # 保存高溫
for day in li:  # 便利找到的每一個li
    if i < 7:
        temp = []
        date = day.find('h2').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:  # 天氣預(yù)報有時候可能沒有最高氣溫,需要做一個判斷
            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標簽下面的各數(shù)據(jù)位置進行相應(yīng)的提取,要注意提取標簽的數(shù)量,如果在當(dāng)前標簽下有多個相同的提取標簽,要使用find_all()而不是find,然后用[n]進行相應(yīng)的數(shù)據(jù)提取。

Python爬蟲如何爬取天氣并數(shù)據(jù)圖形化

在提取溫度時要注意一個問題,中國天氣網(wǎng)一般都會顯示最高氣溫和最低氣溫,但有時候只會顯示一個溫度,無最高氣溫,這時就要做一個判斷,否則腳本會出錯。然后將天氣拼接成一個字符串,和其他數(shù)據(jù)一起放入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文件中存儲的天氣數(shù)據(jù),如下圖所示:

Python爬蟲如何爬取天氣并數(shù)據(jù)圖形化

7、使用pygal繪圖

使用該模塊前需先安裝pip install pygal,然后導(dǎo)入import pygal。

bar = pygal.Line()  # 創(chuàng)建折線圖
bar.add('最低氣溫', lows)   #添加兩線的數(shù)據(jù)序列
bar.add('最高氣溫', highs)  #注意lows和highs是int型的列表
bar.x_labels = daytimes
bar.x_labels_major = daytimes[::30]
bar.x_label_rotation = 45
bar.title = cityname+'未來七天氣溫走向圖'   #設(shè)置圖形標題
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文件,可通過瀏覽器查看

最終生成的圖形如下圖所示,直觀的顯示了天氣情況:

Python爬蟲如何爬取天氣并數(shù)據(jù)圖形化

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")  # 設(shè)置頭部信息
http_handler = urllib.request.HTTPHandler()
opener = urllib.request.build_opener(http_handler)  # 修改頭部信息
opener.addheaders = [header]
request = urllib.request.Request(url)  # 制作請求
response = opener.open(request)  # 得到應(yīng)答包
html = response.read()  # 讀取應(yīng)答包
html = html.decode('utf-8')  # 設(shè)置編碼,否則會亂碼
# 根據(jù)得到的頁面信息進行初步篩選過濾
final = []  # 初始化一個列表保存數(shù)據(jù)
bs = BeautifulSoup(html, "html.parser")  # 創(chuàng)建BeautifulSoup對象
body = bs.body
data = body.find('div', {'id': '7d'})
print(type(data))
ul = data.find('ul')
li = ul.find_all('li')
# 爬取自己需要的數(shù)據(jù)
i = 0  # 控制爬取的天數(shù)
lows = []  # 保存低溫
highs = []  # 保存高溫
daytimes = []  # 保存日期
weathers = []  # 保存天氣
for day in li:  # 便利找到的每一個li
    if i < 7:
        temp = []  # 臨時存放每天的數(shù)據(jù)
        date = day.find('h2').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:  # 天氣預(yù)報可能沒有最高氣溫
            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轉(zhuǎn)成int類型并存入低溫列表
        if temhigh is None:
            highs.append(int(lowStr[:-1]))
        else:
            highStr = ""
            highStr = highStr.join(temhigh)
            highs.append(int(highStr))  # 以上三行將高溫NavigableString轉(zhuǎn)成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()  # 創(chuàng)建折線圖
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')

關(guān)于Python爬蟲如何爬取天氣并數(shù)據(jù)圖形化就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI