首页 文章

如何使用R从帧中的网站抓取数据?

提问于
浏览
1

以下链接包含巴黎马拉松的结果:http://www.schneiderelectricparismarathon.com/us/the-race/results/results-marathon . 我想抓取这些结果,但信息在一个框架内 . 我知道使用Rvest和Rselenium进行刮擦的基础知识,但我对如何在这样的框架内检索数据毫无头绪 . 为了得到一个想法,我尝试的其中一件事是:

url = "http://www.schneiderelectricparismarathon.com/us/the-race/results/results-marathon"
site = read_html(url)
ParisResults = site %>% html_node("iframe") %>% html_table()
ParisResults = as.data.frame(ParisResults)

任何帮助解决这个问题都是非常受欢迎的!

1 回答

  • 3

    结果由以下url中的ajax加载:

    url="http://www.aso.fr/massevents/resultats/ajax.php?v=1460995792&course=mar16&langue=us&version=3&action=search"
      table <- url %>%
        read_html(encoding="UTF-8") %>%
        html_nodes(xpath='//table[@class="footable"]') %>%
        html_table()
    

    PS:我不知道ajax究竟是什么,我只知道rvest的基础知识

    编辑:为了回答评论中的问题:我没有很多网络抓取经验 . 如果您只使用rvest或xml的非常基本的技术,您必须了解更多的网站,并且每个站点都有自己的结构 . 对于这个,我是这样做的:

    • 如您所见,在源代码中您没有看到任何结果,因为它们位于iframe中,并且在检查代码时,您可以在“2016版本的结果”后看到:

    class =“iframe-xdm iframe-resultats”data-href =“http://www.aso.fr/massevents/resultats/index.php?langue=us&course=mar16&version=3”

相关问题