溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

結(jié)合Python網(wǎng)絡(luò)爬蟲做一個(gè)今日新聞小程序的方法教程

發(fā)布時(shí)間:2021-09-30 13:37:54 來源:億速云 閱讀:123 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“結(jié)合Python網(wǎng)絡(luò)爬蟲做一個(gè)今日新聞小程序的方法教程”,在日常操作中,相信很多人在結(jié)合Python網(wǎng)絡(luò)爬蟲做一個(gè)今日新聞小程序的方法教程問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”結(jié)合Python網(wǎng)絡(luò)爬蟲做一個(gè)今日新聞小程序的方法教程”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

核心代碼

requests.get 下載html網(wǎng)頁
bs4.BeautifulSoup 分析html內(nèi)容

from requests import get
from bs4 import BeautifulSoup as bs
from datetime import datetime as dt
 
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 = '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)]

爬取標(biāo)題

>>> for i,news in enumerate(SinaNews(1)):
print(f'No{i+1}:',news[0])


No1: 外媒:*****
No2: 日媒:******
......

.......

內(nèi)容已馬賽克?。?!
>>>

首次做爬蟲,為了方便下手找一個(gè)不用破解網(wǎng)頁的某新聞網(wǎng)站,下載網(wǎng)頁就能直接取得內(nèi)容。其中的國際、國內(nèi)和軍事新聞三個(gè)網(wǎng)頁作內(nèi)容源,requests.get下載網(wǎng)頁后,分析所得html文本,所有<a href=...>標(biāo)記帶日期剛好所需要的。

爬取正文

然后再根據(jù)url下載正文網(wǎng)頁,分析可知id=‘a(chǎn)rticle'的<div>層就是正文所在位置,.get_text()是取得文本的關(guān)鍵函數(shù),然后適當(dāng)做一些格式處理:

>>> 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)置的圖形界面庫 tkinter 控件 Text 、Listbox、Scrollbar、Button。設(shè)置基本屬性、放置位置、綁定命令,然后調(diào)試到程序完工!

源代碼 News.pyw :其中涉及的網(wǎng)站名稱已馬賽克!

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 = '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("<Double-Button-1>",NewsShow)
 
if __name__=='__main__':
 
    win = tk.Tk()
    bTitle = ('今日新聞','國際新聞','國內(nèi)新聞','軍事新聞')
    news = SinaNews()
    InitWindow(win,480,640)

奉上全部代碼,在此就不作詳細(xì)分析了,如有需要請留言討論。我的使用環(huán)境 Win7+Python3.8.8 下可以無錯(cuò)運(yùn)行!文中涉及網(wǎng)站名稱已打上馬賽克,猜不出名字的可以私下里問我。

軟件編譯

使用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還能接受。

反正拿走就能直接用,臨走前記得收藏,謝謝!

到此,關(guān)于“結(jié)合Python網(wǎng)絡(luò)爬蟲做一個(gè)今日新聞小程序的方法教程”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI