Python-Selenium爬取bvc.com.co时第三页加载空白的问题求助
Python-Selenium爬取bvc.com.co时第三页加载空白的问题求助
各位大佬好,我最近在爬取哥伦比亚证券交易所(bvc.com.co)的个股数据时遇到了一个棘手的问题:当循环加载到第三个股票页面时,浏览器会突然变成空白状态,导致Selenium的等待条件无法定位到目标元素,直接抛出超时异常。
我当前的代码如下:
stocks = [ 'https://www.bvc.com.co/renta-variable-mercado-local/cibest?tab=operaciones', 'https://www.bvc.com.co/renta-variable-mercado-local/pfcibest?tab=operaciones', 'https://www.bvc.com.co/renta-variable-mercado-local/bogota?tab=operaciones', 'https://www.bvc.com.co/renta-variable-mercado-local/bhi?tab=operaciones', 'https://www.bvc.com.co/renta-variable-mercado-local/celsia?tab=operaciones' ] import selenium, time import selenium.webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait driver = selenium.webdriver.Chrome() for i in stocks: print(i) # 我试过把driver初始化放到循环里,能解决但效率太低 driver.get(i) time.sleep(1) # 等待目标元素出现 target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="__next"]/div/div[3]/div[3]/div/div[1]/ul/li[3]'))) driver.execute_script("arguments[0].scrollIntoView()", target) time.sleep(1)
目前我找到的临时解决办法是把驱动初始化的代码移到循环内部——也就是每爬一只股票就重新打开一次Chrome。但这样每次都要启动和关闭浏览器,导致代码运行速度慢了很多,显然不是最优解。
另外,我还尝试过添加反反爬参数options.add_argument('--disable-blink-features=AutomationControlled'),但这个设置并没有解决页面空白的问题。
想请教各位,有没有办法能复用同一个Chrome驱动实例,同时避免加载到第三页时出现空白的情况?
内容来源于stack exchange
相关产品推荐
相关产品推荐

