吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2765|回复: 18
收起左侧

[Python 转载] tkinter 摸索着做了一个天气界面(多线程), 加一个爬虫

  [复制链接]
Cool_Breeze 发表于 2020-11-27 18:58
本帖最后由 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))


示例图片:
我的影片2.gif

免费评分

参与人数 1吾爱币 +1 收起 理由
何故 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

奔跑的蜗牛119 发表于 2020-12-2 13:37
尝试用Java写了下,控制台打印
[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[1]/text()")
    private List<String> weather1;
    @ExtractBy(value = "//div[@class='weaday7-wrap']/ul/li/a/p[2]/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");
        }
    }
}
奔跑的蜗牛119 发表于 2020-12-3 08:35
老飞机 发表于 2020-12-2 22:49
你这个是什么jar呢

webmagic 爬虫
[XML] 纯文本查看 复制代码
        
        <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>
头像被屏蔽
zhaoyun007 发表于 2020-11-27 19:31
hshcompass 发表于 2020-11-27 19:38
看看热闹,太难了
boxer 发表于 2020-11-27 20:52
html写界面绝对一流, 不二选择,其它的都是扯
他是传说 发表于 2020-11-27 21:13
支持一下!!,给力!
林英俊 发表于 2020-11-27 22:25
支持一下 ,但是要做UI还是QT5给力啊
寒冰流火 发表于 2020-11-28 08:53
如此地步小白羡慕呵
wxd5288 发表于 2020-11-29 16:23
提示bs4没有这个?
wxd5288 发表于 2020-11-29 16:25
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'

[Program finished]
 楼主| Cool_Breeze 发表于 2020-11-30 11:40
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
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 21:42

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表