获取中国天气网最低温度数据

1.需要提前安装的库

  1. bs4 (BeautifulSoup) : 解析网页
  2. requests:网络请求
  3. pyecharts : 数据可视化

2.环境

Python3.6.3

4.全部代码

# /usr/bin/env python2.7
# -*- coding:utf-8 -*-

from bs4 import BeautifulSoup
import requests
import time
import json

from pyecharts import Bar

TEMPERATURE_LIST = []

def getWether(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
        'Referer': 'http://www.weather.com.cn/forecast/',
        'Host': 'www.weather.com.cn',
        'Upgrade-Insecure-Requests': '1'
    }

    req = requests.get(url, headers=headers)

    content = req.content.decode('utf-8')

    soup = BeautifulSoup(content, 'lxml')

    conMid = soup.find('div', class_='conMidtab')

    conMidtab2_list = conMid.find_all('div', class_='conMidtab2')

    for x in conMidtab2_list:

        tr_list = x.find_all('tr')[2:]  # 排除 前面两个 标题行tr
        province = ''
        for index, tr in enumerate(tr_list):

            if index == 0:
                td_list = tr.find_all('td')
                province = td_list[0].text.replace('\n', '')
                city = td_list[1].text.replace('\n', '')
                min = td_list[7].text.replace('\n', '')
            else:
                td_list = tr.find_all('td')
                city = td_list[0].text.replace('\n', '')
                min = td_list[6].text.replace('\n', '')

            # print(province + city + min)

            TEMPERATURE_LIST.append({
                'city':city,
                'min':min
            })



def writeFile():
    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']

    for url in urls:
        getWether(url)
        time.sleep(3)

    tem = json.dumps(TEMPERATURE_LIST,ensure_ascii=False)
    with open('temperature.json','w') as fr:
        fr.write(tem)


def openFile():
    print('制作柱状图')

    with open('temperature.json','r') as fr:
        TEMPERATURE_LIST = json.load(fr,encoding='utf-8')


    TEMPERATURE_LIST.sort(key=lambda x:(int(x['min']),int(x['min'])))

    TOP20_TEMPERATURE_LIST = TEMPERATURE_LIST[:20]
    CITY_LIST = []
    MINTEMPERATURE_LIST = []

    for dict1 in TOP20_TEMPERATURE_LIST:
        CITY_LIST.append(dict1['city'])
        MINTEMPERATURE_LIST.append(dict1['min'])


    bar = Bar(u'全国最低温度前20排行榜',u'XXX贡献')
    bar.add(u'最低温度排行',CITY_LIST,MINTEMPERATURE_LIST,is_more_utils=True)

    bar.show_config()
    bar.render()


def main():
    # writeFile()   #先获取数据,保存到本地
    openFile()     #获取完成后,直接取本地的数据


if __name__ == '__main__':
    main()

4.最后完成的效果截图

获取中国天气网最低温度数据_第1张图片
data.png

你可能感兴趣的:(获取中国天气网最低温度数据)