通過開發(fā)者工具找到對應部位源碼,發(fā)現(xiàn)數(shù)據(jù)就在源碼中,證明這是一個靜態(tài)數(shù)據(jù),確定使用BeautifulSoup庫。
import requests
from bs4 import BeautifulSoup
championname={'阿卡麗 ':'akali','牛頭':'alistar','阿木木':'amumu','冰鳥':'anivia','安妮':'annie','艾希':'ashe','機器人':'blitzcrank','火男':'brand','女警':'caitlyn',
'蛇女':'cassiopeia','大蟲子':'chogath','飛機':'corki','諾手':'darius','皎月':'diana','蒙多':'drmundo','德萊文':'delevin','蜘蛛':'elise',
'寡婦':'evelynn','ez':'ezreal','稻草人':'fiddlesticks','劍姬':'fiora','魚人':'fizz','加里奧':'galio','船長':'gangplank','蓋倫':'garen',
'酒桶':'gragas','人馬':'hecarim','大頭':'heimerdinger','刀妹':'irelia','鳳女':'janna','皇子':'jarvaniv','賈克斯':'jax','杰斯':'jayce','卡爾瑪':'karma',
'死歌':'karthus','卡薩丁':'kassadin','卡特':'katarina','天使':'kayle','凱南':'kennen','螳螂':'khazix','大嘴':'kogmaw','妖姬':'leblanc','盲僧':'leesin','女坦':'Leona','露露':'lulu','拉克絲':'Lux',
'石頭人':'Malphite','馬爾扎哈':'Malzahar','大樹':'Maokai','劍圣':'Yi','女槍':'MissFortune','猴子':'Monkeyking','鐵男':'Mordekaiser','莫甘娜':'Morgana'
,'娜美':'Nami','狗頭':'Nasus','泰坦':'Nautilus','豹女':'Nidalee','夢魘':'Nocturne','雪人':'Nunu','奧拉夫':'Olaf','發(fā)條':'Orianna','潘森':'Pantheon','波比':'Poopy','龍龜':'Rammus','鱷魚':'Renekton','獅子狗':'Rengar',
'瑞文':'Rivan','蘭博':'Rumble','瑞茲':'Ryze','豬女':'Sejuani','小丑':'Shaco','慎':'Shen','龍女':'Shyvana','煉金':'Singed','塞恩':'Sion','希維爾':'Sivir','蝎子':'Skarner','琴女':'Sona','奶媽':'Soraka','烏鴉':'Swain','辛德拉':'Syndra'
,'男刀':'Talon','寶石':'Taric','提莫':'Teemo','錘石':'Thresh','小炮':'Tristana','巨魔':'Trundle','蠻王':'Tryndamere','卡牌':'TwistedFate','老鼠':'Twitch','烏迪爾':'Udyr','厄加特':'Urgot','維魯斯':'Varus','薇恩':'Vayne',
'小法':'Veigar','蔚':'Vi','維克托':'Viktor','吸血鬼':'Vladimir','狗熊':'Volibear','狼人':'Warwick','澤拉斯':'Xerath','趙信':'XinZhao','掘墓':'Yorick','劫':'Zed','炸彈人':'Ziggs','時光':'Zilean','婕拉':'Zyra','佐伊':'zoe','永恩':'yone','薩米拉':'samira','亞索':'yasuo',
'塞拉斯':'sylas','盧錫安':'lucian','艾克':'ekko','阿貍':'ahri','瑟提':'sett','奇亞娜':'qiyana','龍王':'aurelionsol','克烈':'kled','妮蔻':'neeko'
}
position_all = {'top':'top','jun':'jungle','mid':'mid','ad':'bot','sup':'support'}
#由于網站反爬蟲機制,使用請求通來偽裝成瀏覽器,否則會被檢測為爬蟲,爬取數(shù)據(jù)失敗
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
name_input = input('輸入名字:')
myname = championname[name_input]
myposition = input('輸入位置:')
position = position_all[myposition]
print('正在查詢,請稍等~~~')
#向url發(fā)出請求,將請求頭傳入,返回結果保留在res中,res為response對象
res = requests.get('http://www.op.gg/champion/{}/statistics/{}/matchup'.format(myname,position),headers=headers)
#res.text是要解析的網頁源代碼,html。parser是python的解析器
soup = BeautifulSoup(res.text,'html.parser')
#find方法返回tag對象,find_all返回有tag對象組成的列表,tag是BeautifSoup中的對象
#查找class屬性為champion-matchup-champion-list__item的div標簽,組成名為items的列表
items = soup.find_all('div',class_='champion-matchup-champion-list__item')
print('英雄 勝率')
for i in items:
#div中的data-champion-name屬性值為英雄名字
name = i['data-champion-name']
#div屬性中的data-value-winrate屬性值為查找的英雄勝率,這里轉換為供選擇的英雄勝率
rate = 1-float(i['data-value-winrate'])
print(name,'{}%'.format(round(rate*100,2)))
到此這篇關于Python爬蟲獲取op.gg英雄聯(lián)盟英雄對位勝率的源碼的文章就介紹到這了,更多相關Python爬蟲英雄聯(lián)盟內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!