代码之家  ›  专栏  ›  技术社区  ›  SIM

脚本无法从重定向的url解析标题

  •  1
  • SIM  · 技术社区  · 7 年前

    我用python和selenium编写了一个脚本,从网页中获取标题地址。我在脚本中使用的url会在几秒钟内自动重定向。这就是我的脚本遇到错误的地方。我粘贴了错误的一部分给你一个想法。

    ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
    
    During handling of the above exception, another exception occurred:
    

    Link to that url which gets redirected to another page

    from contextlib import closing
    from selenium import webdriver
    from selenium.webdriver.support import ui
    
    url = "https://www.rightmove.co.uk/propertyMedia/redirect.html?propertyId=30578943&contentId=1625965454&index=1"
    
    with closing(webdriver.Chrome()) as wd:
        wait = ui.WebDriverWait(wd, 10)
        wd.get(url)
        item = wait.until(lambda driver: driver.find_element_by_css_selector("h1.header_address__title")).text
        print(item)
    

    这是我希望从该页面得到的输出:

    Park View Back Road, Locharbriggs, Dumfries, DG1
    

    这是我在那个错误之前看到的:

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   Andersson    7 年前

    item = wait.until(lambda driver: driver.find_element_by_css_selector("h1.header_address__title")).text
    

    这意味着 等待特定元素出现在DOM中,并立即获取其当前可见的文本(可能返回空字符串)

    item = wait.until(lambda driver: driver.find_element_by_css_selector("h1.header_address__title").text)
    

    这意味着 等待特定元素,并在它不是空字符串时返回其可见文本

    item = driver.find_element_by_css_selector("h1.header_address__title").get_attribute('textContent')
    

    至于你的 chromedriver that stops working Chrome 和 chromedriver