tkinter 摸索着做了一个天气界面(多线程), 加一个爬虫
本帖最后由 Cool_Breeze 于 2021-3-2 00:13 编辑#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)
continue
elif n == 5:
var2.set(res)
continue
varL.set(res)
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,
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()
爬虫部分:#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)
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))
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 + " " + bList)
# datalist.append(bList + " : " + aList + " " + bList)
datalist.append(date)
datalist.append(aList)
# ['空气优']
c = re.compile('''<span>(空气..?)</span>''')
cList = c.findall(Text)
# ['43']
d = re.compile('''<span>(\d+)</span>''')
dList = d.findall(Text)
datalist.append(cList + " " + dList)
# ['阵雨']
e = re.compile('''<span class="banner-whether-desc1">(.*?)</span><span class="banner-whether-desc2">(.*?)</span>''')
eList = e.findall(Text)
datalist.append(eList)
datalist.append(eList)
# ['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)
# ['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)
# ['无']
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)
# [('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))
示例图片:
尝试用Java写了下,控制台打印
package com.sun;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.model.OOSpider;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.pipeline.PageModelPipeline;
import java.util.List;
/**
* 天气爬虫
*
* @ClassName: TianqiSpider
*/
public class TianqiSpider {
@ExtractBy(value = "//[@class='weaday7-day']/text()")
private List<String> date;
@ExtractBy(value = "//div[@class='weaday7-wrap']/ul/li/a/em/text()")
private List<String> riqi;
@ExtractBy(value = "//div[@class='weaday7-wrap']/ul/li/a/font/text()")
private List<String> weather;
@ExtractBy(value = "//div[@class='weaday7-wrap']/ul/li/a/p/text()")
private List<String> weather1;
@ExtractBy(value = "//div[@class='weaday7-wrap']/ul/li/a/p/text()")
private List<String> weather2;
//省略setter/getter
public static void main(String[] args) throws InterruptedException {
OOSpider.create(Site.me(),
new TianQiPipeline()
, TianqiSpider.class)
.addUrl("http://tianqi.2345.com")//
.thread(1)//
.run();
}
}
class TianQiPipeline implements PageModelPipeline<TianqiSpider> {
@Override
public void process(TianqiSpider tianqiSpider, Task task) {
for (int i = 0; i < tianqiSpider.getDate().size(); i++) {
System.out.println(tianqiSpider.getDate().get(i)+" "+tianqiSpider.getRiqi().get(i)
+" "+tianqiSpider.getWeather().get(i) +" "+tianqiSpider.getWeather1().get(i)
+" "+tianqiSpider.getWeather2().get(i) +
"\t");
}
}
} 老飞机 发表于 2020-12-2 22:49
你这个是什么jar呢
webmagic 爬虫
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-core</artifactId>
<version>0.7.3</version>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId>
<version>0.7.3</version>
</dependency> 看看热闹,太难了 html写界面绝对一流, 不二选择,其它的都是扯 支持一下!!,给力! 支持一下 ,但是要做UI还是QT5给力啊 如此地步小白羡慕呵 提示bs4没有这个? Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(),__main__.__dict__)
File "<string>", line 4, in <module>
ModuleNotFoundError: No module named 'bs4'
wxd5288 发表于 2020-11-29 16:25
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_ru ...
pip install bs4
页:
[1]
2