首页 > 解决方案 > 当我想在RSelenium中获得“没有html标签”时如何返回

问题描述

我正在抓取 RSelenium 的一些社区帖子。

使用 clickElement() 进入文章 url,并使用 goBack() 返回第一个屏幕。

但是我在进入特定文章时遇到了问题,该文章已被删除。

在第一个屏幕上,列表中有一篇文章,但是当我 clickElement() 那个特定文章时,它显示“文章已删除”,然后重定向到其他 url。

所以我得到错误,它停止工作。

这是我的代码。

 for(i in 1:25){
    tryCatch({
      title_url <- mybrowser$findElement('xpath', paste0('//*[@id="container"]/div/section[2]/div[1]/div[2]/ul/li[',i,']/a'))
      title_url$clickElement()
      Sys.sleep(sample(1:3,1))
      html <- mybrowser$getPageSource()[[1]]
      post_html <- read_html(html)
      title <- post_html %>% html_node('.title_subject') %>% html_text('span')
      datetime <- post_html %>% html_node('.gall_date') %>% html_text()
      content <- post_html %>% html_node(xpath='//*[@id="container"]/section/article[2]/div[1]/div/div[1]/div[1]/div[2]') %>% html_text()
      mybrowser$goBack()
      })
  }

无论您遇到什么错误,我都希望使用 goBack() 函数。

错误喜欢

  1. 如果没有我期望的 html 标签。
  2. 重定向到我没想到的其他网址。

等等

请帮我 :(

标签: rselenium-chromedriver

解决方案


推荐阅读