【发布时间】:2019-09-19 09:22:17
【问题描述】:
我目前的问题是确定如何使用 BeautifulSoup 和 Selenium 来抓取一个名为 Rocket League eSports 的电子竞技网站。
我能够找到数据并使用 Selenium,因为他们网页上使用了脚本。然后我使用 BeautifulSoup 来获取数据。从这里我可以导出所有团队名称,但是当我添加它时,我的列表中一直显示“无”。
from selenium import webdriver
from bs4 import BeautifulSoup
#import soupsieve
import time
#create a Google Chrome session
browser =
webdriver.Chrome(executable_path='/home/jdr1018/chromedriver')
#maximizes Google Chrome window
browser.maximize_window()
#fetches the URL
browser.get('https://www.rocketleagueesports.com/stats/')
#pause to allow page to load
time.sleep(4)
#search the container and find all elements with h5 tag to print
given elements
#container = browser.find_elements_by_tag_name('h5')
#hand over Selenium page source to Beautiful BeautifulSoup
soup_source = BeautifulSoup(browser.page_source, 'lxml')
namelist = [] #empty list for Team names
winpercentlist = [] #empty list for Win Percentage
rocketleaguedict = {} #empty dict for namelist + winpercentlist
#using XPath to find h5 element with class name and assinging it to
teamnames
elements = browser.find_elements_by_xpath('//h5[@class="name"]/a')
teamnames = [element.text for element in elements]
#loop through team names to get each individual team name
for name in teamnames:
#if statement to determine if name is already in the list
if name in namelist:
#append each team name through loop into empty list.
pass
else:
namelist.append(name)
#return namelist to verify
return namelist
#for i in container:
#print(i.get_attribute("innerHTML"))
#once program is done close Google Chrome
browser.close()}
我的输出类似于这样:
['CHIEFS ESPORTS CLUB']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS', 'ICON ESPORTS']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS', 'ICON ESPORTS', 'RENAULT SPORT
TEAM VITALITY']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS', 'ICON ESPORTS', 'RENAULT SPORT
TEAM VITALITY', 'ERODIUM']
['CHIEFS ESPORTS CLUB', 'NRG ESPORTS', 'ICON ESPORTS', 'RENAULT SPORT
TEAM VITALITY', 'ERODIUM', 'LOWKEY ESPORTS'] ...
这不完全是,但关键是它们是一堆“无”,我无法弄清楚为什么。
【问题讨论】:
标签: python selenium selenium-webdriver web-scraping beautifulsoup