我正在尝试抓取无限页面中的所有可用链接,向下滚动并获取新链接。但是,time.sleep()不允许在一次又一次向下滚动之前暂停驱动程序一段合理的时间。
有没有办法调整你可以在底部找到的代码,以减少第一次迭代期间的睡眠次数(当页面仍然快速加载新内容时)并等待下一次迭代所需的时间(当页面时)会慢慢加载新内容)?
使用简单
for i in range(1,20):
time.sleep(i)
不会让我在第一次迭代中节省时间,并且在多次迭代后不会有效地调整time.sleep()。
以下是我在“How can I scroll a web page using selenium webdriver in python?”中找到的建议中使用的代码:
from selenium import webdriver
scroll_pause_time = 5
scraped_links = []
driver = webdriver.Chrome(executable_path=driver_path)
driver.get(url)
links = driver.find_elements_by_xpath(links_filepath)
for link in links:
if link not in scraped_links:
scraped_links.append(link)
print(link)
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(scroll_pause_time)
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
links = driver.find_elements_by_xpath(links_filepath)
for link in links:
if link not in scraped_links:
scraped_links.append(link)
print(link)
在20-30次迭代之后,代码中断,因为time.sleep()与网页的刷新速度相比太低。
如果你不想猜测每次加载页面需要多长时间并设置一些随机秒钟来休眠,你可以使用Explicit Waits。例:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
except common.exceptions.TimeoutException:
print('TimeoutException')
finally:
driver.quit()
# do what you want after necessary elements are loaded
当time.sleep()与网页的刷新速度相比变得太低时,这将解决问题。