简体   繁体   English

如何使用Selenium在nba.stats.com上将页面从“ 1”更改为“全部”

[英]How to use Selenium to change the page from “1” to “All” on nba.stats.com

As of now my web scraper works to scrape all of the NBA player names off of the first page of the table (50 rows). 截至目前,我的网络抓取工具可从表格的第一页(50行)中抓取所有NBA球员姓名。 I am quite new to this stuff, and I am unsure how to navigate to the "1" and change the value to "All" for the pages to display. 我对这些东西还很陌生,我不确定如何导航到“ 1”并将值更改为“全部”以显示页面。 My end goal is to scrape all 488 entries at once, instead of only being able to scrape 50. Would I just have to create another driver.execute_script() as I did with the scroll? 我的最终目标是一次driver.execute_script()所有488个条目,而不是仅能够driver.execute_script() 50个条目。我是否需要像滚动一样创建另一个driver.execute_script() Thanks. 谢谢。

Here is my code as of now: 到目前为止,这是我的代码:

from selenium import webdriver
from bs4 import BeautifulSoup
import time
import os

driver = webdriver.Firefox(executable_path="/Users/myusername/Documents/geckodriverfolder/geckodriver")
driver.get('https://stats.nba.com/players/traditional/?PerMode=Totals&sort=NBA_FANTASY_PTS&dir=-1&Season=2017-18&SeasonType=Regular%20Season')
time.sleep(5)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
html = driver.page_source

soup = BeautifulSoup(html, 'lxml')

for test in soup.find_all('td', class_='player'):
player_name = test.a.text
print(player_name)

Here is what I came up with: 这是我想出的:

driver.find_element_by_xpath("//select/option[text()='All']").click()

I tried to use Select from Selenium, but there was no elementID for the select tag so I used the xpath route. 我尝试使用Select from Selenium,但是没有selectID的elementID,所以我使用了xpath路由。

I would be interested if someone was able to do this with Select without an elementID (if that is possible). 如果有人能够使用Select而没有elementID(如果可能),我将很感兴趣。

Hope this helps! 希望这可以帮助!

This is how it can work with Select : 这就是Select工作方式:

select = Select(driver.find_element_by_css_selector('.stats-table- pagination__select'))
select.select_by_visible_text("All")

or by index: 或按索引:

select.select_by_index(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM