自动天气

该程序不需要手动输入城市,会自动定位

手动天气(需要手动输入城市名):https://www.cnblogs.com/Ctrl-cCtrl-v/p/12906077.html

 1 import urllib.request
 2 import gzip
 3 import json
 4 
 5 def dw():
 6     from urllib.request import urlopen
 7     import jieba
 8     my_ip = urlopen('http://ip.42.pl/raw').read()
 9 
10     import requests
11     import IPy
12      
13     def get_location(ip):
14         url =  'https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?co=&resource_id=6006&t=1529895387942&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110203920624944751099_1529894588086&_=1529894588088&query=%s'%ip
15         # headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}
16         r = requests.get(url)
17         r.encoding = r.apparent_encoding
18         html = r.text
19         c1 = html.split('location":"')[1]
20         c2 = c1.split('","')[0]
21         return c2
22      
23     def check_ip(ip):
24         try:
25             IPy.IP(ip)
26             return True
27         except Exception as e:
28             print(e)
29             return False
30      
31 
32     ip = str(my_ip).strip('b')
33     ip=eval(ip)
34     print(ip)
35     if check_ip(ip):
36         cs=get_location(ip)
37         cs=list(jieba.cut(cs))
38         print('城市位置为:',str(cs[1]))
39         return str(cs[1])
40 
41     print('------天气查询------')
42 def get_weather_data() :
43     city_name = dw()
44     url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
45     url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
46     #网址1只需要输入城市名,网址2需要输入城市代码
47     #print(url1)
48     weather_data = urllib.request.urlopen(url1).read()
49     #读取网页数据
50     weather_data = gzip.decompress(weather_data).decode('utf-8')
51     #解压网页数据
52     weather_dict = json.loads(weather_data)
53     #将json数据转换为dict数据
54     return weather_dict
55 
56 def show_weather(weather_data):
57     weather_dict = weather_data
58     #将json数据转换为dict数据
59     if weather_dict.get('desc') == 'invilad-citykey':
60         print('你输入的城市名有误,或者天气中心未收录你所在城市')
61     elif weather_dict.get('desc') =='OK':
62         forecast = weather_dict.get('data').get('forecast')
63         print('城市:',weather_dict.get('data').get('city'))
64         print('温度:',weather_dict.get('data').get('wendu')+'')
65         print('感冒:',weather_dict.get('data').get('ganmao'))
66         #print('风向:',forecast[0].get('fengxiang'))
67         print('风级:',forecast[0].get('fengli'))
68         print('高温:',forecast[0].get('high'))
69         print('低温:',forecast[0].get('low'))
70         print('天气:',forecast[0].get('type'))
71         print('日期:',forecast[0].get('date'))
72         print('*******************************')
73     
74         for i in range(1,5):
75             print('日期:',forecast[i].get('date'))
76             #print('风向:',forecast[i].get('fengxiang'))
77             print('风级:',forecast[i].get('fengli'))
78             print('高温:',forecast[i].get('high'))
79             print('低温:',forecast[i].get('low'))
80             print('天气:',forecast[i].get('type'))
81             print('--------------------------')
82     print('***********************************')
83 
84 show_weather(get_weather_data())
85 input()

猜你喜欢

转载自www.cnblogs.com/Ctrl-cCtrl-v/p/12906084.html
今日推荐