使用 Selenium 抓取网站时出现 NoSuchElementException

时间:2021-04-29 18:37:46

标签: python-3.x selenium selenium-webdriver web-scraping

我试图从这个网址中抓取玩家姓名和位置:https://thedraftnetwork.com/articles/2021-nfl-draft-big-board-marino

这是我的代码:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get('https://thedraftnetwork.com/articles/2021-nfl-draft-big-board-marino')
p_element = driver.find_element_by_xpath('//*[@class="player-name-event name gold-fade"]')
p_element.text

我收到的错误信息:

<块引用>

NoSuchElementException: Message: {"errorMessage":"无法找到 带有 xpath '//[@class="player-name-event name 的元素 gold-fade"]'","re​​quest":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Content-Length":"132","Content -Type":"application/json;charset=UTF-8","Host":"127.0.0.1:51897","User-Agent":"selenium/3.141.0 (Python windows)"},"httpVersion":"1.1","method":"POST","post":"{"using": "xpath", "value": "//[@class=\"player-name-event name 金色渐变\"]", "sessionId": "ece0eb30-a916-11eb-95db-d1d816c61299"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element", "目录":"/","路径":"/element","re​​lative":"/element","port":"","host":"","password":"","user" :"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"] },"urlOriginal":"/session/ece0eb30-a916-11eb-95db-d1d816c61299/element"}} 截图:可通过屏幕获取

我知道这个类存在(玩家名称-事件名称gold-fade),但错误消息指出它找不到它。还有一个叫做“player-info”的类,Selenium 也找不到它。感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

请尝试以下代码片段:

driver.get('https://thedraftnetwork.com/articles/2021-nfl-draft-big-board-marino')

wait = WebDriverWait(driver, 60)
players = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'h4.player-name-event.name.gold-fade')))
positions = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div.editorial')))

for player, position in zip(players, positions):
    print(player.text +" " +position.text)

以下导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

答案 1 :(得分:0)

只需从 api 中获取:

import requests
import pandas as pd

s = requests.Session()
s.get('https://thedraftnetwork.com/')
url = 'https://apiv2.thedraftnetwork.com/api/classes/BigBoard'
headers = {
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9,en-GB;q=0.8',
'content-length': '279',
'content-type': 'text/plain',
'origin': 'https://thedraftnetwork.com',
'referer': 'https://thedraftnetwork.com/',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
'sec-ch-ua-mobile': '?0',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'}
payload = '''{"where":{"objectId":"6YRF5b8Y8e"},"include":"picks,picks.player,picks.player.collegeTeam","limit":1,"_method":"GET","_ApplicationId":"the-draft-network","_JavaScriptKey":"OBv0uezmNiZ2DUuz4Chk","_ClientVersion":"js2.15.0","_InstallationId":"6089d72c-1dd9-4f07-90de-1ef897ed1318"}'''

jsonData = s.post(url, headers=headers, data=payload).json()
picks = jsonData['results'][0]['picks']

df = pd.json_normalize(picks).sort_values('pickPosition').reset_index(drop=True)

输出:

print(df.head(10).to_string())

Output from spyder call 'get_namespace_view':
      objectId  pickPosition                  editorialReview                 createdAt                 updatedAt  __type className player.objectId player.collegeTeam.objectId player.collegeTeam.school player.collegeTeam.name                                                    player.collegeTeam.logoUrl  player.collegeTeam.sportsDataId player.collegeTeam.conference player.collegeTeam.shortDisplayName player.collegeTeam.slug player.collegeTeam.ourLadsName player.collegeTeam.tdnName player.collegeTeam.createdAt player.collegeTeam.updatedAt player.collegeTeam.__type player.collegeTeam.className player.draftYear player.height player.highschoolStats       player.name player.position player.weight player.year player.dateOfBirth      player.slug  player.school          player.createdAt          player.updatedAt                                                                   player.analyses  player.featuredImage player.image.__type                                     player.image.name                                                                                                      player.image.url player.jerseyNumber player.playerMatch.__type player.playerMatch.className player.__type player.className player.highSchool         player.conference player.featuredImage.__type                                       player.featuredImage.name                                                                                                      player.featuredImage.url  player.image player.ourLadsName player.collegeStats
57  nSvC9hokSg             1  <p>Final Grade: 94.00 / 100</p>  2021-04-22T19:28:09.350Z  2021-04-22T19:28:09.350Z  Object      Pick      KEAwwL73zW                  FEXHZnJnWJ                   Clemson                  Tigers   https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/27.png                         50000027     Atlantic Coast - Atlantic                                CLEM                 clemson                 Clemson Tigers                    Clemson     2019-07-26T22:41:00.004Z     2019-07-26T22:41:00.004Z                    Object                  CollegeTeam             2021          6'6"         5-star recruit   Trevor Lawrence              QB       220 lbs      Junior         10/06/1999  trevor-lawrence        Clemson  2020-04-17T21:05:58.198Z  2020-12-31T18:28:55.709Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': 'VxMuexuSKt'}]                   NaN                File  c409766d6560d54ae063a53a358d4f77_Trevor Lawrence.png  https://apiv2.thedraftnetwork.com/api/files/the-draft-network/c409766d6560d54ae063a53a358d4f77_Trevor%20Lawrence.png                None                  Relation                       Player        Object           Player               NaN                       NaN                        File   c466aea5fdc42dfb955c3cd0e9458b65_TDN-FeaturedImage-3x2__1.jpg   https://apiv2.thedraftnetwork.com/api/files/the-draft-network/c466aea5fdc42dfb955c3cd0e9458b65_TDN-FeaturedImage-3x2__1.jpg           NaN                NaN                 NaN
23  ZKQxFpHEX9             2  <p>Final Grade: 93.00 / 100</p>  2021-04-22T19:28:09.333Z  2021-04-22T19:28:09.333Z  Object      Pick      R2fSJ5V9IZ                  PXUTCKJdCA                    Oregon                   Ducks  https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/245.png                         50000245                Pac-12 - North                              OREGON                  oregon                   Oregon Ducks                     Oregon     2019-07-26T22:42:51.439Z     2019-07-26T22:42:51.439Z                    Object                  CollegeTeam             2021          6'5"         4-star recruit    Penei Sewell                OT       325 lbs      Junior         10/09/2000     penei-sewell         Oregon  2020-04-22T22:14:50.965Z  2020-12-31T19:16:02.346Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': 'GoLA2yogOK'}]                   NaN                File     bd12a6584f0ebbaef2c263a5a1ee8a78_Penei Sewell.png     https://apiv2.thedraftnetwork.com/api/files/the-draft-network/bd12a6584f0ebbaef2c263a5a1ee8a78_Penei%20Sewell.png                None                  Relation                       Player        Object           Player               NaN                       NaN                        File   4d6fb86f63a0725c42bc5362afaf0d8c_TDN-FeaturedImage-3x2__2.jpg   https://apiv2.thedraftnetwork.com/api/files/the-draft-network/4d6fb86f63a0725c42bc5362afaf0d8c_TDN-FeaturedImage-3x2__2.jpg           NaN                NaN                 NaN
47  vAfcOLpH2J             3  <p>Final Grade: 92.50 / 100</p>  2021-04-22T19:28:09.350Z  2021-04-22T19:28:09.350Z  Object      Pick      SZFJcdG3nL                  5PxoN5ziAz                   Florida                  Gators  https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/271.png                         50000271                    SEC - East                                 UFL                 florida                 Florida Gators                    Florida     2019-07-26T22:43:09.010Z     2019-07-26T22:43:09.010Z                    Object                  CollegeTeam             2021          6'5"         4-star recruit     Kyle Pitts                 TE       239 lbs      Junior                          kyle-pitts        Florida  2020-04-22T22:54:42.521Z  2020-12-31T18:52:43.365Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': 'CuiHkn3Q2h'}]                   NaN                File       1cb027bcd7c4bfe09126f15a99beb874_Kyle Pitts.png       https://apiv2.thedraftnetwork.com/api/files/the-draft-network/1cb027bcd7c4bfe09126f15a99beb874_Kyle%20Pitts.png                None                  Relation                       Player        Object           Player               NaN                       NaN                        File  c5571dfbabfda2eebcc030df950128f5_TDN-FeaturedImage-3x2__11.jpg  https://apiv2.thedraftnetwork.com/api/files/the-draft-network/c5571dfbabfda2eebcc030df950128f5_TDN-FeaturedImage-3x2__11.jpg           NaN                NaN                 NaN
0   MniaCcCb1Y             4  <p>Final Grade: 92.00 / 100</p>  2021-04-22T19:28:09.315Z  2021-04-22T19:28:09.315Z  Object      Pick      gmqrK85AUP                  BXMxcTnllS                   Alabama            Crimson Tide  https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/275.png                         50000275                    SEC - West                                 ALA                 alabama           Alabama Crimson Tide                    Alabama     2019-07-26T22:43:12.230Z     2019-07-26T22:43:12.230Z                    Object                  CollegeTeam             2021         5'10"         4-star recruit   Jaylen Waddle                WR       182 lbs      Junior                       jaylen-waddle        Alabama  2020-04-22T22:58:45.599Z  2021-01-05T14:39:06.697Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': '4AlB2vRwSl'}]                   NaN                File    f969cc2594ffc82b49022a9dd403a35b_Jaylen Waddle.png    https://apiv2.thedraftnetwork.com/api/files/the-draft-network/f969cc2594ffc82b49022a9dd403a35b_Jaylen%20Waddle.png                None                  Relation                       Player        Object           Player               NaN                       NaN                         NaN                                                             NaN                                                                                                                           NaN           NaN                NaN                 NaN
55  Tz2wsZgaEB             5  <p>Final Grade: 90.00 / 100</p>  2021-04-22T19:28:09.347Z  2021-04-22T19:28:09.347Z  Object      Pick      SDOMHXqIQp                  7t5H4RpD0i                       LSU                  Tigers  https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/269.png                         50000269                    SEC - West                                 LSU                     lsu                     LSU Tigers                        LSU     2019-07-26T22:43:07.540Z     2019-07-26T22:43:07.540Z                    Object                  CollegeTeam             2021          6'1"         4-star recruit   Ja'Marr Chase                WR       208 lbs      Junior         03/01/2000     jamarr-chase            LSU  2020-04-22T22:57:16.625Z  2020-12-31T18:31:38.799Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': 'dJXQUftMVk'}]                   NaN                File   e97294429e533d6d78f7d4e8e09abe15_Chase Claypool.PNG   https://apiv2.thedraftnetwork.com/api/files/the-draft-network/e97294429e533d6d78f7d4e8e09abe15_Chase%20Claypool.PNG                None                  Relation                       Player        Object           Player               NaN                       NaN                        File   245f8056f57f98461710df480e0d627e_TDN-FeaturedImage-3x2__3.jpg   https://apiv2.thedraftnetwork.com/api/files/the-draft-network/245f8056f57f98461710df480e0d627e_TDN-FeaturedImage-3x2__3.jpg           NaN                NaN                 NaN
7   sdSaBnzBGh             6  <p>Final Grade: 89.50 / 100</p>  2021-04-22T19:28:09.363Z  2021-04-22T19:28:09.363Z  Object      Pick      0mSBgLCNXv                  o0tGvEgAHr                Ohio State                Buckeyes   https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/98.png                         50000098                Big Ten - East                                 OSU              ohio-state            Ohio State Buckeyes                 Ohio State     2019-07-26T22:41:34.608Z     2019-07-26T22:41:34.608Z                    Object                  CollegeTeam             2021          6'3"         5-star recruit     Justin Fields              QB       223 lbs      Junior         03/05/1999    justin-fields     Ohio State  2020-04-21T17:42:27.056Z  2020-12-20T16:13:41.704Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': 'jCNzEqDwxk'}]                   NaN                File    e4f19085a7b586d55262ea1affc961bf_Justin Fields.png    https://apiv2.thedraftnetwork.com/api/files/the-draft-network/e4f19085a7b586d55262ea1affc961bf_Justin%20Fields.png                   1                  Relation                       Player        Object           Player               NaN                       NaN                        File   c45e99461d035624a45379368b7b491b_TDN-FeaturedImage-3x2__4.jpg   https://apiv2.thedraftnetwork.com/api/files/the-draft-network/c45e99461d035624a45379368b7b491b_TDN-FeaturedImage-3x2__4.jpg           NaN                NaN                 NaN
51  17hkC7PIjk             7  <p>Final Grade: 89.02 / 100</p>  2021-04-22T19:28:09.374Z  2021-04-22T19:28:09.374Z  Object      Pick      EjpZ4YtzI0                  6pvjvEq470                       BYU                 Cougars  https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/336.png                         50000336              FBS Independents                                 BYU                     byu                    BYU Cougars                        BYU     2019-07-26T22:43:51.073Z     2019-07-26T22:43:51.073Z                    Object                  CollegeTeam             2021          6'3"                   None       Zach Wilson              QB       210 lbs      Junior                         zach-wilson            BYU  2020-06-13T11:14:19.831Z  2021-01-05T14:34:11.409Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': 'VcxDPxplrM'}]                   NaN                File           e31007f3497cac69538f687990ce0a65_Wilson.JPG             https://apiv2.thedraftnetwork.com/api/files/the-draft-network/e31007f3497cac69538f687990ce0a65_Wilson.JPG                None                  Relation                       Player        Object           Player               NaN                       NaN                         NaN                                                             NaN                                                                                                                           NaN           NaN                NaN                 NaN
11  QgaKc0JC2n             8  <p>Final Grade: 89.00 / 100</p>  2021-04-22T19:28:09.372Z  2021-04-22T19:28:09.372Z  Object      Pick      NKA9vgu5S8                  t0ClOyak0N             Virginia Tech                  Hokies   https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/31.png                         50000031      Atlantic Coast - Coastal                               VTECH           virginia-tech           Virginia Tech Hokies              Virginia Tech     2019-07-26T22:41:03.789Z     2019-07-26T22:41:03.789Z                    Object                  CollegeTeam             2021          6'2"                   None      Caleb Farley              CB       197 lbs   RS Junior         11/02/1998     caleb-farley  Virginia Tech  2019-07-27T01:19:38.823Z  2021-01-05T14:30:20.936Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': 'jHbxDweRAX'}]                   NaN                File     254ac6fa059278663cb171e6c46e6dc7_Caleb Farley.png     https://apiv2.thedraftnetwork.com/api/files/the-draft-network/254ac6fa059278663cb171e6c46e6dc7_Caleb%20Farley.png                   3                  Relation                       Player        Object           Player            Maiden  Atlantic Coast - Coastal                         NaN                                                             NaN                                                                                                                           NaN           NaN                NaN                 NaN
24  oHFu4Ds6Gb             9  <p>Final Grade: 88.00 / 100</p>  2021-04-22T19:28:09.376Z  2021-04-22T19:28:09.376Z  Object      Pick      1pKUn4IfC4                  S6HQBGhYej              Northwestern                Wildcats  https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/100.png                         50000100                Big Ten - West                                 NWU            northwestern          Northwestern Wildcats               Northwestern     2019-07-26T22:41:36.490Z     2019-07-26T22:41:36.490Z                    Object                  CollegeTeam             2021          6'4"         3-star recruit    Rashawn Slater              OT       315 lbs      Senior                      rashawn-slater   Northwestern  2020-04-29T13:50:52.192Z  2021-01-05T14:44:40.745Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': 'ig2TEpdipx'}]                   NaN                File           18c2861114e0945e8f20933bd4d3fdcf_Slater.JPG             https://apiv2.thedraftnetwork.com/api/files/the-draft-network/18c2861114e0945e8f20933bd4d3fdcf_Slater.JPG                None                  Relation                       Player        Object           Player               NaN                       NaN                         NaN                                                             NaN                                                                                                                           NaN           NaN                NaN                 NaN
74  ke7Gynui5r            10  <p>Final Grade: 87.50 / 100</p>  2021-04-22T19:28:09.435Z  2021-04-22T19:28:09.435Z  Object      Pick      fm74W3OByo                  2AGpCcpgFf                Penn State           Nittany Lions  https://s3-us-west-2.amazonaws.com/static.fantasydata.com/logos/ncaa/101.png                         50000101                Big Ten - East                                 PSU              penn-state       Penn State Nittany Lions                 Penn State     2019-07-26T22:41:37.362Z     2019-07-26T22:41:37.362Z                    Object                  CollegeTeam             2021          6'2"         5-star recruit  Micah Parsons                 LB       245 lbs      Junior         05/26/1999    micah-parsons     Penn State  2020-04-22T21:50:24.520Z  2020-12-18T21:56:58.016Z  [{'__type': 'Pointer', 'className': 'PlayerAnalysis', 'objectId': 'dWmm2BfE8E'}]                   NaN                File    3f5cffd4dbada1c0e83997285c240b10_Micah Parsons.png    https://apiv2.thedraftnetwork.com/api/files/the-draft-network/3f5cffd4dbada1c0e83997285c240b10_Micah%20Parsons.png                  11                  Relation                       Player        Object           Player               NaN                       NaN                        File   c5624e14eda2def4f52cae4bbeb7c0ee_TDN-FeaturedImage-3x2__7.jpg   https://apiv2.thedraftnetwork.com/api/files/the-draft-network/c5624e14eda2def4f52cae4bbeb7c0ee_TDN-FeaturedImage-3x2__7.jpg           NaN                NaN                 NaN