我正在尝试学习如何进行网页抓取,所以我决定尝试从我玩的手机游戏Stats Royale中抓取。我按照一些简单的步骤来自动化无聊的东西,但是我想用一些更难的代码来挑战自己。但是,运行代码后,它指出“ return trophy [0] .text.strip()”处存在索引错误。
from selenium import webdriver
import bs4
def Trophycount():
print('What is the player\'s tag number?')
tag = input()
elems.send_keys(tag)
elems.submit()
soup = bs4.BeautifulSoup('html.parser')
trophy = soup.select('#profile-metrics > div >
div.statistic__metricGroupMain > div:nth-child(2) >
div.statistics__metricCounter.ui__headerExtraSmall')
print('The player\'s highest trophy count is:')
return trophy[0].text.strip()
browser = webdriver.Chrome()
html = browser.get('https://statsroyale.com')
elems = browser.find_element_by_css_selector('#search-bar > input')
elems.click()
Trophycount()