日韩综合精品69页|成人综合国产成人亚洲|中文字幕与邻居少妇乱码|2021亚洲国产精品无码|国产一区二区三区久久精品|人妻无码精品一区二区毛片|无码专区国产精品视频可下载|欧美国产日韩。欧美在线视视频

<strike id="u4tvh"></strike>
<input id="u4tvh"><th id="u4tvh"><del id="u4tvh"></del></th></input><rp id="u4tvh"></rp>

<rt id="u4tvh"></rt>
    1. <rt id="u4tvh"><dfn id="u4tvh"><center id="u4tvh"></center></dfn></rt>
      <style id="u4tvh"><del id="u4tvh"></del></style>

        資訊

        精準傳達 ? 有效溝通

        從品牌網(wǎng)站建設到網(wǎng)絡營銷策劃,從策略到執(zhí)行的一站式服務

        不知道天氣咋樣?一起用Python爬取天氣數(shù)據(jù)分析告訴你

        來源:公司資訊 | 2021.08.17

        前語
        今日咱們共享一個小案例,獲取氣候數(shù)據(jù),進行可視化剖析,帶你直觀了解氣候狀況!

        一、中心功能設計
        整體來說,咱們需求先對我國氣候網(wǎng)中的氣候數(shù)據(jù)進行爬取,保存為csv文件,并將這些數(shù)據(jù)進行可視化剖析展示。

        拆解需求,大致能夠整理出咱們需求分為以下幾步結束:

        通過爬蟲獲取我國氣候網(wǎng)7.20-7.21的降雨數(shù)據(jù),包含城市,風力方向,風級,降水量,相對濕度,空氣質量。
        對獲取的氣候數(shù)據(jù)進行預處理,剖析河南的風力等級和風向,制造風向風級雷達圖。
        依據(jù)獲取的溫度和濕度制造溫濕度相關性剖析圖,進行溫度、濕度對比剖析。
        依據(jù)獲取的各城市的降雨量,可視化近24小時的每小時時段降水狀況。
        制造各城市24小時的累計降雨量。




        依據(jù)對數(shù)據(jù)剖析,回來的json格式數(shù)據(jù),不難發(fā)現(xiàn):

        101180101便是代表城市編號
        7天的氣候預告數(shù)據(jù)信息在div標簽中并且id=“7d”
        日期、氣候、溫度、風級等信息都在ul和li標簽
        網(wǎng)頁結構咱們上面現(xiàn)已剖析好了,那么咱們就能夠來著手爬取所需求的數(shù)據(jù)了。獲取到一切的數(shù)據(jù)資源之后,能夠把這些數(shù)據(jù)保存下來。

        請求網(wǎng)站:

        氣候網(wǎng)的網(wǎng)址:http://www.weather.com.cn/weather/101180101.shtml。假如想爬取不同的區(qū)域只需修改畢竟的101180101區(qū)域編號,前面的weather代表是7天的網(wǎng)頁。

        def getHTMLtext(url):
        """請求獲得網(wǎng)頁內(nèi)容"""
        try:
        r = requests.get(url, timeout = 30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        print("Success")
        return r.text
        except:
        print("Fail")
        return" "
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11


        處理數(shù)據(jù):

        選用BeautifulSoup庫對剛剛獲取的字符串進行數(shù)據(jù)提取。獲取咱們需求的風力方向,風級,降水量,相對濕度,空氣質量等。

        def get_content(html,cityname):
        """處理得到有用信息保存數(shù)據(jù)文件"""
        final = []  # 初始化一個列表保存數(shù)據(jù)
        bs = BeautifulSoup(html, "html.parser")  # 創(chuàng)建BeautifulSoup方針
        body = bs.body
        data = body.find('div', {'id': '7d'})    # 找到div標簽且id = 7d
        # 下面爬取當天的數(shù)據(jù)
        data2 = body.find_all('div',{'class':'left-div'})
        text = data2[2].find('script').string
        text = text[text.index('=')+1 :-2] # 移除改var data=將其變?yōu)閖son數(shù)據(jù)
        jd = json.loads(text)
        dayone = jd['od']['od2'] # 找到當天的數(shù)據(jù)
        final_day = []      # 寄存當天的數(shù)據(jù)
        count = 0
        for i in dayone:
        temp = []
        if count <=23:
        temp.append(i['od21']) # 增加時刻
        temp.append(cityname+'市') # 增加城市
        temp.append(i['od22']) # 增加當時時刻溫度
        temp.append(i['od24']) # 增加當時時刻風力方向
        temp.append(i['od25']) # 增加當時時刻風級
        temp.append(i['od26']) # 增加當時時刻降水量
        temp.append(i['od27']) # 增加當時時刻相對濕度
        temp.append(i['od28']) # 增加當時時刻操控質量
        # print(temp)
        final_day.append(temp)
        data_all.append(temp)
        count = count +1
        # 下面爬取24h的數(shù)據(jù)
        ul = data.find('ul')                     # 找到一切的ul標簽
        li = ul.find_all('li')                   # 找到左右的li標簽
        i = 0                                    # 操控爬取的天數(shù)
        for day in li:                          # 遍歷找到的每一個li
            if i < 7 and i > 0:
                temp = []                        # 暫時寄存每天的數(shù)據(jù)
                date = day.find('h1').string     # 得到日期
                date = date[0:date.index('日')]  # 取出日期號
                temp.append(date)
                inf = day.find_all('p')          # 找出li下面的p標簽,提取第一個p標簽的值,即氣候
                temp.append(inf[0].string)

                tem_low = inf[1].find('i').string  # 找到最低氣溫

                if inf[1].find('span') is None:  # 氣候預告或許沒有最高氣溫
                    tem_high = None
                else:
                    tem_high = inf[1].find('span').string  # 找到最高氣溫
                temp.append(tem_low[:-1])
                if tem_high[-1] == '℃':
                temp.append(tem_high[:-1])
                else:
                temp.append(tem_high)

                wind = inf[2].find_all('span') # 找到風向
                for j in wind:
                temp.append(j['title'])

                wind_scale = inf[2].find('i').string # 找到風級
                index1 = wind_scale.index('級')
                temp.append(int(wind_scale[index1-1:index1]))
                final.append(temp)
            i = i + 1
        return final_day,final

        —— 靈通云微信公眾號 ——

        熱門標簽

        上一條———————

        下一條———————

        十七年 建站經(jīng)驗

        多一份參考,總有益處

        聯(lián)系靈通云,免費獲得專屬《策劃方案》及報價

        咨詢相關問題或預約面談,可以通過以下方式與我們聯(lián)系

        業(yè)務熱線:400-688-6062 / 大客戶專線   南通:15818561755

        泗阳县| 乌兰浩特市| 盐源县| 旬阳县| 利津县| 海阳市| 沧州市| 拉孜县| 金门县| 康乐县| 光山县| 天镇县| 兴安县| 达日县| 阆中市| 鲜城| 泰和县| 玉山县| 辽宁省| 开化县| 新郑市| 余姚市| 元朗区| 永善县| 嘉定区| 舞钢市| 西青区| 崇文区| 水富县| 扶余县| 姚安县| 化隆| 都兰县| 边坝县| 安福县| 利辛县| 盐源县| 灵宝市| 武冈市| 太保市| 历史|