Python爬取中國天氣網天氣數據


一、主題式網絡爬蟲設計方案

1.主題式網絡爬蟲名稱

名稱:Python爬取中國天氣網天氣數據

2.主題式網絡爬蟲爬取的內容與數據特征分析

本次爬蟲主要爬取中國天氣網天氣數據

3.主題式網絡爬蟲設計方案概述(包括實現思路與技術難點)

requests庫實現發送請求、獲取響應。  
beautifulsoup實現數據解析、提取和清洗  
pyechart模塊實現數據可視化

二、主題頁面的結構特征分析

 

 http://www.weather.com.cn/textFC/hd.shtml  華東天氣數據

 

 conMidtab 頁面整體標簽

conMidtab2 地區總體標簽

 

 

三、網絡爬蟲程序設計

import requests
from bs4 import BeautifulSoup
from pyecharts import Bar

ALL_DATA = []
def send_parse_urls(start_urls):
    headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36"
    }
    for start_url in start_urls:
        response = requests.get(start_url,headers=headers)
        # 編碼問題的解決
        response = response.text.encode("raw_unicode_escape").decode("utf-8")
        soup = BeautifulSoup(response,"html5lib") #lxml解析器:性能比較好,html5lib:適合頁面結構比較混亂的
        div_tatall = soup.find("div",class_="conMidtab") #find() 找符合要求的第一個元素
        tables = div_tatall.find_all("table") #find_all() 找到符合要求的所有元素的列表
        for table in tables:
            trs = table.find_all("tr")
            info_trs = trs[2:]
            for index,info_tr in enumerate(info_trs): # 枚舉函數,可以獲得索引
                # print(index,info_tr)
                # print("="*30)
                city_td = info_tr.find_all("td")[0]
                temp_td = info_tr.find_all("td")[6]
                # if的判斷的index的特殊情況應該在一般情況的后面,把之前的數據覆蓋
                if index==0:
                    city_td = info_tr.find_all("td")[1]
                    temp_td = info_tr.find_all("td")[7]
                city=list(city_td.stripped_strings)[0]
                temp=list(temp_td.stripped_strings)[0]
                ALL_DATA.append({"city":city,"temp":temp})
    return ALL_DATA

def get_start_urls():
    start_urls = [
       "http://www.weather.com.cn/textFC/hb.shtml",
        "http://www.weather.com.cn/textFC/db.shtml",
        "http://www.weather.com.cn/textFC/hd.shtml",
        "http://www.weather.com.cn/textFC/hz.shtml",
        "http://www.weather.com.cn/textFC/hn.shtml",
        "http://www.weather.com.cn/textFC/xb.shtml",
        "http://www.weather.com.cn/textFC/xn.shtml",
        "http://www.weather.com.cn/textFC/gat.shtml",
    ]
    return start_urls

def main():
    """
    主程序邏輯
    展示全國實時溫度最低的十個城市氣溫排行榜的柱狀圖
    """
    # 1 獲取所有起始url
    start_urls = get_start_urls()
    # 2 發送請求獲取響應、解析頁面
    data = send_parse_urls(start_urls)
    # print(data)
    # 4 數據可視化
        #1排序
    data.sort(key=lambda data:int(data["temp"]))
        #2切片,選擇出溫度最低的十個城市和溫度值
    show_data = data[:10]
        #3分出城市和溫度
    city = list(map(lambda data:data["city"],show_data))
    temp = list(map(lambda data:int(data["temp"]),show_data))
        #4創建柱狀圖、生成目標圖
    chart = Bar("中國最低氣溫排行榜") #需要安裝pyechart模塊
    chart.add("",city,temp)
    chart.render("tempture.html")

if __name__ == '__main__':
    main()

 

 

    四、可視化

 

柱狀圖:

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM