国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

瀏覽:17日期:2022-06-16 10:56:48
制作NBA數(shù)據(jù)爬蟲捋順思路

我們在這里選擇的是百度體育帶來的數(shù)據(jù),我們在百度當中直接搜索NBA跳轉(zhuǎn)到網(wǎng)頁,我們可以看到,百度已經(jīng)為我們提供了相關的數(shù)據(jù)

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

我們點擊進去后,可以發(fā)現(xiàn)這是一個非常簡潔的網(wǎng)址

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

我們看一下這個地址欄,發(fā)現(xiàn)毫無規(guī)律https://tiyu.baidu.com/live/detail/576O5Zu955S35a2Q6IGM5Lia56%2Bu55CD6IGU6LWbI2Jhc2tldGJhbGwjMjAyMS0wNi0xMyPniLXlo6t2c%2BWspritq%2BiIuQ%3D%3D/from/baidu_aladdin

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

好吧,只能再找找了,我們點擊整個標簽發(fā)現(xiàn),這是一個網(wǎng)址,那就容易多了。

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

這里我們想要獲取的無非就是具體的每一節(jié)數(shù)據(jù)和總分,然后如果用戶還有其他需求的話我們就直接將其推送到百度網(wǎng)址上面來

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

爬取的思路大概就是這樣,首先先訪問主頁面,然后在訪問旗下今天的比賽,最后將比賽結果返回

編寫代碼

首先我們使用REQUESTS來訪問網(wǎng)址

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

我們可以看到,百度沒有做任何限制,直接訪問也可以獲得內(nèi)容

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

接下來我們使用解析庫進行解析

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

首先我們先將程序定位到Main標簽

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

代碼則是這樣的,運行代碼我們會發(fā)現(xiàn),整個代碼縮進了不少

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

獲取主要的頁面,我們使用FIND函數(shù)進行進一步操作

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

我們成功定位到了這個主頁面,接下來就是我們開始爬取最近幾次的比賽信息和詳細頁面了

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

更改代碼,我們直接獲取所有的比賽信息

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

在測試網(wǎng)站的時候,我發(fā)現(xiàn)百度竟然使用了AJAX技術,就是說你一次性獲得的網(wǎng)站源代碼可能只有五條,然后要進行再一次加載才能獲取接下來的數(shù)據(jù)。但是這也對我們程序來說挺好的,我們本來也不需要那么多數(shù)據(jù)。

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

我們在這里查找了每一個的日期,查找對象為 date,接下來我們把其轉(zhuǎn)換成字符串,因為百度上面這個日期有縮進,所以我們在后面添加 STRIP() 取消字符串前面的空格。按照這樣的方式獲取比賽地址

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

在這里,我們使用拼接字符串的方法,完成了對最后地址的解析

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

# 程序名稱 : NBAReporter# 制作時間 : 2021年6月13日# 運行環(huán)境 : Windows 10import requestsfrom bs4 import BeautifulSoup# 基礎數(shù)據(jù)定義baidu_nba_url = 'https://tiyu.baidu.com/match/NBA/'request_url = 'https:'nba_dict = {}# 訪問網(wǎng)址nba_res = requests.get(baidu_nba_url)# print(nba_res.text)# 開始使用解析器nba_soup = BeautifulSoup(nba_res.text, 'html.parser')nba_main = nba_soup.main# print(nba_main)nba_div = nba_main.find_all('div', class_ = 'wa-match-schedule-list-wrapper')for i in nba_div:# 獲取比賽時間nba_time = i.find('div', class_ = 'date').string.strip()print(nba_time)# 獲取比賽的次數(shù)nba_times = i.find('div', class_ = 'list-num c-color').stringprint(nba_times)# 獲取詳細的比賽地址nba_href = i.find_all('div', class_ = 'wa-match-schedule-list-item c-line-bottom')for url_nba in nba_href:url_nba = url_nba.aurl_href = url_nba['href']real_url = request_url + url_hrefprint(real_url)

接下來我們要開始剩余部分的解析,我們可以看到我們還有一部分的詳細信息沒有爬取,所以我們開始爬取詳細信息

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

按照邏輯繼續(xù)編寫代碼

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

然后我們獲取一下這里面的值

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

獲取比賽的相關分數(shù)后,我們創(chuàng)建兩個列表,一個列表定義我們等一下需要用到NBA的樣式,另一個列表則存儲今天的日期,最后返回

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

我們已經(jīng)在這里吧這個方法封裝了,所以我們創(chuàng)建一個新的文件,直接導入即可

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

NBAReporter.py

# 程序名稱 : NBAReporter# 制作時間 : 2021年6月13日# 運行環(huán)境 : Windows 10import requestsfrom bs4 import BeautifulSoupdef NBAReporter():# 基礎數(shù)據(jù)定義baidu_nba_url = 'https://tiyu.baidu.com/match/NBA/'request_url = 'https:'nba_list = []today_list = []# 訪問網(wǎng)址nba_res = requests.get(baidu_nba_url)# print(nba_res.text)# 開始使用解析器nba_soup = BeautifulSoup(nba_res.text, 'html.parser')nba_main = nba_soup.main# print(nba_main)nba_div = nba_main.find_all('div', class_ = 'wa-match-schedule-list-wrapper')for i in nba_div:# 獲取比賽時間today = i.find('div', class_ = 'date').string.strip()# 獲取比賽的次數(shù)nba_times = i.find('div', class_ = 'list-num c-color').string# 獲取詳細的比賽地址nba_href = i.find_all('div', class_ = 'wa-match-schedule-list-item c-line-bottom')for url_nba in nba_href:url_nba = url_nba.aurl_href = url_nba['href']real_url = request_url + url_href# print(real_url) # 獲取詳細數(shù)據(jù)vs_time = url_nba.find('div', class_ = 'font-14 c-gap-bottom-small').stringvs_finals = url_nba.find('div',class_ = 'font-12 c-color-gray').stringteam_row_1 = url_nba.find('div', class_ = 'team-row')team_row_2 = url_nba.find('div', class_ = 'c-gap-top-small team-row')'''team_row_1_png = team_row_1.find('div', class_ = 'inline-block')['style']team_row_2_png = team_row_2.find('div', class_ = 'inline-block')['style']print(team_row_1_png,team_row_2_png)'''team_row_1_name = team_row_1.find('span', class_ = 'inline-block team-name team-name-360 team-name-320 c-line-clamp1').stringteam_row_2_name = team_row_2.find('span', class_ = 'inline-block team-name team-name-360 team-name-320').string# print(team_row_1_name,team_row_2_name)team_row_1_score = team_row_1.find('span', class_ = 'inline-block team-score-num c-line-clamp1').stringteam_row_2_score = team_row_2.find('span', class_ = 'inline-block team-score-num c-line-clamp1').string# print(team_row_1_score,team_row_2_score)'''import re # 導入re庫,不過最好還是在最前面導入,這里是為了演示的需要team_row_1_png_url = re.search(r’background:url(.*)’, team_row_1_png)team_row_1_png_url = team_row_1_png_url.group(1)team_row_2_png_url = re.search(r’background:url(.*)’, team_row_2_png)team_row_2_png_url = team_row_2_png_url.group(1)'''nba = [ today, nba_times,'','',vs_time, vs_finals, team_row_1_name, team_row_2_name,'','', team_row_1_score, team_row_2_score]nba_list.append(nba)today_list.append(today)return nba_list,today_list

這里我們要編寫的是GUI界面的實現(xiàn)程序

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

首先先導入我們運行所需要的庫

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

簡單定義一下我們的代碼,設置標題和其他的一些窗口屬性# self.setWindowOpacity(0.5)這里是設置窗口透明程度的一行代碼,但是經(jīng)過我的測驗之后,發(fā)現(xiàn)這樣子真的對于用戶體驗一點也不好,所以在這里我把它注釋掉了

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

程序主邏輯如上圖所示,我們創(chuàng)建了一個單元布局,然后又創(chuàng)建了和比賽一樣的若干個標簽,最后將函數(shù)返回的列表以標簽的形式放在主窗口上面

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

最后創(chuàng)建事件,運行程序,這樣子整個程序就完成了

NBAWindow.py

# 程序名稱 : NBAWindow# 制作時間 : 2021年6月14日# 運行環(huán)境 : Windows 10import sysfrom PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *from NBAReporter import *# 首先創(chuàng)建一個主窗口class NBAWindow(QTabWidget):def __init__(self):super().__init__()self.make_Ui()'''# 兩分鐘自動刷新self.timer = QTimer()self.timer.setInterval(500)self.timer.timeout.connect(self.make_Ui)self.timer.start()'''self.setWindowTitle('NBA數(shù)據(jù)')self.setGeometry(1440,0,480,300)self.setFixedSize(480,300)self.setWindowIcon(QIcon(’images/nba.png’))self.setStyleSheet('''background-color:red; ''')# self.setWindowOpacity(0.5)self.setWindowFlags(Qt.WindowStaysOnTopHint|Qt.WindowMinimizeButtonHint|Qt.FramelessWindowHint)def make_Ui(self):self.nba,today = NBAReporter()self.tab = 0for a in self.nba:# 設置網(wǎng)格單元布局grid = QGridLayout()self.setLayout(grid)# 開始添加一個標簽tab = QWidget()# 將這個標簽設置為TAB并按照列表中的數(shù)值命名self.addTab(tab,today[self.tab])# 獲取循環(huán)之后的位置,重寫列表positions = [(i, j) for i in range(3) for j in range(4)]nba_list = self.nba[self.tab]# 開始創(chuàng)建Tab下面的標簽for position, nba in zip(positions, nba_list):#print(nba)# 當時空值時,跳過執(zhí)行if nba == '':continue# 設置文字樣式label = QLabel('<font color=’black’, size=5><b>%s</b></font>'%nba)grid.addWidget(label, *position)# 設置整個窗口為表格布局tab.setLayout(grid)# grid.update()# 將數(shù)值加一self.tab += 1if __name__ == ’__main__’:app = QApplication(sys.argv)window = NBAWindow()window.show()app.exec_()NBAReporter.py

# 程序名稱 : NBAReporter# 制作時間 : 2021年6月13日# 運行環(huán)境 : Windows 10import requestsfrom bs4 import BeautifulSoupdef NBAReporter():# 基礎數(shù)據(jù)定義baidu_nba_url = 'https://tiyu.baidu.com/match/NBA/'request_url = 'https:'nba_list = []today_list = []# 訪問網(wǎng)址nba_res = requests.get(baidu_nba_url)# print(nba_res.text)# 開始使用解析器nba_soup = BeautifulSoup(nba_res.text, 'html.parser')nba_main = nba_soup.main# print(nba_main)nba_div = nba_main.find_all('div', class_ = 'wa-match-schedule-list-wrapper')for i in nba_div:# 獲取比賽時間today = i.find('div', class_ = 'date').string.strip()# 獲取比賽的次數(shù)nba_times = i.find('div', class_ = 'list-num c-color').string# 獲取詳細的比賽地址nba_href = i.find_all('div', class_ = 'wa-match-schedule-list-item c-line-bottom')for url_nba in nba_href:url_nba = url_nba.aurl_href = url_nba['href']real_url = request_url + url_href# print(real_url) # 獲取詳細數(shù)據(jù)vs_time = url_nba.find('div', class_ = 'font-14 c-gap-bottom-small').stringvs_finals = url_nba.find('div',class_ = 'font-12 c-color-gray').stringteam_row_1 = url_nba.find('div', class_ = 'team-row')team_row_2 = url_nba.find('div', class_ = 'c-gap-top-small team-row')'''team_row_1_png = team_row_1.find('div', class_ = 'inline-block')['style']team_row_2_png = team_row_2.find('div', class_ = 'inline-block')['style']print(team_row_1_png,team_row_2_png)'''team_row_1_name = team_row_1.find('span', class_ = 'inline-block team-name team-name-360 team-name-320 c-line-clamp1').stringteam_row_2_name = team_row_2.find('span', class_ = 'inline-block team-name team-name-360 team-name-320').string# print(team_row_1_name,team_row_2_name)team_row_1_score = team_row_1.find('span', class_ = 'inline-block team-score-num c-line-clamp1').stringteam_row_2_score = team_row_2.find('span', class_ = 'inline-block team-score-num c-line-clamp1').string# print(team_row_1_score,team_row_2_score)'''import re # 導入re庫,不過最好還是在最前面導入,這里是為了演示的需要team_row_1_png_url = re.search(r’background:url(.*)’, team_row_1_png)team_row_1_png_url = team_row_1_png_url.group(1)team_row_2_png_url = re.search(r’background:url(.*)’, team_row_2_png)team_row_2_png_url = team_row_2_png_url.group(1)'''nba = [ today, nba_times,'','',vs_time, vs_finals, team_row_1_name, team_row_2_name,'','', team_row_1_score, team_row_2_score]nba_list.append(nba)today_list.append(today)return nba_list,today_list效果演示

Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序

到此這篇關于Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序的文章就介紹到這了,更多相關Python PyQt5數(shù)據(jù)播報程序內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产精品久久一区 | 72种姿势欧美久久久久大黄蕉 | 天堂8中文在线最新版在线 天堂8资源8在线 | 91亚洲国产成人久久精品网站 | 欧美顶级毛片在线播放小说 | 97视频在线播放 | 欧美黄色一级视屏 | 欧美精品成人3d在线 | 国产一级视频久久 | 日韩专区亚洲国产精品 | 一级毛片成人午夜 | 成人看片免费 | 91影视永久福利免费观看 | 国产精品久久国产三级国电话系列 | 三级中文字幕永久在线视频 | 欧美a一级| 日韩字幕 | 毛片网站免费在线观看 | 日韩午夜三级 | 日韩三级观看 | 欧美亚洲免费久久久 | 亚洲一区二区天海翼 | 日日噜噜噜夜夜爽爽狠狠69 | 日本韩国台湾香港三级 | 国产亚洲人成网站在线观看不卡 | 日本美女高清在线观看免费 | 偷自拍第一页 | 亚洲三级小视频 | 91久久精品国产91久久性色也 | 日本护士一级毛片在线播放 | 中文字幕在线视频精品 | 精品欧美一区二区三区在线观看 | 亚洲综合区 | 成人精品亚洲 | 国产日韩欧美综合在线 | 99热精品在线免费观看 | 国内精品九一在线播放 | 国产综合成人亚洲区 | 国产精品一区亚洲一区天堂 | 久草在线观看首页 | 91九色精品国产免费 |