Python爬取OPGG上英雄聯(lián)盟英雄勝率及選取率信息的操作
本次爬取網(wǎng)站為opgg,網(wǎng)址為:” http://www.op.gg/champion/statistics”
由網(wǎng)站界面可以看出,右側(cè)有英雄的詳細(xì)信息,以Garen為例,勝率為53.84%,選取率為16.99%,常用位置為上單
現(xiàn)對(duì)網(wǎng)頁(yè)源代碼進(jìn)行分析(右鍵鼠標(biāo)在菜單中即可找到查看網(wǎng)頁(yè)源代碼)。通過(guò)查找“53.84%”快速定位Garen所在位置
由代碼可看出,英雄名、勝率及選取率都在td標(biāo)簽中,而每一個(gè)英雄信息在一個(gè)tr標(biāo)簽中,td父標(biāo)簽為tr標(biāo)簽,tr父標(biāo)簽為tbody標(biāo)簽。
對(duì)tbody標(biāo)簽進(jìn)行查找
代碼中共有5個(gè)tbody標(biāo)簽(tbody標(biāo)簽開(kāi)頭結(jié)尾均有”tbody”,故共有10個(gè)”tbody”),對(duì)字段內(nèi)容分析,分別為上單、打野、中單、ADC、輔助信息
以上單這部分英雄為例,我們需要首先找到tbody標(biāo)簽,然后從中找到tr標(biāo)簽(每一條tr標(biāo)簽就是一個(gè)英雄的信息),再?gòu)淖訕?biāo)簽td標(biāo)簽中獲取英雄的詳細(xì)信息
二、爬取步驟爬取網(wǎng)站內(nèi)容->提取所需信息->輸出英雄數(shù)據(jù)
getHTMLText(url)->fillHeroInformation(hlist,html)->printHeroInformation(hlist)
getHTMLText(url)函數(shù)是返回url鏈接中的html內(nèi)容
fillHeroInformation(hlist,html)函數(shù)是將html中所需信息提取出存入hlist列表中
printHeroInformation(hlist)函數(shù)是輸出hlist列表中的英雄信息
三、代碼實(shí)現(xiàn)1、getHTMLText(url)函數(shù)def getHTMLText(url): #返回html文檔信息 try:r = requests.get(url,timeout = 30)r.raise_for_status()r.encoding = r.apparent_encodingreturn r.text #返回html內(nèi)容 except:return ''2、fillHeroInformation(hlist,html)函數(shù)
以一個(gè)tr標(biāo)簽為例,tr標(biāo)簽內(nèi)有7個(gè)td標(biāo)簽,第4個(gè)td標(biāo)簽內(nèi)屬性值為'champion-index-table__name'的div標(biāo)簽內(nèi)容為英雄名,第5個(gè)td標(biāo)簽內(nèi)容為勝率,第6個(gè)td標(biāo)簽內(nèi)容為選取率,將這些信息存入hlist列表中
def fillHeroInformation(hlist,html): #將英雄信息存入hlist列表 soup = BeautifulSoup(html,'html.parser') for tr in soup.find(name = 'tbody',attrs = 'tabItem champion-trend-tier-TOP').children: #遍歷上單tbody標(biāo)簽的兒子標(biāo)簽if isinstance(tr,bs4.element.Tag): #判斷tr是否為標(biāo)簽類型,去除空行 tds = tr(’td’) #查找tr標(biāo)簽下的td標(biāo)簽 heroName = tds[3].find(attrs = 'champion-index-table__name').string #英雄名 winRate = tds[4].string #勝率 pickRate = tds[5].string #選取率 hlist.append([heroName,winRate,pickRate]) #將英雄信息添加到hlist列表中3、printHeroInformation(hlist)函數(shù)
def printHeroInformation(hlist): #輸出hlist列表信息 print('{:^20}t{:^20}t{:^20}t{:^20}'.format('英雄名','勝率','選取率','位置')) for i in range(len(hlist)): i = hlist[i] print('{:^20}t{:^20}t{:^20}t{:^20}'.format(i[0],i[1],i[2],'上單'))4、main()函數(shù)
網(wǎng)站地址賦值給url,新建一個(gè)hlist列表,調(diào)用getHTMLText(url)函數(shù)獲得html文檔信息,使用fillHeroInformation(hlist,html)函數(shù)將英雄信息存入hlist列表,再使用printHeroInformation(hlist)函數(shù)輸出信息
def main(): url = 'http://www.op.gg/champion/statistics' hlist = [] html = getHTMLText(url) #獲得html文檔信息 fillHeroInformation(hlist,html) #將英雄信息寫(xiě)入hlist列表 printHeroInformation(hlist) #輸出信息四、結(jié)果演示1、網(wǎng)站界面信息
import requests #導(dǎo)入requests庫(kù)import bs4 #導(dǎo)入bs4庫(kù)from bs4 import BeautifulSoup #導(dǎo)入BeautifulSoup庫(kù)def getHTMLText(url): #返回html文檔信息 try:r = requests.get(url,timeout = 30)r.raise_for_status()r.encoding = r.apparent_encodingreturn r.text #返回html內(nèi)容 except:return ''def fillHeroInformation(hlist,html): #將英雄信息存入hlist列表 soup = BeautifulSoup(html,'html.parser') for tr in soup.find(name = 'tbody',attrs = 'tabItem champion-trend-tier-TOP').children: #遍歷上單tbody標(biāo)簽的兒子標(biāo)簽if isinstance(tr,bs4.element.Tag): #判斷tr是否為標(biāo)簽類型,去除空行 tds = tr(’td’) #查找tr標(biāo)簽下的td標(biāo)簽 heroName = tds[3].find(attrs = 'champion-index-table__name').string #英雄名 winRate = tds[4].string #勝率 pickRate = tds[5].string #選取率 hlist.append([heroName,winRate,pickRate]) #將英雄信息添加到hlist列表中def printHeroInformation(hlist): #輸出hlist列表信息 print('{:^20}t{:^20}t{:^20}t{:^20}'.format('英雄名','勝率','選取率','位置')) for i in range(len(hlist)):i = hlist[i]print('{:^20}t{:^20}t{:^20}t{:^20}'.format(i[0],i[1],i[2],'上單'))def main(): url = 'http://www.op.gg/champion/statistics' hlist = [] html = getHTMLText(url) #獲得html文檔信息 fillHeroInformation(hlist,html) #將英雄信息寫(xiě)入hlist列表 printHeroInformation(hlist) #輸出信息main()
如果需要爬取打野、中單、ADC或者輔助信息,只需要修改
fillHeroInformation(hlist,html)
函數(shù)中的
for tr in soup.find(name = 'tbody',attrs = 'tabItem champion-trend-tier-TOP').children語(yǔ)句
將attrs屬性值修改為
'tabItem champion-trend-tier-JUNGLE'
'tabItem champion-trend-tier-MID'
'tabItem champion-trend-tier-ADC'
'tabItem champion-trend-tier-SUPPORT'
等即可!
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. Java之JSP教程九大內(nèi)置對(duì)象詳解(中篇)2. 基于python計(jì)算滾動(dòng)方差(標(biāo)準(zhǔn)差)talib和pd.rolling函數(shù)差異詳解3. 使用css實(shí)現(xiàn)全兼容tooltip提示框4. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果5. 詳解CSS不定寬溢出文本適配滾動(dòng)6. IDEA項(xiàng)目的依賴(pom.xml文件)導(dǎo)入問(wèn)題及解決7. CSS自定義滾動(dòng)條樣式案例詳解8. JS繪圖Flot如何實(shí)現(xiàn)動(dòng)態(tài)可刷新曲線圖9. 詳解Python中openpyxl模塊基本用法10. springboot基于Redis發(fā)布訂閱集群下WebSocket的解決方案
