【问题标题】:Scraping "load more" buttom giving error - unable to locate element抓取“加载更多”按钮给出错误 - 无法定位元素
【发布时间】:2019-08-08 21:15:03
【问题描述】:

我正在尝试在此网站https://www.coindesk.com/ 上重现此帖子Issue scraping page with "Load more" button with rvest 中所选答案的代码。但是,以下行给出了错误:

#original    
#load_btn <- ffd$findElement(using = "css selector", ".load-more .btn")
#modified
load_btn <- ffd$findElement(using = "css selector", ".load-more-stories .btn")

Selenium 消息:无法定位元素:load-more-stories For 有关此错误的文档,请访问: https://www.seleniumhq.org/exceptions/no_such_element.html 构建信息: 版本:'4.0.0-alpha-2',修订:'f148142cf8',时间: '2019-07-01T21:30:10' 系统信息:主机:'LAPTOP-sdsds9L',ip: 'sdssd',os.name:'Windows 10',os.arch:'x86',os.version: '10.0', java.version: '1.8.0_211' 驱动信息:driver.version: 未知

错误:摘要:NoSuchElement 详细信息:一个元素不能 使用给定的搜索参数位于页面上。班级: org.openqa.selenium.NoSuchElementException 更多细节:运行 errorDetails 方法

我根据第 449-452 行假设了按钮名称:

 </div>
            <div id="load-more-stories">
    <button>Load More Stories</button>
</div>        </div>

知道如何正确调整此策略吗?

【问题讨论】:

    标签: html r web-scraping rselenium


    【解决方案1】:

    您首先需要通过单击接受按钮关闭 cookie 栏,然后继续使用 load-more-stories 作为 id,而不是类。我无法在 R 中进行测试,但类似:

    cookie_button  <- ffd$findElement("css selector", '#CybotCookiebotDialogBodyLevelButtonAccept')
    cookie_button$clickElement()
    load_more_button  <- ffd$findElement("css selector", '#load-more-stories')
    load_more_button$clickElement()
    

    参考资料:

    1. https://cran.r-project.org/web/packages/RSelenium/RSelenium.pdf

    【讨论】:

      【解决方案2】:

      诊断:基本上你遇到了这个问题,因为页面没有重定向到另一个页面,而是在页面上添加文章链接。我用Web Scraping Language写了这篇文章

      GOTO www.coindesk.com &gt;&gt; CRAWL ['#load-more-stories', 3] .stream-article &gt;&gt; EXTRACT {'title':'.meta h1', 'article':'.article-content'}

      解释:这应该通过点击底部的#load-more-stories 或“加载更多故事”链接将所有文章抓取到3rd 页面。然后,它使用选择器 .stream-article 访问每个链接,并在随后的页面上,使用各自的选择器提取 titlearticle

      【讨论】:

      • 知道如何使用 RSelenium 包进行调整吗?
      • @user3091668 它与库无关,只是基于云的服务,这意味着您编写 WSL,它会抓取和抓取所有数据。它也很容易阅读,这意味着您可以在以后维护它,而不是处理实际代码。
      【解决方案3】:

      HTML id= 与 CSS 类不同。

      因此,您的选择器错误且不匹配。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 2021-03-05
        • 1970-01-01
        • 2021-09-26
        • 2015-12-14
        相关资源
        最近更新 更多