【问题标题】:How to Skip a Webpage After a Period of Time Selenium如何在一段时间后跳过网页 Selenium
【发布时间】:2019-10-15 17:12:11
【问题描述】:

我正在解析一个包含大量大学的文件。 Selenium googles "Admissions " + college_name 然后单击第一个链接并从每个页面获取一些数据。问题是我从中提取的大学名称列表非常粗略(技术上是美国所有认可机构的列表),因此某些链接已断开或卡在加载循环中。我如何设置某种基本上说的计时器

    if page load time > x seconds:
        go to next element in list

【问题讨论】:

标签: python selenium web-scraping automation


【解决方案1】:

您可以在页面上调用WebDriverWait,如果页面捕获到TimeoutException,那么您将知道加载时间过长,因此您可以继续下一个。

鉴于您不知道每个页面的 HTML 会是什么样子,这是一个非常具有挑战性的问题。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


# list of college names
names = []

for name in names:
    # search for the college here

    # get list of search results
    WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='rc']")))
    search_results = driver.find_elements_by_xpath("//div[@class='rc']")

    # get first result
    search_result = search_results[0]

    # attempt to load the page
    try:
        search_result.click()
    except TimeoutException:
        # click operation should time out if next page does not load
        # pass to move on to next URL
        pass

这是一个非常粗略的概括性大纲。正如我所提到的,如果不知道预期的页面标题是什么,或者预期的页面内容会是什么样子,那么编写一个能够成功完成此任务的通用方法是非常困难的。这段代码只是您的起点。

【讨论】:

    猜你喜欢
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多