python爬取中国天气网并展示最低温度

import requests
from bs4 import BeautifulSoup
import lxml
import json
from echarts import Echart,Bar,Axis
import time

#城市与温度的集合
weather_list = []
#城市集合
city_list = []
#温蒂集合
lowest_list =[]

#获取温度
def getTemperature(url):
    result = requests.get(url)
    #print result.content
    soup = BeautifulSoup(result.content,'lxml')
    div_conMidTab =  soup.find("div",attrs={'class':'conMidtab'})
    div_conMidTab2 = div_conMidTab.find_all("div",attrs={'class':'conMidtab2'})
    for x in div_conMidTab2:
        tr_list = x.find_all("tr")[2:]
        provider =''
        for index,tr in enumerate(tr_list):
            td_list = tr.find_all('td')
            if index ==0:
                provider = td_list[0].text.replace('\n','')
                city = td_list[1].text.replace('\n','')
                #最高温度
                hight_temperature = td_list[4].text
                #最低温度
                low_temperatrue = td_list[7].text
            else:
                city = td_list[0].text.replace('\n','')
                # 最高温度
                hight_temperature = td_list[3].text
                #最低温度
                low_temperatrue = td_list[6].text
            print provider+'  '+city+'  '+low_temperatrue+'~'+hight_temperature
            weather_list.append({
                'city':provider+city,
                'min':low_temperatrue
            })
            city_list.append(provider+city)
            lowest_list.append(low_temperatrue)
        print '-' * 40

def main():
    result = requests.get('http://www.weather.com.cn/textFC/hb.shtml')
    soup = BeautifulSoup(result.content,'lxml')
    #获取华东,华北...等地区对应的url港澳台的数据格式不一样,过滤掉
    li_list = soup.find("ul",attrs={'class':'lq_contentboxTab2'}).find_all('li')[0:-1]
    #遍历获取数据
    for li in li_list:
        url = 'http://www.weather.com.cn/'+li.find('a')['href']
        getTemperature(url)
        time.sleep(1)

    #写到本地文件
    # line = json.dumps(weather_list,ensure_ascii=False)
    # with open ('weather.json','w') as fp:
    #     fp.write(line.encode('utf-8'))

    #读取本地的json文件
    # with open('weather.json', 'r') as fp:
    #     weather_list = json.load(fp,encoding='utf-8')

    #将数据进行排序
    sort_weather_list = sorted(weather_list,lambda x,y:cmp(int(x['min']),int(y['min'])))
    #获取前20名
    TOP10_SORT_WEATHER_LIST = sort_weather_list[0:20]
    TOP10_CITY = []
    TOP10_MIN = []
    #遍历将城市和低温放到新的集合中
    for city_min in TOP10_SORT_WEATHER_LIST:
        TOP10_CITY.append(city_min['city'])
        TOP10_MIN.append(city_min['min'])

    #创建表格
    echart = Echart(u'低温排名','温度')
    #柱状图
    bar = Bar(u'最低温度',TOP10_MIN)
    #坐标
    axis = Axis('category','bottom',data = TOP10_CITY)
    echart.use(bar)
    echart.use(axis)
    echart.plot()


if __name__ == '__main__':
    main()

你可能感兴趣的:(python爬取中国天气网并展示最低温度)