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之for循环语句
Oct 02 Python
整理Python中的赋值运算符
May 13 Python
解析Python中的二进制位运算符
May 13 Python
简单解决Python文件中文编码问题
Nov 22 Python
Python算术运算符实例详解
May 31 Python
python OpenCV学习笔记之绘制直方图的方法
Feb 08 Python
python执行CMD指令,并获取返回的方法
Dec 19 Python
浅谈Python中的可迭代对象、迭代器、For循环工作机制、生成器
Mar 11 Python
python学习--使用QQ邮箱发送邮件代码实例
Apr 16 Python
Django框架自定义模型管理器与元选项用法分析
Jul 22 Python
Python利用PyExecJS库执行JS函数的案例分析
Dec 18 Python
python openCV实现摄像头获取人脸图片
Aug 20 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
收音机鉴频器对声音的影响和频偏分析
2021/03/02 无线电
探讨GDFONTPATH能否被winxp下的php支持
2013/06/21 PHP
CodeIgniter CLI模式简介
2014/06/17 PHP
php实现的click captcha点击验证码类实例
2014/09/23 PHP
JavaScript XML和string相互转化实现代码
2011/07/04 Javascript
Javascript 面向对象编程(一) 封装
2011/08/28 Javascript
js网页中的(运行代码)功能实现思路
2013/02/04 Javascript
下拉框select的绑定示例
2014/09/04 Javascript
实例分析javascript中的call()和apply()方法
2014/11/28 Javascript
JavaScript中的replace()方法使用详解
2015/06/06 Javascript
javascript实现框架高度随内容改变的方法
2015/07/23 Javascript
gulp-uglify 与gulp.watch()配合使用时报错(重复压缩问题)
2016/08/24 Javascript
详解JavaScript权威指南之对象
2016/09/27 Javascript
EasyUI 结合JS导出Excel文件的实现方法
2016/11/10 Javascript
在Vue中使用echarts的实例代码(3种图)
2017/07/10 Javascript
JS数组去重常用方法实例小结【4种方法】
2018/05/28 Javascript
详解Require.js与Sea.js的区别
2018/08/05 Javascript
vue中封装axios并实现api接口的统一管理
2020/12/25 Vue.js
windows下ipython的安装与使用详解
2016/10/20 Python
django定期执行任务(实例讲解)
2017/11/03 Python
python 检查数据中是否有缺失值,删除缺失值的方式
2019/12/02 Python
python GUI库图形界面开发之PyQt5开发环境配置与基础使用
2020/02/25 Python
python 对一幅灰度图像进行直方图均衡化
2020/10/27 Python
YOOX美国官方网站:全球著名的多品牌时尚网络概念店
2016/09/11 全球购物
Piercing Pagoda官网:耳环、戒指、项链、手链等
2020/09/28 全球购物
Nike俄罗斯官方网站:Nike RU
2021/03/05 全球购物
二手书店创业计划书
2014/01/16 职场文书
KTV的创业计划书范文
2014/02/02 职场文书
家长评语和期望
2014/02/10 职场文书
投资合作协议书
2014/04/17 职场文书
三好学生个人总结
2015/02/15 职场文书
确保工程质量承诺书
2015/04/29 职场文书
2015年高校保卫处工作总结
2015/07/23 职场文书
Java Socket实现Redis客户端的详细说明
2021/05/26 Redis
React 高阶组件HOC用法归纳
2021/06/13 Javascript
Vue router配置与使用分析讲解
2022/12/24 Vue.js