python可视化爬虫界面之天气查询


Posted in Python onJuly 03, 2019

执行效果如下:

python可视化爬虫界面之天气查询

python可视化爬虫界面之天气查询

from tkinter import *
import urllib.request
import gzip
import json
from tkinter import messagebox
root = Tk()
def main():
  # 输入窗口
  root.title('Python学习交流群:973783996') # 窗口标题
  Label(root, text='请输入城市').grid(row=0, column=0) # 设置标签并调整位置
  enter = Entry(root) # 输入框
  enter.grid(row=0, column=1, padx=20, pady=20) # 调整位置
  enter.delete(0, END) # 清空输入框
  enter.insert(0, 'Python学习交流群:973783996') # 设置默认文本
  # enter_text = enter.get()#获取输入框的内容
   running = 1
   def get_weather_data(): # 获取网站数据
    city_name = enter.get() # 获取输入框的内容
    url1 = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
    url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
    # 网址1只需要输入城市名,网址2需要输入城市代码
    # print(url1)
    weather_data = urllib.request.urlopen(url1).read()
    # 读取网页数据
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    # 解压网页数据
    weather_dict = json.loads(weather_data)
    # 将json数据转换为dict数据
    if weather_dict.get('desc') == 'invilad-citykey':
      print(messagebox.askokcancel("xing", "你输入的城市名有误,或者天气中心未收录你所在城市"))
    else:
      # print(messagebox.askokcancel('xing','bingguo'))
      show_data(weather_dict, city_name)  def show_data(weather_dict, city_name): # 显示数据
    forecast = weather_dict.get('data').get('forecast') # 获取数据块
    root1 = Tk() # 副窗口
    root1.geometry('650x280') # 修改窗口大小
    root1.title(city_name + '天气状况') # 副窗口标题
     # 设置日期列表
    for i in range(5): # 将每一天的数据放入列表中
      LANGS = [(forecast[i].get('date'), '日期'),
           (forecast[i].get('fengxiang'), '风向'),
           (str(forecast[i].get('fengji')), '风级'),
           (forecast[i].get('high'), '最高温'),
           (forecast[i].get('low'), '最低温'),
           (forecast[i].get('type'), '天气')]
      group = LabelFrame(root1, text='天气状况', padx=0, pady=0) # 框架
      group.pack(padx=11, pady=0, side=LEFT) # 放置框架
      for lang, value in LANGS: # 将数据放入框架中
        c = Label(group, text=value + ': ' + lang)
        c.pack(anchor=W)
    Label(root1, text='今日' + weather_dict.get('data').get('ganmao'),
       fg='green').place(x=40, y=20, height=40) # 温馨提示
    Label(root1, text="StarMan: 49star.com", fg="green", bg="yellow").place(x=10, y=255, width=125,                              height=20) # 作者网站
    Button(root1, text='确认并退出', width=10, command=root1.quit).place(x=500, y=230, width=80, height=40) # 退出按钮
    root1.mainloop() 
  # 布置按键
  Button(root, text="确认", width=10, command=get_weather_data) \
    .grid(row=3, column=0, sticky=W, padx=10, pady=5)
  Button(root, text='退出', width=10, command=root.quit) \
    .grid(row=3, column=1, sticky=E, padx=10, pady=5)
  if running == 1:
    root.mainloop() 
 if __name__ == '__main__':
  main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
盘点提高 Python 代码效率的方法
Jul 03 Python
利用Tkinter(python3.6)实现一个简单计算器
Dec 21 Python
Python把对应格式的csv文件转换成字典类型存储脚本的方法
Feb 12 Python
Python函数定义及传参方式详解(4种)
Mar 18 Python
Python的高阶函数用法实例分析
Apr 11 Python
Python3如何在Windows和Linux上打包
Feb 25 Python
python deque模块简单使用代码实例
Mar 12 Python
浅谈keras保存模型中的save()和save_weights()区别
May 21 Python
Python是怎样处理json模块的
Jul 16 Python
python获取百度热榜链接的实例方法
Aug 25 Python
python opencv肤色检测的实现示例
Dec 21 Python
解决Pytorch中关于model.eval的问题
May 22 Python
python读写配置文件操作示例
Jul 03 #Python
Python正则表达式匹配数字和小数的方法
Jul 03 #Python
在python中利用numpy求解多项式以及多项式拟合的方法
Jul 03 #Python
python读取图片的方式,以及将图片以三维数组的形式输出方法
Jul 03 #Python
Python read函数按字节(字符)读取文件的实现
Jul 03 #Python
在Python中COM口的调用方法
Jul 03 #Python
python字符串中匹配数字的正则表达式
Jul 03 #Python
You might like
php日期转时间戳,指定日期转换成时间戳
2012/07/17 PHP
基于PHP中的常用函数回顾
2013/07/11 PHP
PHP基于imap获取邮件实例
2014/11/11 PHP
PHP使用mysqldump命令导出数据库
2015/04/14 PHP
php抽象方法和抽象类实例分析
2016/12/07 PHP
PHP实现查询手机归属地的方法详解
2017/04/28 PHP
Mac系统下安装PHP Xdebug
2018/03/30 PHP
JavaScript使用技巧精萃[代码非常实用]
2008/11/21 Javascript
使用Grunt.js管理你项目的应用说明
2013/04/24 Javascript
jquery制作select列表双向选择示例代码
2014/09/02 Javascript
jquery.cookie.js用法实例详解
2015/12/25 Javascript
AngularJS入门教程之ng-class 指令用法
2016/08/01 Javascript
JavaScript prototype属性详解
2016/10/25 Javascript
JS实现用户注册时获取短信验证码和倒计时功能
2016/10/27 Javascript
express.js中间件说明详解
2019/03/19 Javascript
浅谈Vue.js组件(二)
2019/04/09 Javascript
element中的$confirm的使用
2020/04/26 Javascript
python实现去除下载电影和电视剧文件名中的多余字符的方法
2014/09/23 Python
python抓取最新博客内容并生成Rss
2015/05/17 Python
Python冒泡排序注意要点实例详解
2016/09/09 Python
基于随机梯度下降的矩阵分解推荐算法(python)
2018/08/31 Python
对python文件读写的缓冲行为详解
2019/02/13 Python
Django web框架使用url path name详解
2019/04/29 Python
python实现梯度法 python最速下降法
2020/03/24 Python
django ORM之values和annotate使用详解
2020/05/19 Python
python中的unittest框架实例详解
2021/02/05 Python
html5页面结构_动力节点Java学院整理
2017/07/10 HTML / CSS
项目副经理岗位职责
2013/12/30 职场文书
实习指导老师评语
2014/04/26 职场文书
增员口号大全
2014/06/18 职场文书
获奖感言怎么写
2015/07/31 职场文书
幼儿园托班开学寄语(2016秋季)
2015/12/03 职场文书
python 实现德洛内三角剖分的操作
2021/04/22 Python
浅谈Java实现分布式事务的三种方案
2021/06/11 Java/Android
详解Oracle块修改跟踪功能
2021/11/07 Oracle
利用Python实时获取steam特惠游戏数据
2022/06/25 Python