1.中国天气网
http://www.weather.com.cn/weather/101010100.shtml

2.剖析页面

3.浏览器-F12-定位查看元素嵌套关系

4.导入须要的库

import requestsfrom bs4 import BeautifulSoupimport re

5.代码局部

result_list_wt = []def get_page(url):    try:        kv = {'user-agent':'Mozilla/5.0'}        r = requests.get(url,headers = kv)        r.raise_for_status()        r.encoding = r.apparent_encoding        return r.text    except:        return 'error'def parse_page(html, return_list):    soup = BeautifulSoup(html, 'html.parser')    day_list = soup.find('ul', 't clearfix').find_all('li')    for day in day_list:        date = day.find('h1').get_text()        wea = day.find('p',  'wea').get_text()        if day.find('p', 'tem').find('span'):                hightem = day.find('p', 'tem').find('span').get_text()        else:                hightem = ''        lowtem = day.find('p', 'tem').find('i').get_text()         win = re.findall('(?<= title=").*?(?=")', str(day.find('p','win').find('em')))        wind = '-'.join(win)        level = day.find('p', 'win').find('i').get_text()        return_list.append([date, wea, lowtem, hightem, wind, level])def print_res(return_list):    tplt = '{0:<10}\t{1:^10}\t{2:^10}\t{3:{6}^10}\t{4:{6}^10}\t{5:{6}^5}'     result_list_wt.append(tplt.format('日期', '天气', '最低温', '最低温', '风向', '风力',chr(12288))+"\n")     for i in return_list:        result_list_wt.append(tplt.format(i[0], i[1],i[2],i[3],i[4],i[5],chr(12288))+"\n")        def main():    # 城市-城市码txt    files = open('city_list.txt',"r")    city_name_id = files.readlines()    try:        # 获取txt-list        for line in city_name_id:            name_id = line.split('-')[1].replace("['","").replace("\n","")            url = 'http://www.weather.com.cn/weather/'+name_id+'.shtml'             city_name = line.split('-')[0].replace("['","").replace("\n","")            city_china = "\n"+"城市名 : "+city_name+"\n"            result_list_wt.append(city_china)            html = get_page(url)            wea_list = []            parse_page(html, wea_list)            print_res(wea_list)        files.close()    except:        print("error")    # 将获取后果写入到文件内        msgs = ''.join(result_list_wt)    print(msgs)    with open('weather.China.txt',"w+") as file:        file.write(msgs)        if __name__ == '__main__':    main()

6.city_list.txt

上海-101020100苏州-101190401无锡-101190201南京-101190101镇江-101190301宜兴-101190203扬州-101190601常州-101191101杭州-101210101宁波-101210401义乌-101210904温州-101210701台州-101210601湖州-101210201金华-101210901绍兴-101210507

7.用处

1.推送到企业微信2.推送到叮叮3.可定制@固定人员或推送到指定群组4.变成机器人揭示5.定时抓取判断以后城市的天气情况,利用到不同的业务场景

8.后果