1153天資料告訴你黃山雲海在哪些天容易遇見
1153天資料告訴你黃山雲海在哪些天容易遇見
資料來源:黃山風景區管理委員會官網 > 氣象資訊
原創:Ing_ideas
1。request請求
2。lxml結合xpath解析網頁原始碼
3正則提取文字
將資料去重、將日期轉為datetime物件等
2。讀入資料
雲海是黃山的第一奇觀。
據百度百科記載,黃山一年只有51天可以看到雲海,每年11月到第二年的5月是觀賞環山雲海的最佳時間段。抱著資料黨的態度,想用真實的資料證明以上結論是否可信。
流程:
1。資料獲取:黃山官網的氣象預告從2018年起至今提供了條天氣預報資料,包括我們所需的基本天氣情況以及雲海機率。
導航:黃山風景區管理委員會官網-氣象資訊
2。對獲得文字資料進行提取,這裡我們採用的Python中正則的方法來提取。
3。進行資料透視和繪圖分析。
一、資料獲取
1。request請求
根據網頁結構構造url請求,一共75頁。
url = ‘https://hsgwh。huangshan。gov。cn/content/column/6794224?pageIndex={}’。format(i)
2。lxml結合xpath解析網頁原始碼
#定義字典儲存資料
item = {}
#解析原始碼
content_lst = selector。xpath(“//div/div[3]/div[3]//ul//a//span/text()”)
#解析日期
date_lst = selector。xpath(‘//li//span[@class=“right date”]/text()’)
3正則提取文字
首先要解決
正則如何匹配中文字
的問題:
1。”[\u4e00-\u9fa5]+”可以表示一個或者多箇中文字元;
2。利用編碼函式將特定字元轉換為unicode編碼。
def get_unicode(str):
str_unicode = str。encode(‘unicode-escape’)。decode()
return print(str_unicode)
M_unicode = get_unicode(‘月’)
D_unicode = get_unicode(‘日’)
#以下是輸出,填入匹配規則就行
#\u6708
#\u65e5
# 迴圈解析
for num,n in enumerate(content_lst):
pattern_num = re。compile(r‘-?\d{1,2}’) # 匹配1-2位數字 -?此符號最多可以有一次
pattern_per = re。compile(r‘-?\d{1,2}%’) # 匹配1-3位機率
pattern_tex = re。compile(r‘[\u4e00-\u9fa5]’) # 匹配所有漢字
pattern_TEM = re。compile(r‘\u6c14\u6e29-?\d{1,2}[\u4e00-\u9fa5]-?\d{1,2}’)
pattern_SR = re。compile(r‘\d{1}\u65f6\d{1,3}\u5206’) # 日出時間文字
# 最低和最高溫度
tems = pattern_TEM。findall(n)
for tem in tems:
tem_min = pattern_num。findall(tem)[0]
tem_max = pattern_num。findall(tem)[1]
item[‘tem_min’] = tem_min
item[‘tem_max’] = tem_max
#雲海機率2018有和2022用的不同表述,需要多個條件和手動判斷
if len(percent) == 2:
sunrise_time_per = percent[0]
item[‘sunrise_time_per’] = sunrise_time_per
# 雲海可見機率
sea_of_clouds_per = percent[1]
item[‘sea_of_clouds_per’] = sea_of_clouds_per
elif len(percent) == 1:
sunrise_time_per = percent[0]
item[‘sunrise_time_per’] = sunrise_time_per
if len(per_kejianyunhai) == 1 :
item[‘sea_of_clouds_per’] = ‘80%’
elif len(per_oujianyunhai) == 1:
item[‘sea_of_clouds_per’] = ‘30%’
else:
print(n+url+‘網頁需核實1’)
else:
print(n+url+‘網頁需核實0’)
item[‘sunrise_time_per’] = item[‘sea_of_clouds_per’] = ‘0’
# 儲存 pd。DataFrame([item])。to_csv(‘。/weather。csv’,mode=‘a’,index=False,header=0)
二、資料清洗
將資料去重、將日期轉為datetime物件等
df。drop_duplicates(inplace=True,ignore_index=True)
read_csv(parse_dates=[‘datetime’]) # parse_dates將某些列解析為 DataTime 物件,這比使用 to_datetime() 方便
2。讀入資料
程式碼如下(示例):
data = pd。read_csv(
‘https://labfile。oss。aliyuncs。com/courses/1283/adult。data。csv’)
print(data。head())
該處使用的url網路請求的資料。
三、資料分析
究竟哪些天容易見到雲海:
每年最可能見到黃山雲海的76天
三年平均下來,共有76天雲海可見機率大於等於60%,佔全年20%,所以碰上黃山雲海還是需要靠運氣的。我們可以看到五一和十一黃金週雲海機率不到60%,同時人流又多還會堵在上下山途中動不了,建議錯峰出遊。
他們集中在每年11-12月、1-2月、6月,其中冬季能見到霧凇與雲海兩大奇觀,黃山的淡季門票還打折,時間是
2021年12月20日至2022年1月20日
,查好天氣錯峰看雪景也是很不錯的。
image-20221125182759721
最後
感謝閱讀,如果覺得文章有用,希望點贊和關注。
本文由 mdnice 多平臺釋出