requests.get 下載html網(wǎng)頁(yè)
bs4.BeautifulSoup 分析html內(nèi)容
from requests import getfrom bs4 import BeautifulSoup as bsfrom datetime import datetime as dtdef 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 = "http://news.***.com.cn/" if style==1: url1 += "world" elif style==2: url1 += "china" else: url1="https://mil.news.sina.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)]
>>> for i,news in enumerate(SinaNews(1)):
?? ?print(f"No{i+1}:",news[0])?? ?
No1: 外媒:*****
No2: 日媒:******
.............
內(nèi)容已馬賽克?。?!
>>>?
首次做爬蟲(chóng),為了方便下手找一個(gè)不用破解網(wǎng)頁(yè)的某新聞網(wǎng)站,下載網(wǎng)頁(yè)就能直接取得內(nèi)容。其中的國(guó)際、國(guó)內(nèi)和軍事新聞三個(gè)網(wǎng)頁(yè)作內(nèi)容源,requests.get下載網(wǎng)頁(yè)后,分析所得html文本,所有標(biāo)記帶日期剛好所需要的。
然后再根據(jù)url下載正文網(wǎng)頁(yè),分析可知id=‘a(chǎn)rticle’的
>>> def NewsDownload(url): 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("點(diǎn)擊進(jìn)入專題:","相關(guān)專題:") text = text.replace(" ","/n ") while "/n/n/n" in text: text = text.replace("/n/n/n","/n/n") return text>>> url = "https://******/w/2021-09-29/doc-iktzqtyt8811588.shtml">>> NewsDownload(url)"原標(biāo)題:******************************************************">>>
使用內(nèi)置的圖形界面庫(kù) tkinter 控件 Text 、Listbox、Scrollbar、Button。設(shè)置基本屬性、放置位置、綁定命令,然后調(diào)試到程序完工!
源代碼 News.pyw :其中涉及的網(wǎng)站名稱已馬賽克!
from requests import getfrom bs4 import BeautifulSoup as bsfrom datetime import datetime as dtfrom os import pathimport tkinter as tkdef 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 = "http://news.****.com.cn/" if style==1: url1 += "world" elif style==2: url1 += "china" else: url1="https://mil.****.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("點(diǎn)擊進(jìn)入專題:","相關(guān)專題:") 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 #按鈕的坐標(biāo) 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 = ("今日新聞","國(guó)際新聞","國(guó)內(nèi)新聞","軍事新聞") news = SinaNews() InitWindow(win,480,640)
奉上全部代碼,在此就不作詳細(xì)分析了,如有需要請(qǐng)留言討論。我的使用環(huán)境 Win7+Python3.8.8 下可以無(wú)錯(cuò)運(yùn)行!文中涉及網(wǎng)站名稱已打上馬賽克,猜不出名字的可以私下里問(wèn)我。
使用pyinstaller.exe編譯成單個(gè)運(yùn)行文件,注意源碼文件的后綴名應(yīng)該用.pyw否則會(huì)有cmd黑窗口出現(xiàn)。還有一個(gè)小知識(shí)點(diǎn),任意網(wǎng)站的Logo圖標(biāo)icon文件,一般都能在根目錄里下載到,即:
http(s)://websiteurl.com(.cn)/favicon.ico
編譯命令如下:
D:/>pyinstaller --onefile --nowindowed --icon="D:/favicon.ico" News.pyw
編譯完成后,在dist文件夾下生成一個(gè)News.exe可執(zhí)行文件,大小約15M還能接受。?
反正拿走就能直接用,臨走前給個(gè)一鍵三連吧,謝謝!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/121684.html
摘要:的分類的六要素的生命周期。第二階段測(cè)試工具自學(xué)時(shí)會(huì)用即可,不必精通需求分析工具用例編寫(xiě)相關(guān)函數(shù)統(tǒng)計(jì)數(shù)據(jù)整合條件判定數(shù)據(jù)有效性等性能測(cè)試工具。語(yǔ)言數(shù)據(jù)庫(kù)都是必須的,當(dāng)然測(cè)試工具也是要會(huì)的。 ?軟實(shí)力? ● 關(guān)于剛?cè)肼殨r(shí) ●?關(guān)于對(duì)待問(wèn)題 ●?關(guān)于執(zhí)行力 ●?關(guān)于個(gè)性 ●?關(guān)于下...
摘要:適用人群爬蟲(chóng)方向數(shù)據(jù)分析方向非程序員加薪四開(kāi)發(fā)前后端開(kāi)發(fā)是程序員職業(yè)中的熱門(mén),目前來(lái)講,人才缺口依然很大。寄語(yǔ)上面就是所有方向的學(xué)習(xí)路線了,把你感興趣的方向掌握了之后,你去找工作不是什么問(wèn)題的。 ...
摘要:前言大家好,我是安果之前寫(xiě)過(guò)一篇文章,文中提出了一種方案,可以實(shí)現(xiàn)每天自動(dòng)給微信群群發(fā)新聞早報(bào)如何利用爬蟲(chóng)實(shí)現(xiàn)給微信群發(fā)新聞早報(bào)詳細(xì)但是對(duì)于很多人來(lái)說(shuō),首先編寫(xiě)一款需要一定的移動(dòng)端開(kāi)發(fā)經(jīng)驗(yàn),其次還需要另外編寫(xiě)無(wú)障礙服務(wù)應(yīng)用,如此顯得有一定難1. 前言大家好,我是安果!之前寫(xiě)過(guò)一篇文章,文中提出了一種方案,可以實(shí)現(xiàn)每天自動(dòng)給微信群群發(fā)新聞早報(bào)如何利用 Python 爬蟲(chóng)實(shí)現(xiàn)給微信群發(fā)新聞早報(bào)?...
摘要:而且我們一直在講的,也可以用中文來(lái)編程。帶來(lái)的一個(gè)額外功能就是,你可以使用中文作為變量名。另外如果在代碼里寫(xiě)中文,別忘了在開(kāi)頭加上或的聲明。 現(xiàn)代計(jì)算機(jī)和編程的起源和推動(dòng)力量主要源自美國(guó),再加上26個(gè)字母很便于表示(算上大小寫(xiě),6位bit就夠了),因此英語(yǔ)一直是編程領(lǐng)域的不二之選。但這就給部分非英語(yǔ)國(guó)家的編程學(xué)習(xí)者帶來(lái)一些困擾。以至于有些人還沒(méi)開(kāi)始學(xué),就擔(dān)心自己的英語(yǔ)問(wèn)題。這完全沒(méi)必要...
閱讀 1542·2021-11-17 09:33
閱讀 1292·2021-10-11 10:59
閱讀 2926·2021-09-30 09:48
閱讀 1932·2021-09-30 09:47
閱讀 3061·2019-08-30 15:55
閱讀 2362·2019-08-30 15:54
閱讀 1515·2019-08-29 15:25
閱讀 1672·2019-08-29 10:57