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实现求解括号匹配问题的方法
Apr 17 Python
Python实现删除时保留特定文件夹和文件的示例
Apr 27 Python
python使用webdriver爬取微信公众号
Aug 31 Python
Python读取txt某几列绘图的方法
Oct 14 Python
Python编程图形库之Pillow使用方法讲解
Dec 28 Python
Python目录和文件处理总结详解
Sep 02 Python
python之yield和Generator深入解析
Sep 18 Python
python第三方库学习笔记
Feb 07 Python
python实现飞船大战
Apr 24 Python
python小白切忌乱用表达式
May 29 Python
python ConfigParser库的使用及遇到的坑
Feb 12 Python
python双向链表实例详解
May 25 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
DSP接收机前端设想
2021/03/02 无线电
从零开始学YII2框架(六)高级应用程序模板
2014/08/20 PHP
PHP简单选择排序算法实例
2015/01/26 PHP
JSON用法之将PHP数组转JS数组,JS如何接收PHP数组
2015/10/08 PHP
两种php去除二维数组的重复项方法
2015/11/04 PHP
PHP 生成微信红包代码简单
2016/03/25 PHP
[原创]解决wincache不支持64位PHP5.5/5.6的问题(提供64位wincache下载)
2016/06/22 PHP
php5.3后静态绑定用法详解
2016/11/11 PHP
laravel框架实现去掉URL中index.php的方法
2019/10/12 PHP
基于JavaScript实现通用tab选项卡(通用性强)
2016/01/07 Javascript
值得分享的bootstrap table实例
2016/09/22 Javascript
Bootstrap模态框水平垂直居中与增加拖拽功能
2016/11/09 Javascript
js 性能优化之快速响应的用户界面
2017/02/15 Javascript
原生JS实现的碰撞检测功能示例
2018/05/18 Javascript
JS面向对象编程实现的拖拽功能案例详解
2020/03/03 Javascript
[01:34]2014DOTA2 TI预选赛预选赛 选手比赛房大揭秘!
2014/05/20 DOTA
Python通过paramiko远程下载Linux服务器上的文件实例
2018/12/27 Python
python基于paramiko将文件上传到服务器代码实现
2019/07/08 Python
python多线程扫描端口(线程池)
2019/09/04 Python
python socket通信编程实现文件上传代码实例
2019/12/14 Python
通过实例解析Python RPC实现原理及方法
2020/07/07 Python
python正则表达式的懒惰匹配和贪婪匹配说明
2020/07/13 Python
通过案例解析python鸭子类型相关原理
2020/10/10 Python
日本动漫周边服饰销售网站:Atsuko
2019/12/16 全球购物
意大利在线高尔夫商店:Online Golf
2021/03/09 全球购物
介绍一下OSI七层模型
2012/07/03 面试题
关于逃课的检讨书
2014/01/23 职场文书
《逃家小兔》教学反思
2014/02/23 职场文书
活动倡议书范文
2014/05/13 职场文书
宣传工作经验材料
2014/06/02 职场文书
2015年计算机教学工作总结
2015/07/22 职场文书
2016年圣诞节义工活动总结
2016/04/01 职场文书
导游词之苏州盘门景区
2019/11/12 职场文书
导游词之江南周庄
2019/12/06 职场文书
Python中的程序流程控制语句
2022/02/24 Python
微信小程序纯CSS实现无限弹幕滚动效果
2022/09/23 HTML / CSS