Python 脚本可以';t从重定向的url解析标题
我用python和selenium编写了一个脚本,用于从网页获取标题地址。我在脚本中使用的url会在几秒钟内自动重定向。这就是我的脚本遇到错误的地方。我粘贴了错误的一部分给你一个想法Python 脚本可以';t从重定向的url解析标题,python,python-3.x,selenium,selenium-webdriver,web-scraping,Python,Python 3.x,Selenium,Selenium Webdriver,Web Scraping,我用python和selenium编写了一个脚本,用于从网页获取标题地址。我在脚本中使用的url会在几秒钟内自动重定向。这就是我的脚本遇到错误的地方。我粘贴了错误的一部分给你一个想法 ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host During handling of the above exception, another exceptio
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
我尝试过的脚本:
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
这是我在那个错误之前看到的:
您可能需要更换
item = wait.until(lambda driver: driver.find_element_by_css_selector("h1.header_address__title")).text
这意味着等待特定元素出现在DOM中,并立即获取其当前可见的文本(可能返回空字符串)
与
这意味着等待特定元素并在它不是空字符串时返回其可见文本
但我知道你能做什么
item = driver.find_element_by_css_selector("h1.header_address__title").get_attribute('textContent')
获取文本值,即使该文本当前未显示在页面上
至于你的
chromedriver停止工作
问题:尝试更新这两个版本和最新版本现在它给了我一个非常友好的异常错误@sir Andersson。你更新chromedriver的建议让我摆脱了那个恶性问题(上图中的那个)。我现在遇到的第一个`选择器
的错误是引发TimeoutException(消息、屏幕、堆栈跟踪)selenium.common.exceptions.TimeoutException:message:
。对于第二个建议:`raiseexception_class(message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:message:没有这样的元素:无法定位元素:{“method”:“css选择器”,“选择器”:“h1.header_address_utitle”}`@asmitu,oh。。。抱歉:)我没有检查元素-只是使用了选择器。。。让我检查一下now@asmitu是的。一些备注:)第一个选项不起作用,因为当浏览器窗口未最大化时,标题似乎不可见,所以在脚本开头使用wd.maximize\u window()
,或者item=wait.until(lambda driver:driver.find\u element\u by\u css\u选择器(“h1.header\u address\u title”)。获取\u属性(“textContent”)
(driver.find\u element\u by_css\u selector(“h1.header\u address\u title”)。get\u属性('textContent')
without wait不起作用,因为找不到它,因为初始页面不是我们的目标页面)顺便说一句,在更新我的chromedriver之前,我在其他网站上取得了成功,但在这个网站上遇到了上述错误。这种神秘行为有什么原因吗?如果你不想回答这一部分,请随意忽略。再次感谢,先生。@asmitu,我实际上没有做任何研究,所以我不知道为什么会发生这种情况。我只是知道如果出现任何操作系统错误,首先要做的是检查浏览器webDriver的兼容性:)
item = driver.find_element_by_css_selector("h1.header_address__title").get_attribute('textContent')