爬取背景
這套課程雖然叫爬蟲入門類課程,但是裡面涉及到的點是非常多,十分檢驗你的基礎掌握的牢固程度,程式碼中的很多地方都是可以細細品味的。
為什麼要寫這麼一個小東東呢,因為我生活在大河北,那霧霾醇厚的很,去了趟三亞,那空氣,嘖嘖,舒服的很,所以爬取一下三亞天氣,看看什麼時候去最好,理想的溫度為24~28,呵呵噠
程式碼走起來 ,天氣類的網址多的很,重點關注歷史天氣 找到這麼一個網站 www.tianqi.com/sanya/

程式碼走起來,爬蟲套路上吧
簡單的requests,複雜的scrapy 總有一款適合你的
哇哦~從2011年到2019年都有唉


URL 是這樣子的 lishi.tianqi.com/sanya/20111… lishi.tianqi.com/sanya/20110… lishi.tianqi.com/sanya/20110… lishi.tianqi.com/sanya/20110…
OK,這就簡單了,走起,看起來就簡單,那我就使用pyspider了 ,好久沒有,都有點遺忘了呢 怎麼執行呢?

一頓操作,資料就下載到了
沒有特別複雜的地方,基本就屬於常規操作了
def __init__(self):
self._city = "sanya"
def get_date_list(self,begin,end):
date_list = [x.strftime("%Y%m") for x in list(pd.date_range(start=begin,end=end,freq="M"))]
return date_list
@every(minutes=24 * 60)
def on_start(self):
# 我需要生成201101~201812的所有連結
date_list = self.get_date_list("2011-01-01","2019-01-01")
for item in date_list:
self.crawl('http://lishi.tianqi.com/%s/%s.html' %(self._city,item) , callback=self.index_page)
@config(age=10 * 24 * 60 * 60)
def index_page(self, response):
items = []
for each in response.doc('.tqtongji2 > ul:not(.t1)').items():
date = each("li:eq(0)").text()
hot = each("li:eq(1)").text()
cold = each("li:eq(2)").text()
weather = each("li:eq(3)").text()
wind_direction = each("li:eq(4)").text()
wind_power = each("li:eq(5)").text()
item = {
"date":date,
"hot":hot,
"cold":cold,
"weather":weather,
"wind_direction":wind_direction,
"wind_power":wind_power}
items.append(item)
return items
複製程式碼
基本資料分析
首先看一下資料的基本面

- 75% 都是26度的天氣,很舒服
- 都發現了異常資料,例如最低氣溫最小的竟然為0,最高氣溫為13,不合理
- 最熱竟然是37度,還不如我大河北溫度,奇怪了?
excel 讀取
def read_xlsx():
df = pd.read_excel("天氣資料.xlsx",names =["cold","date","hot","weather","wind","wind_s"])
return df
複製程式碼
我要知道,三亞天氣的一個走勢,這個地方直接按照時間進行圖示製作吧
按照時間排序
df = read_xlsx()
opt_df = df[['date','cold','hot']]
opt_df = opt_df.sort_values(by='date')
複製程式碼
可以明顯的看到波峰和波谷,這個地方只能知道三亞的天氣是有波動的,距離我得到最後的結果進了一點點

opt_df = opt_df.set_index("date")
for i in range(2011,2019):
data = opt_df[str(i)]
plt.plot(range(1,len(data)+1), data['cold'])
plt.show()
複製程式碼
每個顏色表示不同的年分,看來還是要用子圖了。

opt_df = opt_df.set_index("date")
for i in range(2011,2019):
data = opt_df[str(i)]
plt.subplot(8,1,i-2010)
plt.grid(True)
plt.plot(range(1,len(data)+1), data['cold'])
plt.plot(range(1, len(data) + 1), data['hot'])
plt.title(str(i)+u"年,溫度曲線")
plt.tight_layout()
plt.savefig("filename.png")
plt.show()
複製程式碼
一個小的注意事項是:解決使用 plt.savefig 儲存圖片時一片空白
原因
其實產生這個現象的原因很簡單:在 plt.show() 後呼叫了 plt.savefig() ,在 plt.show() 後實際上已經建立了一個新的空白的圖片(座標軸),這時候你再 plt.savefig() 就會儲存這個新生成的空白圖片。
知道了原因,就不難知道解決辦法了,解決辦法有兩種:
在 plt.show() 之前呼叫 plt.savefig();
import matplotlib.pyplot as plt
""" 一些畫圖程式碼 """
plt.savefig("filename.png")
plt.show()
複製程式碼
畫圖的時候獲取當前影象(這一點非常類似於 Matlab 的控制程式碼的概念):
# gcf: Get Current Figure
fig = plt.gcf()
plt.show()
fig1.savefig('tessstttyyy.png', dpi=100)
複製程式碼

d2018 = opt_df["2018"]
print(d2018)
for i in range(1,13):
data = opt_df["2018-"+str(i)]
plt.subplot(12, 1, i)
plt.grid(True)
plt.plot(range(1, len(data) + 1), data['cold'])
plt.plot(range(1, len(data) + 1), data['hot'])
plt.title(str(i) + u"月,溫度曲線")
plt.yticks([0,5,10,15,20,25,30,35,40])
plt.xticks(range(1,32))
plt.tight_layout()
plt.savefig("filename.png")
plt.show()
複製程式碼


月份 | 最低氣溫 | 最高氣溫 |
---|---|---|
1 | 20.1 | 26.1 |
2 | 17.6 | 26.6 |
3 | 19.6 | 29.3 |
4 | 22.0 | 30.7 |
6 | 25.2 | 32.5 |
7 | 24.5 | 31.4 |
8 | 24.5 | 31.8 |
9 | 25.2 | 31.9 |
10 | 23.2 | 31.5 |
11 | 21.7 | 30.2 |
12 | 20.4 | 28.1 |
哈哈哈,都是好天氣......我到底在做啥結果.....



天氣 | 天數 |
---|---|
中雨 | 15 |
多雲 | 176 |
大雨 | 5 |
小雨 | 5 |
晴 | 12 |
暴雨 | 1 |
陰 | 11 |
陣雨 | 38 |
雷陣雨 | 6 |
統計一下每個月的天氣變化

- 1月,2月,3月,11月,12月基本沒雨
- 其他月份有雨
好吧,停止了,好像沒得到啥資料,就是1月,2月,3月,11月,12月去三亞吧,這幾個月份中3月份和11月份機票最便宜,看來我要寫爬取機票的部落格了
