# -*- coding: utf-8 -*- from msvcrt import putwch import requests from bs4 import BeautifulSoup
h = {'User-Agent': 'Chrome/97.0.4692.99'}
city = input('请用小写字母输入待查询城市的英文名(比如“hongkong”或“beijing”):\n') try_url = 'https://lishi.tianqi.com/' + city + '/' try_r = requests.get(try_url, headers = h) while try_r.status_code != 200 : print('您输入的格式有误或者我们暂无这一城市的资料。请再试一次:\n') try_url = 'https://lishi.tianqi.com/' + city + '/' try_r = requests.get(try_url, headers = h)
whileTrue : date = input('请以“YYYYMMDD”的格式输入待查询的日期或者输入“exit”以退出:\n')
if date == 'exit' : break
url = 'https://lishi.tianqi.com/' + city + '/' + date + '/' r = requests.get(url, headers = h) if r.status_code != 200 : print('您输入的格式有误或者我们暂无这一天的资料。请再试一次:\n') continue
b = BeautifulSoup(r.text, 'lxml')
print('\n当天的天气为:') temp = str(b.find_all(attrs = {'class': 'hisdailytemp'})[0].text) for c in temp : iford(c) > 32 : putwch(c) if c == '℃' : putwch(' ') putwch('\n')
wea = str(b.find_all(attrs = {'class': 'hisdailywea'})[0].text) for c in wea : iford(c) > 32 : putwch(c) putwch('\n')
wind = str(b.find_all(attrs = {'class': 'hisdailywind'})[0].text) for c in wind : iford(c) > 32 : putwch(c) putwch('\n') putwch('\n')