Selenium爬取RotoWire时遇iFrame拦截且仅返回单队数据的问题求助
Selenium爬取RotoWire时遇iFrame拦截且仅返回单队数据的问题求助
我之前写了个用Selenium爬取RotoWire网站NBA球员预计出场时间的函数,之前一直运行得很顺畅。但周末的时候网站好像加了个Google Ads的iFrame,直接导致我的程序抛出ElementClickInterceptedException: not clickable at point (x,y)的错误。
我试着用execute_script做了些调整——比如把元素滚动到视图内、强制点击,好不容易重新能获取到预计出场时间了,但新问题又来了:现在程序只返回每一组对决里其中一支球队(客队)的数据,另一支球队的完全拿不到。
我的代码如下:
def get_nba_lineups(): driver = gs.Chrome() ## Some code to login # Navigate to NBA Lineups driver.get('https://www.rotowire.com/basketball/nba-lineups.php') time.sleep(3) data = [] buttons = WebDriverWait(driver, 3).until( EC.presence_of_all_elements_located((By.CLASS_NAME, "see-proj-minutes"))) for button in buttons: driver.execute_script("arguments[0].scrollIntoView(true);", button) time.sleep(1) WebDriverWait(driver, 3).until(EC.element_to_be_clickable(button)) driver.execute_script("arguments[0].click();", button) time.sleep(1) team = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CLASS_NAME, 'lineups-viz'))) teamName = team.find_element(By.TAG_NAME, 'img').get_attribute('alt') players = driver.find_elements(By.CLASS_NAME, 'lineups-viz__player') for player in players: name = player.find_element(By.TAG_NAME, 'a').text projMin = player.find_element(By.CLASS_NAME, "minutes-meter__proj") projMin = int(projMin.text.split(' ')[0]) data.append({'Team': teamName, 'Player': name, 'Projected Minutes': projMin}) close_button = WebDriverWait(driver, 3).until( EC.element_to_be_clickable((By.CLASS_NAME, "lineups-viz__close")) ) driver.execute_script("arguments[0].click();", close_button) time.sleep(1) driver.quit() df = pd.DataFrame(data) return df
有没有大佬知道为什么现在没法获取全部数据?该怎么修复这个问题?或者有没有办法绕过那个挡路的iFrame,让我回到之前能用的代码逻辑里?我试过放大浏览器窗口、加更长的停顿时间,但都不管用。提前谢谢大家的建议了!
备注:内容来源于stack exchange,提问作者AMJ
相关产品推荐
相关产品推荐

