1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| 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)
while True : 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 : if ord(c) > 32 : putwch(c) if c == '℃' : putwch(' ') putwch('\n')
wea = str(b.find_all(attrs = {'class': 'hisdailywea'})[0].text) for c in wea : if ord(c) > 32 : putwch(c) putwch('\n')
wind = str(b.find_all(attrs = {'class': 'hisdailywind'})[0].text) for c in wind : if ord(c) > 32 : putwch(c) putwch('\n') putwch('\n')
|