首页 > 解决方案 > 使用 Selenium 获取数据

问题描述

我正在学习 Python,我的第一个项目是使用 Selenium 从 NFL 网站获取数据。我在添加数据的循环中遇到了一些问题,然后单击下一个按钮直到结束。出现的错误信息是这样的:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method": "xpath", "selector": "// * [@ title="下一页"]"}(会话信息: 铬 = 89.0.4389.114)

另外,我不知道这段代码是否会将数据添加到我的数据框中。我做错了什么或缺少什么?

# -*- encoding: utf-8 -*-
import time
import requests
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import json
from selenium.common.exceptions import TimeoutException


colunas =['Player','Pass Yds','Yds/Att','Att','Cmp','TD','INT','Rate','1st','1st%','20+','40+','Lng','Sck','SckY']
qb=pd.DataFrame()


url = "https://www.nfl.com/stats/player-stats/"
option = Options
option.headless= True
driver = webdriver.Chrome(executable_path='C:\Program Files\chromedriver.exe')
driver.get(url)

next_link = driver.find_element_by_xpath('//*[@title="Next Page"]')

while True:
    element = driver.find_element_by_xpath("//div[@class='d3-o-table--horizontal-scroll']//table")
    html_content = element.get_attribute('outerHTML')
    time.sleep(3)

    soup = BeautifulSoup(html_content,'html.parser')
    table = soup.find(name='table')

    df_full = pd.read_html( str(table))[0]

    df1= df_full[['Player','Pass Yds','Yds/Att','Att','Cmp','TD','INT','Rate','1st','1st%','20+','40+','Lng','Sck','SckY']]
    qb = pd.concat([df1])

    try:
        next_link = driver.find_element_by_xpath('//*[@title="Next Page"]')
        next_link.click()

    except ModuleNotFoundError:
        break

driver.quit()

标签: pythonseleniumweb-scraping

解决方案


try:
    next_link = driver.find_element_by_xpath('//*[@title="Next Page"]')
    next_link.click()

except NoSuchElementException:
    break

只需抓住错误并打破。

进口

from selenium.common.exceptions import NoSuchElementException

推荐阅读