#Date:2019.6.20
#author : isenwen
#describe : 中国天气网 ,爬取天气信息


import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
}


def get_detail(url):
    response = requests.get(url, headers=headers)
    text = response.content.decode('utf-8')
    #需要港澳台的信息则把lxml改为html5lib
    soup = BeautifulSoup(text, 'lxml')
    conMidtab = soup.find("div", class_='conMidtab')
    tables = conMidtab.find_all("table")
    for table in tables:
        trs = table.find_all("tr")[2:]
        for index,tr in enumerate(trs):
            tds = tr.find_all("td")
            city_td = tds[0]
            if index==0:
                city_td=tds[1]
            city_name = list(city_td.stripped_strings)[0]#获取城市名字
            temp_low_td = tds[-2]
            temp_low = list(temp_low_td.stripped_strings)[0]#获取最低温度
            weather_td=tds[-4]
            weather = list(weather_td.stripped_strings)[0]#获取天气现象

            print({"city":city_name,"min_temp":temp_low,"weather":weather})

def main():
    url = 'http://www.weather.com.cn/textFC/hb.shtml'
    # url = 'http://www.weather.com.cn/textFC/db.shtml'
    # url = 'http://www.weather.com.cn/textFC/hd.shtml'
    # url = 'http://www.weather.com.cn/textFC/hz.shtml'
    # url = 'http://www.weather.com.cn/textFC/hn.shtml'
    # url = 'http://www.weather.com.cn/textFC/xb.shtml'
    # url = 'http://www.weather.com.cn/textFC/xn.shtml'
    # url = 'http://www.weather.com.cn/textFC/gat.shtml'
    get_detail(url)


if __name__ == '__main__':
    main()