繁体   English   中英

Python + Selenium:网页抓取

[英]Python + Selenium: web scraping

我正在尝试使用Selenium从网站中提取一些信息,以下是该网站的链接: http ://www.ultimatetennisstatistics.com/playerProfile?playerId=4742我要获取的信息是位于以下位置的玩家统计信息下拉按钮“ statistics”将您带到另一个页面,我已经检查了该按钮并获得了XPath和CSS,但是当我运行程序时,它不会打开播放器的统计信息页面,而是仅打开以下链接: http:// www。 ultimatetennisstatistics.com/playerProfile?playerId=4742

并给我一个错误:

NoSuchElementException: no such element: 

Unable to locate element: {"method":"css selector","selector":"#playerPills > li.dropdown.active.open > ul > li.active"}
  (Session info: chrome=67.0.3396.99)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.3.9600 x86_64)

下面是我的代码:

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.ultimatetennisstatistics.com/playerProfile?playerId=4742")
soup = BeautifulSoup(driver.page_source,"lxml")

bm = driver.find_element_by_css_selector('#playerPills > li.dropdown.active.open > ul > li.active')
bm.click()

有人可以告诉我们如何使用Selenium打开播放器的统计页面并提取表中的信息吗?

如果检查页面的html源,则可以直接访问要单击的按钮的CSS ID。 使用硒,您可以通过执行driver.find_element_by_id('statisticsPill')来按其ID找到按钮,然后单击该按钮即可显示该表。
加载之后,您可以解析表以获取所需的数据。

例:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.ultimatetennisstatistics.com/playerProfile?playerId=4742")

try:
    # Fist click on the dropdown
    dropdown = driver.find_element_by_xpath("//a[@id='statisticsPill']/../../..")
    dropdown.click()

    # Then click on the statistics button
    bm = driver.find_element_by_id('statisticsPill')
    bm.click()
except NoSuchElementException as e:
    # Do error handling when cannot find the button

编辑:您必须先单击下拉菜单以使按钮可见,然后单击它。

暂无
暂无

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

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