爬蟲之全國天氣最低的十個城市

__author__ = '田明博'
__date__ = '2019/10/9 21:23'
'''
獲取所有城市的天氣預報,按最低溫度排名
'''
import requests
import operator
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt


def get_page(link):
    '''
    獲取每一個頁面的源代碼,並分析
    :param link:每一個頁面的鏈接
    :return:
    '''
    all_temp = []
    url = link
    resp = requests.get(url)
    resp.encoding = 'utf-8'
    soup = BeautifulSoup(resp.text, 'html5lib')  # html5lib解析器,解析速度沒有lxml快
    conMidtab = soup.find('div', attrs={'class': 'conMidtab'})  #第一個,每週七天,提取當天的即可
    tables = conMidtab.find_all('table')
    for table in tables:
        trs = table.find_all('tr')[2:]  # 獲取數據
        for index, tr in enumerate(trs):
            one_info = {}  # 一條記錄
            tds = tr.find_all('td')  # 找到所有的信息td
            city_td = tds[0]  # 找到city所在的表格
            if index == 0:
                city_td = tds[1]
            city = list(city_td.stripped_strings)[0]  # city名字
            temp = tds[-2]  # 溫度
            temp = int(list(temp.stripped_strings)[0])

            one_info['city'] = city
            one_info['temp'] = temp
            all_temp.append(one_info)  # append所有記錄
    return all_temp


def show_charset(min_temp_citys):
    '''
    展示圖表
    :param min_temp_citys:
    :return:
    '''
    # print(min_temp_citys)
    x = []
    y = []
    # 解析獲取的前十數據(字典格式)
    for i in min_temp_citys:
        x.append(i['city'])
        y.append(int(i['temp']))
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用來正常顯示中文標籤
    plt.rcParams['axes.unicode_minus'] = False  # 用來正常顯示負號
    plt.title('全國溫度最低的前十城市')
    plt.xlabel('城市')  # 橫座標
    plt.ylabel('溫度℃')  # 縱座標
    plt.bar(x, y)  # 繪製柱狀圖
    plt.show()


def main():
    all_infos = []
    # 各地區鏈接
    links = ['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', ]
    for link in links:
        all = get_page(link)
        all_infos = all_infos + all  # 用於拼接列表
    # print(all_infos)
    min_temp_ten = sorted(all_infos, key=operator.itemgetter('temp'))[:10]
    print(min_temp_ten)
    show_charset(min_temp_ten)


if __name__ == '__main__':
    main()

運行截圖:

採用matplotlib庫繪製。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章