1. China’s weather is http://www.weather.com.cn/weather/101010100.shtml

2. Analyze the page

3. Browser-F12 – Locate view element nesting relationship

4. Import the required libraries

import requests
from bs4 import BeautifulSoup
import re

5. Code

result_list_wt = [] def get_page(url): try: Get (url,headers = kV) r.raise_for_status() r.coding = 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} {10} 1: ^ \ \ t t {2: ^ 10} \ t {3: {6} ^ 10} \ t {4: {6} ^ 10} \ t {5: {6} ^ 5}' result_list_wt. Append (TPLT. The format (' date ', 'the weather, 'the most low temperature', 'one of the most high temperature', 'the wind', 'wind', CRH (12288)) + "\ n") for I return_list in: result_list_wt.append(tplt.format(i[0], i[1],i[2],i[3],i[4],i[5],chr(12288))+"\n") def main(): City_name_id = files.readlines() try: 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 (" result_list_wt ") print(" result_list_wt ") with open(' weather.china.txt ',"w+") as file: file.write(msgs) if __name__ == '__main__': main()

6.city_list.txt

Shanghai -101020100 Suzhou -101190401 Wuxi -101190201 Nanjing -101190101 Zhenjiang -101190301 Yixing -101190203 Yangzhou -101190601 Changzhou -101191101 Hangzhou -101210101 Ningbo -101210401 Yiwu -101210904 Wenzhou -101210701 Taizhou -101210601 Huzhou -101210201 Jinhua -101210901 Shaoxing -101210507

7. Use

1. Push it to enterprise WeChat; 2. Push it to Dingding; 3. 5. Timely capture and judge the current weather conditions of the city and apply them to different business scenarios

8. Write the contents of the local file