python爬虫天气数据_python爬虫:天气数据的分析
就在前幾天還是二十多度的舒適溫度,今天一下子就變成了個位數,小編已經感受到冬天寒風的無情了。之前對獲取天氣都是數據上的搜集,做成了一個數據表后,對溫度變化的感知并不直觀。那么,我們能不能用python中的方法做一個天氣數據分析的圖形,幫助我們更直接的看出天氣變化呢?
使用pygal繪圖,使用該模塊前需先安裝pip install pygal,然后導入import pygalbar?=?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文件,可通過瀏覽器查看
最終生成的圖形如下圖所示,直觀的顯示了天氣情況:
完整代碼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?=?'非常抱歉,網頁無法訪問'? ?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?
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')
完整代碼看不懂的小伙伴可以翻一下之前的python爬蟲天氣文章,了解獲取天氣數據的方法,這里小編就不再重復了。更多Python學習推薦:JQ教程網Python大全。
總結
以上是生活随笔為你收集整理的python爬虫天气数据_python爬虫:天气数据的分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .xyz是什么域名(什么域名后缀适合做个
- 下一篇: python之禅怎么关闭_《Python