本次爬取網(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_encoding
return 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)站界面信息




2、爬取結(jié)果


五、完整代碼
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_encoding
return 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語(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è)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- Python爬蟲(chóng)獲取op.gg英雄聯(lián)盟英雄對(duì)位勝率的源碼
- Python3爬蟲(chóng)爬取英雄聯(lián)盟高清桌面壁紙功能示例【基于Scrapy框架】
- python 爬取英雄聯(lián)盟皮膚并下載的示例
- Python3爬取英雄聯(lián)盟英雄皮膚大圖實(shí)例代碼
- 用Python爬取LOL所有的英雄信息以及英雄皮膚的示例代碼
- python 爬取英雄聯(lián)盟皮膚圖片