【实例简介】

【实例截图】

【核心代码】

from tkinter import *

import tkinter as tk

import requests

from PIL import ImageTk as itk

class MyFrame(Frame):

def __init__(self):

self.root=Tk()

self.root.title("天气查询")

self.root.geometry('1200x700 400 220')

bg = tk.Canvas(self.root, width=1200, height=600, bg='white')

#self.img = itk.PhotoImage(file="bg.gif")

#bg.place(x=100, y=40)

#bg.create_image(0, 0, anchor=NW, image=self.img)

self.city = Entry(self.root, width=16, font=("仿宋", 18, "normal"))

self.city.place(x=200, y=60)

citylabel=Label(self.root,text='查询城市',font=("仿宋", 18, "normal"))

citylabel.place(x=80,y=60)

#查询按钮

chaxun = Button(self.root, width=10, height=3, text="查询", bg='#00CCFF', bd=5, font="bold")

chaxun.bind("", self.search)

chaxun.place(x=800, y=50)

self.result=Listbox(self.root,heigh=18,width=65,font=("仿宋", 20, "normal"))#显示天气框

self.result.place(x=125,y=120)

def tianqiforecast(self,searchcity):

print('请输入所要查询天气的城市:')

city = searchcity

# city='minquan'

url = 'http://toy1.weather.com.cn/search?cityname=' city '&callback=success_jsonpCallback&_=1548048506469'

# print(url)

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',

'Cookie': '__guid=182823328.3322839646442213000.1543932524694.901; vjuids=1858d43b6.167798cbdb7.0.8c4d7463d5c5d; vjlast=1543932526.1543932526.30; userNewsPort0=1; f_city=%E5%B9%B3%E9%A1%B6%E5%B1%B1%7C101180501%7C; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1543932526,1543932551,1543932579; Wa_lvt_1=1547464114,1547464115,1547880054,1547983123; defaultCty=101181001; defaultCtyName=%u5546%u4E18; monitor_count=6; Wa_lpvt_1=1547983809'

}

response = requests.get(url, headers=headers)

html1 = response.content.decode('utf-8')

# print(html)

citys = re.findall('"ref":"(.*?)~.*?~(.*?)~.*?~(.*?)~.*?~.*?~.*?~.*?~(.*?)"', html1, re.S)

if (len(citys) == 0):

print('未查找到该城市')

exit(-5)

for i in range(0, len(citys)):

print(i 1, ':%14s%14s%14s%14s ' % (citys[i][0], citys[i][3], citys[i][2], citys[i][1]))

#choose = int(input('请选择城市编号:[1~' str(len(citys)) ']\n'))

choose=1

if (len(citys[choose - 1][0]) == 9):

if (citys[choose - 1][0][0] != '1' or citys[choose - 1][0][1] != '0' or citys[choose - 1][0][2] != '1'):

print('暂时无法查询国外天气,程序已退出')

exit(404)

else:

url2 = 'http://www.weather.com.cn/weathern/' citys[choose - 1][0] '.shtml'

responseweather = requests.get(url2, headers=headers)

html2 = responseweather.content.decode('utf-8')

weather = re.findall('

(.*?)*?"date-i.*?">(.*?)<.*?', html2, re.S)

weather.append(re.findall(

'

(.*?)

',

html2, re.S))

Hightempture = re.findall(

'

re.S)

Lowtempture = re.findall(

'var eventNight =\["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];',

html2, re.S)

# print(Hightempture,Lowtempture)

b='查询城市为:' str(citys[choose - 1][3]) ' ' str(citys[choose - 1][1])

self.result.insert(END, b)

for i in range(0, 8):

# print(weather[i])

'''print("%4s%4s%10s\t\t\t%s℃ ~ %s℃\t\t\t%s%s%-s" % (

weather[i][0], weather[i][1], weather[8][i][0], Lowtempture[0][i], Hightempture[0][i],

weather[8][i][1],

weather[8][i][3], weather[8][i][2]))'''

a = weather[i][0] ' ' weather[i][1] ' ' weather[8][i][0] ' ' Lowtempture[0][i] '℃ ~ ' \

Hightempture[0][i] '℃ ' weather[8][i][1] weather[8][i][3] weather[8][i][2]

self.result.insert(END, a)

if (len(citys[choose - 1][0]) == 12):

url2 = 'http://forecast.weather.com.cn/town/weathern/' citys[choose - 1][0] '.shtml'

responseweather = requests.get(url2, headers=headers)

html2 = responseweather.content.decode('utf-8')

weather = re.findall('

(.*?)*?"date-i.*?">(.*?)<.*?', html2, re.S)

html2 = re.sub('lt;', '<', html2)

weather.append(re.findall(

'

\\r\\n(.*?)\\r\\n',

html2, re.S))

Hightempture = re.findall(

'var eventDay = \["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', html2, re.S)

Lowtempture = re.findall(

'var eventNight = \["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];',

html2, re.S)

# print(Hightempture,Lowtempture)

b='查询城市为:' str(citys[choose - 1][3]) ' ' str( citys[choose - 1][2]) ' ' str(citys[choose - 1][1])

self.result.insert(0,b)

#print(weather[8][2])

#print(weather)

for i in range(0, 8):

# print(weather[i])

'''print("%4s%4s%10s\t\t\t%s℃ ~ %s℃\t\t\t%s%s%-s" % (

weather[i][0], weather[i][1], weather[8][i][0], Lowtempture[0][i], Hightempture[0][i],

weather[8][i][1],

weather[8][i][3], weather[8][i][2]))'''

a = weather[i][0] ' ' weather[i][1] ' ' weather[8][i][0] ' ' Lowtempture[0][i] '℃ ~ ' \

Hightempture[0][i] '℃ ' weather[8][i][1] weather[8][i][3] weather[8][i][2]

self.result.insert(END, a)

def search(self,event):

mycity=self.city.get()

if(mycity!=''):

self.result.delete(0,END)

self.city.delete(0,END)

self.tianqiforecast(mycity)

if __name__=='__main__':

myframe=MyFrame()

myframe.root.mainloop()

python查天气预报_python查询全国天气预报相关推荐

  1. ajax查询全国天气预报,使用聚合数据api接口

    空值情况 苏州 界面如上,可以根据自己需求再添加一些内容. 功能需求:根据城市名称查询该城市的天气情况,输入空值的时候默认查询北京天气 这个是我使用聚合数据提供的免费20次请求全国天气预报API,注册 ...

  2. php 当地天气预报,PHP调用全国天气预报数据接口查询天气示例

    本文实例讲述了PHP调用全国天气预报数据接口查询天气.分享给大家供大家参考,具体如下: 基于PHP的聚合数据全国天气预报API服务请求的代码样例 本代码示例是基于PHP的聚合数据全国天气预报API服务 ...

  3. android天气预报sdk,Android全国天气预报SDK演示

    今天要分享的是Android手机设备集成天气预报功能的方案.我采用的方案是使用聚合数据上的全国天气预报API.如需要,可申请:https://www.juhe.cn/docs/api/id/39 实现 ...

  4. python查天气预报_Python编写一个天气预报查询系统

    Python编写一个天气预报查询系统 学了Python这么久 可以用它来做许多好玩的小程序哦! 这里给 大家做了个查询天气预报的小程序! 可以查询未来几天的天气! 需要大家自己修改下代码即可! 首先我 ...

  5. python查天气预报_Python如何读取天气预报

    Python如何读取天气预报? import urllib2 import json # 101180101 url = 'http://m.weather.com.cn/data/10118010 ...

  6. python查题_python 数据库连表查询习题

    # 多表查询 # 连表查 # 内连接 必须左表和右表中条件互相匹配的项才会被显示出来 # 表1 inner join 表2 on 条件 # 外链接 会显示条件不匹配的项 # left join 左表显 ...

  7. vfp 调用接口取数据_2分钟教你调用全国天气预报数据接口

    在智能手机软件的装机量中,天气预报的APP排在比较靠前的位置.说明大家对天气的关注度还是比价高的,毕竟无论工作还是度假旅游等各种活动都需要根据自然天气来安排. 那么,接下来就花2分钟,聚合菌教你如何调 ...

  8. 全国天气预报信息数据 API

    此文章对开放数据接口 API 之「全国天气预报信息数据 API」进行了功能介绍.使用场景介绍以及调用方法的说明,供用户在使用数据接口时参考之用,并对实战开发进行了视频演示. 1. 产品功能 接口开放了 ...

  9. java微信天气查询接口,全国天气预报信息 API 接口

    全国天气预报信息 API 接口 精确到行政区的7日天气预报. 1. 产品功能 全国天气预报每隔 6 小时更新数据: 精确到行政区级别的天气预报数据: 提供最长 7 天的天气预报数据: 提供每日小时级别 ...

最新文章

  1. 信息化道路上,这两家龙头企业做了什么
  2. 机房为什么要设计冷热通道,设计原则及如何施工?
  3. MIMIC 以太坊医疗项目开发(4)Axios
  4. ASP.NET MVC TagBuilder使用
  5. SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
  6. Javascript常用对象的属性和方法
  7. WPF基础到企业应用系列7——深入剖析依赖属性(WPF/Silverlight核心)
  8. PLSQL_统计信息系列07_数据字典统计信息的导出和导入
  9. 十大排序算法——冒泡法
  10. 虚拟显示器软件(win10及以上)
  11. 易语言之编译后图标模糊的处理方案
  12. navicat for mysql 10.0.11简体中文破解版 绿色版
  13. 1小时就会的测试用例【直播推流/拉流】
  14. 计算机逻辑门电路图,关于门电路详细解析
  15. 数据库及对应数据文件使用查询
  16. vscode编辑器搜索小技巧
  17. springboot项目有哪几种运行方式
  18. 深度优先搜索和深度优先搜索的区别
  19. Windows下virtualbox相关软件安装设置全过程
  20. 【BZOJ1412】【ZJOI2009】狼和羊的故事(网络流)

热门文章

  1. [rejected] master -> master (non-fast-forward) error: failed to push some refs to
  2. java代码实现登录跳转_java实现登录案例
  3. 精准营销服务平台设计-基于大数据和AI的商业智能平台
  4. PB powerbuilder 仿EXCEL筛选V2
  5. 判断是否已经安装vc2008运行时库
  6. 微信视频号怎么查看浏览历史记录
  7. 关于调用百度云OCR身份证识别接口,用Java语言,识别结果缺少身份证号码的问题解决
  8. 计算机主机报警 声,电脑出现报警声怎么办?电脑出现报警声的原因
  9. 信息学奥赛C++编程:奥运奖牌计数
  10. 认知电子战 | 无线电中的认知理论