好友
阅读权限 25
听众
最后登录 1970-1-1
本帖最后由 Cool_Breeze 于 2021-3-2 00:13 编辑
[Python] 纯文本查看 复制代码
#coding=utf-8
from tkinter import *
from tkinter import ttk
from sys import exit
from tianqi import tianqi2345
from functools import partial as pt
from threading import Thread
from queue import Queue
def home_mian():
font = 'Consolas', 11
home = Tk()
home.configure(bg='#ffc0cb')
home.title('天气查询系统 v1.0')
# 窗口居中
homeWidth = 585
homeHeight = 680
home.geometry("%dx%d+%d+%d" %(homeWidth,homeHeight,
(home.winfo_screenwidth() - homeWidth) / 2,
(home.winfo_screenheight() - homeHeight) / 2))
############################################################
def tishi():
var.set('正在获取网页信息...')
dataFrame.update() #更新窗口
def dataWeather(n):
queue = Queue()
# th = Thread(target=_run, args=(n,queue), daemon=True)
th = Thread(target=_run, args=(n,queue), daemon=True)
th.start()
def _run(n, queue):
tishi()
count = 0
# 爬虫 获取数据
tianqi2345(n, queue)
res, qitian = queue.get()
# 获取数据失败
if res is False:
var.set(qitian)
return False
for n in range(10):
if n == 0:
var1.set(res[n])
continue
elif n == 5:
var2.set(res[n])
continue
varL[count].set(res[n])
count += 1
# 清空表单
temp = sheetD.get_children()
for item in temp:
sheetD.delete(item)
# 插入数据
for each in qitian:
sheetD.insert('','end',values=each)
var.set('以上数据来源:http://tianqi.2345.com/')
def exit_():
exit()
############################################################
# 城市,温度 特写
# 城市
var1 = StringVar()
# 温度
var2 = StringVar()
# 框架
texie = Frame(home, bg='#ffc0cb', bd=20, padx=20, pady=4,
relief='flat', width=100, height=100)
# texie.pack(fill=X)
texie.grid(row=0, column=0)
cty = Label(texie, textvariable=var1,
font="华康少女字体, 40", fg="#fffff0", bg='#ffc0cb')
cty.grid(row=0, column=0, padx='1c', pady='1c', sticky='s')
cty1 = Label(texie, textvariable=var2,
font="华康少女字体, 80", fg="#fffff0", bg='#ffc0cb')
cty1.grid(row=0, column=1, sticky='s')
############################################################
# 其他信息
# 框架
dataFrame = Frame(home, bg='#ffc0cb', bd=10)
dataFrame.grid(row=1, column=0)
# dataFrame.pack(fill=X)
varL = []
for n in range(8):
varL.append(StringVar())
Llist = []
for n in range(8):
Llist.append(Label(dataFrame, textvariable=varL[n],
bg='#ffc0cb', font=font))
for n in Llist:
n.pack(fill=X, side=TOP)
############################################################
# 未来七天
# 框架
data = []
sheetFrame = Frame(home, bg='#ffc0cb', padx=40)
sheetFrame.grid(row=2, column=0)
sheetD = ttk.Treeview(sheetFrame, column=['1', '2', '3',
'4', '5'], show='headings', height=8)
sheetD.column('1', width=100, anchor='center')
sheetD.column('2', width=100, anchor='center')
sheetD.column('3', width=100, anchor='center')
sheetD.column('4', width=100, anchor='center')
sheetD.column('5', width=100, anchor='center')
sheetD.heading('1', text='日期')
sheetD.heading('2', text='日期')
sheetD.heading('3', text='天气')
sheetD.heading('4', text='温度 ℃')
sheetD.heading('5', text='空气质量')
for each in data:
sheetD.insert('','end',values=each)
sheetD.grid(row=0, column=0)
# 数据来源提示
var = StringVar()
L0 = Label(sheetFrame, textvariable=var, bg='#ffc0cb', font=font)
L0.grid(row=1, column=0)
############################################################
buttonFrame = Frame(home, bg='#ffc0cb', bd=20, padx=20, pady=4,
relief='flat')
buttonFrame.grid(row=3, column=0)
# 封装函数参数
mybutton = pt(Button, buttonFrame, font=font, bg='#ffc0cb',
width=10, height=1)
zg_bt = mybutton(text='自流井',
command=lambda:dataWeather(1))
zg_bt.grid(row=0, column=0)
cd_bt = mybutton(text='成都',
command=lambda:dataWeather(2))
cd_bt.grid(row=0, column=1)
dg_bt = mybutton(text='东莞',
command=lambda:dataWeather(3))
dg_bt.grid(row=0, column=2)
dg_bt = mybutton(text='上海',
command=lambda:dataWeather(4))
dg_bt.grid(row=0, column=3)
ex_bt = mybutton(text='退出',
command=sys.exit)
ex_bt.grid(row=0, column=4)
home.mainloop()
if __name__ == '__main__':
home_mian()
爬虫部分:[Python] 纯文本查看 复制代码
#coding=utf-8
import re
import time
import urllib.request,urllib.error
from enum import Enum
def tianqi2345(number, queue):
city_list = [
'positionCityID=59289; positionCityPinyin=zigong; lastCountyId=71992; lastCountyPinyin=ziliujing',
'lastCountyId=56294; lastCountyPinyin=chengdu; lastCountyTime=1591617161',
'lastCountyId=59289; lastCountyPinyin=dongguan; lastCountyTime=1591612281',
'lastCountyId=58362; lastCountyPinyin=shanghai; lastCountyTime=1595925050',
]
url = 'http://tianqi.2345.com/'
html, err = askurl(url, city_list[number-1])
if html is None:
queue.put((False, err))
return
queue.put(getdata(html))
class Week(Enum):
周一 = 1
周二 = 2
周三 = 3
周四 = 4
周五 = 5
周六 = 6
周日 = 0
def askurl(url,cookkey):
head = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': 'qd_dz_ct=59289; sts=1; theme=-1; wc=59289; lc=59289; lc2=59289; wc_n=%25u4E1C%25u839E; ' + cookkey,
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3641.400 QQBrowser/10.4.3284.400'
}
req = urllib.request.Request(url=url, headers=head)
try:
response = urllib.request.urlopen(req)
if response.chunked:
# print('访问:{} 失败!\n请检查网络是否可以正确的访问Internet!'.format(url))
return None, response.chunked
except urllib.error.URLError as err:
# print('\n网络连接失败!\n请检查网络连接!', err)
return None, err
try:
return response.read().decode("utf-8"), None
except Exception as err:
return None, err
def DeBug(html):
with open("t.txt", "w", encoding="utf-8") as f:
f.write(html)
def getdata(html):
try:
date = Week(int(time.strftime("%w"))).name
Text = html
datalist = []
aa = re.compile('''<h3>(.*?)周边地区天气预报</h3>''')
datalist.append(aa.findall(Text)[0])
a = re.compile('''<div class="banner-city-date">(.+?)<span style="display: none;">(.+?)</span></div>''')
aList = a.findall(Text)
b = re.compile('''<div class="banner-right-con-list-time" data-txt="(?P<date>\d+/\d+)">(.*?).(?P=date)</div>''')
bList = b.findall(Text)
datalist.append(aList[0][1] + " " + bList[1][0])
# datalist.append(bList[1][1] + " : " + aList[0][1] + " " + bList[1][0])
datalist.append(date)
datalist.append(aList[0][0])
# ['空气优']
c = re.compile('''<span>(空气..?)</span>''')
cList = c.findall(Text)
# ['43']
d = re.compile('''<span>(\d+)</span>''')
dList = d.findall(Text)
datalist.append(cList[0] + " " + dList[0])
# ['阵雨']
e = re.compile('''<span class="banner-whether-desc1">(.*?)</span><span class="banner-whether-desc2">(.*?)</span>''')
eList = e.findall(Text)
datalist.append(eList[0][0])
datalist.append(eList[0][1])
# ['5~14°']
f = re.compile('''<div class="banner-whether-list-itm clearfix"><i class="wea-info-temper"></i>温度<span>(.*?)</span></div>''')
fList = f.findall(Text)
datalist.append("温度:" + fList[0])
# ['91%']
g = re.compile('''<div class="banner-whether-list-itm clearfix"><i class="wea-info-humidity"></i>湿度<span>(.*?)</span></div>''')
gList = g.findall(Text)
datalist.append("湿度:" + gList[0])
# ['无']
h = re.compile('''<div class="banner-whether-list-itm clearfix banner-whether-list-itm-no"><i class="wea-info-sun"></i>紫外线<span>(.*?)</span></div>''')
hList = h.findall(Text)
datalist.append("紫外线:" + hList[0])
# [('02/28', '昨天', '小雨', '11~14°', '优'), ('03/01', '今天', '小雨', '5~14°', '优')]
j = re.compile('''
<div class="banner-right-con-list-time" data-txt="(?P<date>\d+/\d+)">(..).*(?P=date)</div>
<div
class="banner-right-con-list-icon.*?"></div>
<div class="banner-right-con-list-status">(.*?)</div>
<div class="banner-right-con-list-temp">(.*?)</div>
<div class="banner-right-con-list-grade">(.*?)</div>''')
jList = j.findall(Text)
except Exception as err:
return False, err
return (datalist, jList)
if __name__ == '__main__':
from queue import Queue
q = Queue()
print(tianqi2345(4, q))
示例图片:
免费评分
查看全部评分