太阳雨 发表于 2014-5-17 10:33:52

使用easygui修改的天气预报代码

本帖最后由 太阳雨 于 2014-5-17 10:39 编辑

各位鱼油们,大家好,今天学习了easygui,就把之前的天气预报的代码优化了一下,和大家交流一下
有图有真相



#encoding: utf-8
import urllib.request
import json
import pickle
import os
import sys
import easygui as eg
sys.path.append(r'C:\Python34\Lib\site-packages')

#获取程序运行目录
def current_file_directory():
    import os, sys, inspect
    path = os.path.realpath(sys.path)      # interpreter starter's path
    if os.path.isfile(path):                  # starter is excutable file
      path = os.path.dirname(path)
      return os.path.abspath(path)            # return excutable file's directory
    else:                                       # starter is python script
      caller_file = inspect.stack()   # function caller's filename
      return os.path.abspath(os.path.dirname(caller_file))# return function caller's

def jsoncall(url):
    url_file = urllib.request.urlopen(url)
    weatherHTML = url_file.read().decode('utf-8')
    weatherJSON = json.JSONDecoder().decode(weatherHTML)
    return weatherJSON

pkl_city = open("{path}\\city.pkl".format(path=current_file_directory()),"rb")
city = pickle.load(pkl_city)
pkl_city.close()

#city_name = input('请输入城市名:')
while True:
    city_name = eg.enterbox(msg='请输入城市名', title='天气情况查询',default='天津',strip=True)
    #用户直接取消查询,系统退出
    if city_name == None:
      sys.exit(0)
    password = city.get(city_name)
    #输入城市不存在
    if password == None:
      choice = eg.boolbox(msg='您所输入的城市不存在,是否继续查询', title='城市信息不存在')
      if choice:
            continue
      else:
            sys.exit(0)
    else:
      break
try:
    url_file_name = 'http://www.weather.com.cn/data/sk/{password}.html'.format(password=password)
    weatherInfo = jsoncall(url_file_name)['weatherinfo']

    url_file_name = 'http://www.weather.com.cn/data/cityinfo/{password}.html'.format(password=password)
    weatherInfo2 = jsoncall(url_file_name)['weatherinfo']
except:
    eg.msgbox(msg='请您检查您的网络', title='无可用网络')
    sys.exit(0)

#http://m.weather.com.cn/img/ 图片地址
msg='城市:{city}\n播报时间:{time}\n天气:{weather}\n室外温度:{temp}℃\n温度:{lower}-{high}\n风力:{wind}{level}'.format(
    city=weatherInfo.get('city'),
    time=weatherInfo.get('time'),
    weather=weatherInfo2.get('weather'),
    temp=weatherInfo.get('temp'),
    lower=weatherInfo2.get('temp2'),
    high=weatherInfo2.get('temp1'),
    wind=weatherInfo.get('WD'),
    level=weatherInfo.get('WS'))

eg.msgbox(msg=msg,title='今日{city_name}天气情况'.format(city_name=city_name))

源代码下载:**** Hidden Message *****

宁录 发表于 2014-5-17 11:26:56

我正想试试呢,等我做完再来看看。{:7_169:}

shenqi508 发表于 2014-5-17 11:30:11

谢谢学习。。。。。。。。。。。。。。。。。。。。。

496298448 发表于 2014-5-17 12:21:29

{:7_169:}感谢楼主分享

AnjoyXu 发表于 2014-5-17 13:19:23

感谢楼主分享:lol:

小桃YT 发表于 2014-5-21 00:45:48

那么屌吗??值得学习

xiaoyaolml 发表于 2014-8-8 19:36:38

学习

墙角君 发表于 2014-8-8 20:20:23

看看。。。。

wdmcwfnh 发表于 2014-8-9 00:38:56

要不要这么屌?~~~~~~~~~~~~~~~

afraidren 发表于 2014-9-23 07:42:09

正在自己做easyui学习中

nirvana436 发表于 2014-9-23 23:51:41

感谢 学习下

happyliuzh 发表于 2014-9-27 10:13:48

非常好,好好学习学习

mengxiang 发表于 2014-10-15 22:08:35

学习 学习

Andy0214 发表于 2014-10-22 11:44:43

这个我的urllib.request模块不起作用啊

の祉崭之殇__ 发表于 2014-10-22 22:05:15

谢谢您楼主

cuno32 发表于 2014-10-22 22:41:58

真不错

chenfujun 发表于 2014-10-23 19:46:03

学习一下

cnhaoning 发表于 2014-10-26 12:51:21

{:7_136:}求源代码啦

taoge6631 发表于 2015-6-6 22:54:22

不错啊 支持一下··

隋唐 发表于 2016-1-15 09:28:31

感谢分享
页: [1] 2 3 4
查看完整版本: 使用easygui修改的天气预报代码