Python “今日新聞”一個小程式,拿走就能用!
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("<Double-Button-1>",NewsShow)
if __name__=='__main__':
win = tk.Tk()
bTitle = (' 今日新聞 ',' 國際新聞 ',' 國內新聞 ',' 軍事新聞 ')
news = SinaNews()
InitWindow(win,480,640)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2794646/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 『技術分享』—— 我的第一個微信小程式-趣聞微信小程式
- Westore 1.0 正式釋出 - 小程式框架一個就夠框架
- 如何使用python做一個聊天小程式?Python
- GoCN 每日新聞 -- 筆記Go筆記
- 小 200 行 Python 程式碼做了一個換臉程式Python
- 每日新聞郵件訂閱
- 7個Python實戰專案(附原始碼),拿走就用Python原始碼
- 一看就懂的python小程式-支援多執行緒聊天例項Python執行緒
- 程式猿的一些幽默趣聞 個個經典
- 程式猿的一些幽默趣聞 個個經典
- 我的第一個微信小程式 (Discuz!! + 微信小程式)微信小程式
- 我的第一個微信小程式 (Discuz!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + 微信小程式)微信小程式
- 關於每日新聞郵件訂閱
- mpvue寫一個CPASS小程式Vue
- 小程式canvas的一個坑Canvas
- 不用下載和安裝,這三個完全不收費的微信小程式,搜一搜就能用啦微信小程式
- 如何製作一個鮮花微信小程式?看完這篇新手教程就知道了!微信小程式
- 一週擼一個 GitHub 微信小程式Github微信小程式
- Python GUI 教程 25行程式碼寫一個小鬧鐘PythonGUI行程
- 第一個Python程式Python
- 程式設計師如何在當今就業市場中讓自己脫穎而出程式設計師就業
- 精選了20個Python實戰專案(附原始碼),拿走就用!Python原始碼
- GoCN每日新聞(2017-10-24)Go
- GoCN每日新聞(2017-10-20)Go
- GoCN每日新聞(2017-10-22)Go
- GoCN每日新聞(2017-10-19)Go
- GoCN每日新聞(2017-10-21)Go
- GoCN每日新聞(2017-10-18)Go
- GoCN每日新聞(2017-07-06)Go
- GoCN每日新聞(2017-07-02)Go
- GoCN每日新聞(2017-06-29)Go
- GoCN每日新聞(2017-07-11)Go
- GoCN每日新聞(2017-07-07)Go
- GoCN每日新聞(2017-07-08)Go
- GoCN每日新聞(2017-11-04)Go
- GoCN每日新聞(2017-11-03)Go
- GoCN每日新聞(2017-10-25)Go
- GoCN每日新聞(2017-10-29)Go