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脚本实现代码行数统计代码分享
Mar 10 Python
Pycharm学习教程(7)虚拟机VM的配置教程
May 04 Python
tensorflow实现softma识别MNIST
Mar 12 Python
python使用folium库绘制地图点击框
Sep 21 Python
python3.7 使用pymssql往sqlserver插入数据的方法
Jul 08 Python
Python OpenCV实现鼠标画框效果
Aug 19 Python
使用pyecharts生成Echarts网页的实例
Aug 12 Python
python 使用while写猜年龄小游戏过程解析
Oct 07 Python
python机器学习库xgboost的使用
Jan 20 Python
Python发送手机动态验证码代码实例
Feb 28 Python
用python按照图像灰度值统计并筛选图片的操作(PIL,shutil,os)
Jun 04 Python
pytorch加载预训练模型与自己模型不匹配的解决方案
May 13 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 数组的指针操作实现代码
2011/02/08 PHP
通过PHP的内置函数,通过DES算法对数据加密和解密
2012/06/21 PHP
php递归使用示例(php递归函数)
2014/02/14 PHP
php常用字符串处理函数实例分析
2014/11/22 PHP
Yii把CGridView文本框换成下拉框的方法
2014/12/03 PHP
php函数重载的替代方法--伪重载详解
2015/05/08 PHP
session 加入redis的实现代码
2016/07/15 PHP
php根据命令行参数生成配置文件详解
2019/03/15 PHP
一些常用的JS功能函数(2009-06-04更新)
2009/06/04 Javascript
jquery多浏览器捕捉回车事件代码
2010/06/22 Javascript
IE事件对象(The Internet Explorer Event Object)
2012/06/27 Javascript
jQuery-Tools-overlay 使用介绍
2012/07/14 Javascript
javascript实现TreeView 无刷新展开的实例代码
2013/07/13 Javascript
JSON 的正确用法探讨:Pyhong、MongoDB、JavaScript与Ajax
2016/05/15 Javascript
深入对Vue.js $watch方法的理解
2017/03/20 Javascript
浅谈Express异步进化史
2017/09/09 Javascript
vue router嵌套路由在history模式下刷新无法渲染页面问题的解决方法
2018/01/25 Javascript
详解ES6 Fetch API HTTP请求实用指南
2018/11/14 Javascript
使用mpvue搭建一个初始小程序及项目配置方法
2018/12/03 Javascript
Vue js 的生命周期(看了就懂)(推荐)
2019/03/29 Javascript
解决vue单页面应用中动态修改title问题
2019/06/09 Javascript
vue单页应用的内存泄露定位和修复问题小结
2019/08/02 Javascript
微信小程序swiper实现文字纵向轮播提示效果
2020/01/21 Javascript
进一步了解Python中的XML 工具
2015/04/13 Python
Python实现单词拼写检查
2015/04/25 Python
在Windows服务器下用Apache和mod_wsgi配置Python应用的教程
2015/05/06 Python
python 创建弹出式菜单的实现代码
2017/07/11 Python
Python获取系统所有进程PID及进程名称的方法示例
2018/05/24 Python
python消费kafka数据批量插入到es的方法
2018/12/27 Python
详解Python函数式编程—高阶函数
2019/03/29 Python
Python列表切片常用操作实例解析
2019/12/16 Python
基于python3生成标签云代码解析
2020/02/18 Python
初中数学教学反思
2014/01/16 职场文书
如何编写优秀的食品项目创业计划书
2014/01/23 职场文书
不尊敬老师的检讨书
2014/12/21 职场文书
2016大学生诚信考试承诺书
2016/03/25 职场文书