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

yipeiwu_com5年前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爬虫设置代理IP(图文)

Python爬虫设置代理IP(图文)

在爬虫的过程中,我们经常会遇见很多网站采取了防爬取技术,或者说因为自己采集网站信息的强度和采集速度太大,给对方服务器带去了太多的压力。 如果你一直用同一个代理ip爬取这个网页,很有可能i...

一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息

一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息

概述 这是一个简单的python爬虫程序,仅用作技术学习与交流,主要是通过一个简单的实际案例来对网络爬虫有个基础的认识。 什么是网络爬虫 简单的讲,网络爬虫就是模拟人访问web站点的行为...

详解Python网络爬虫功能的基本写法

网络爬虫,即Web Spider,是一个很形象的名字。把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛。 1. 网络爬虫的定义 网络蜘蛛是通过网页的链接地址来寻找网页的...

python爬虫模拟浏览器的两种方法实例分析

python爬虫模拟浏览器的两种方法实例分析

本文实例讲述了python爬虫模拟浏览器的两种方法。分享给大家供大家参考,具体如下: 爬虫爬取网站出现403,因为站点做了防爬虫的设置 一、Herders 属性 爬取CSDN博客 i...

Python代理IP爬虫的新手使用教程

Python代理IP爬虫的新手使用教程

前言 Python爬虫要经历爬虫、爬虫被限制、爬虫反限制的过程。当然后续还要网页爬虫限制优化,爬虫再反限制的一系列道高一尺魔高一丈的过程。爬虫的初级阶段,添加headers和ip代理可...