Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3577288
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-09-30 17:13:17

from requests import get

from bs4 import BeautifulSoup as bs

from datetime import datetime as dt

from os import path

import tkinter as tk

def Today(style=1):

    date = dt.today()

    if style!=1: return f'{date.month}{date.day}'

    return f'{date.year}-{date.month:02}-{date.day:02}'

def SinaNews(style=1):

    url1 = '****.com.cn/'

    if style==1: url1 += 'world'

    elif style==2: url1 += 'china'

    else: url1='****.com.cn/'

    text = get(url1)

    text.encoding='uft-8'

    soup = bs(text.text,'html.parser')

    aTags = soup.find_all("a")

    return [(t.text,t['href']) for t in aTags if Today() in str(t)]

def NewsList(i):

    global news

    news = SinaNews(i)

    tList.delete(0,tk.END)

    for idx,item in enumerate(news):

        tList.insert(tk.END,f'{idx+1:03} {item[0]}')

    tText.config(state=tk.NORMAL)

    tText.delete(0.0,tk.END)

    tText.config(state=tk.DISABLED)

    NewsShow(0)

def NewsList1(): NewsList(1)

def NewsList2(): NewsList(2)

def NewsList3(): NewsList(3)

def NewsShow(idx):

    if idx!=0:

        idx = tList.curselection()[0]

    title,url = news[idx][0],news[idx][1]

    html = get(url)

    html.encoding='uft-8'

    soup = bs(html.text,'html.parser')

    text = soup.find('div',id='article').get_text().strip()

    text = text.replace('外汇跟单gendan5.com点击进入专题:','相关专题:')

    text = text.replace('  ','\n  ')

    while '\n\n\n' in text:

        text = text.replace('\n\n\n','\n\n')

    tText.config(state=tk.NORMAL)

    tText.delete(0.0,tk.END)

    tText.insert(tk.END, title+'\n\n'+text)

    tText.config(state=tk.DISABLED)

def InitWindow(self,W,H):

    Y = self.winfo_screenheight()

    winPosition = str(W)+'x'+str(H)+'+8+'+str(Y-H-100)

    self.geometry(winPosition)

    icoFile = 'favicon.ico'

    f = path.exists(icoFile)

    if f: win.iconbitmap(icoFile)

    self.resizable(False,False)

    self.wm_attributes('-topmost',True)

    self.title(bTitle[0])

    SetControl()

    self.update()

    self.mainloop()

def SetControl():

    global tList,tText

    tScroll = tk.Scrollbar(win, orient=tk.VERTICAL)

    tScroll.place(x=450,y=320,height=300)

    tList = tk.Listbox(win,selectmode=tk.BROWSE,yscrollcommand=tScroll.set)

    tScroll.config(command=tList.yview)

    for idx,item in enumerate(news):

        tList.insert(tk.END,f'{idx+1:03} {item[0]}')

    tList.place(x=15,y=320,width=435,height=300)

    tList.select_set(0)

    tList.focus()

    bW,bH = 70,35    #按钮的宽高

    bX,bY = 95,270    #按钮的坐标

    tBtn1 = tk.Button(win,text=bTitle[1],command=NewsList1)

    tBtn1.place(x=bX,y=bY,width=bW,height=bH)

    tBtn2=tk.Button(win,text=bTitle[2],command=NewsList2)

    tBtn2.place(x=bX+100,y=bY,width=bW,height=bH)

    tBtn3 = tk.Button(win,text=bTitle[3],command=NewsList3)

    tBtn3.place(x=bX+200,y=bY,width=bW,height=bH)

    tScroll2 = tk.Scrollbar(win, orient=tk.VERTICAL)

    tScroll2.place(x=450,y=10,height=240)

    tText = tk.Text(win,yscrollcommand=tScroll2.set)

    tScroll2.config(command=tText.yview)

    tText.place(x=15,y=10,width=435,height=240)

    tText.config(state=tk.DISABLED,bg='azure',font=('宋体', '14'))

    NewsShow(0)

    tList.bind("",NewsShow)

if __name__=='__main__':

    win = tk.Tk()

    bTitle = ('今日新闻','国际新闻','国内新闻','军事新闻')

    news = SinaNews()

    InitWindow(win,480,640)

阅读(1033) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~