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)對網(wǎng)頁源代碼進(jìn)行分析(右鍵鼠標(biāo)在菜單中即可找到查看網(wǎng)頁源代碼)。通過查找“53.84%”快速定位Garen所在位置
由代碼可看出,英雄名、勝率及選取率都在td標(biāo)簽中,而每一個(gè)英雄信息在一個(gè)tr標(biāo)簽中,td父標(biāo)簽為tr標(biāo)簽,tr父標(biāo)簽為tbody標(biāo)簽。
對tbody標(biāo)簽進(jìn)行查找
代碼中共有5個(gè)tbody標(biāo)簽(tbody標(biāo)簽開頭結(jié)尾均有”tbody”,故共有10個(gè)”tbody”),對字段內(nèi)容分析,分別為上單、打野、中單、ADC、輔助信息
以上單這部分英雄為例,我們需要首先找到tbody標(biāo)簽,然后從中找到tr標(biāo)簽(每一條tr標(biāo)簽就是一個(gè)英雄的信息),再從子標(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) #將英雄信息寫入hlist列表 printHeroInformation(hlist) #輸出信息四、結(jié)果演示1、網(wǎng)站界面信息
import requests #導(dǎo)入requests庫import bs4 #導(dǎo)入bs4庫from bs4 import BeautifulSoup #導(dǎo)入BeautifulSoup庫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) #將英雄信息寫入hlist列表 printHeroInformation(hlist) #輸出信息main()
如果需要爬取打野、中單、ADC或者輔助信息,只需要修改
fillHeroInformation(hlist,html)
函數(shù)中的
for tr in soup.find(name = 'tbody',attrs = 'tabItem champion-trend-tier-TOP').children語句
將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)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. JavaWeb Servlet中url-pattern的使用2. jsp中sitemesh修改tagRule技術(shù)分享3. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼4. React優(yōu)雅的封裝SvgIcon組件示例5. 輕松學(xué)習(xí)XML教程6. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除9. ASP基礎(chǔ)知識VBScript基本元素講解10. 詳解瀏覽器的緩存機(jī)制
