【问题标题】:How can I scrape data from a website within a frame using R?如何使用 R 从框架内的网站中抓取数据?
【发布时间】:2016-05-23 11:27:18
【问题描述】:

以下链接包含巴黎马拉松赛的结果: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)

非常欢迎任何帮助解决这个问题!

【问题讨论】:

    标签: r web-scraping rvest rselenium


    【解决方案1】:

    结果由 ajax 从以下 url 加载:

    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 的非常基本的技术,您必须对网站了解更多,并且每个网站都有自己的结构。对于这个,我是这样做的:

    1. 如您所见,在源代码中您看不到任何结果,因为它们位于 iframe 中,并且在检查代码时,您可以在“RESULTS OF 2016 EDITION”之后看到:

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

    2. 现在你可以直接使用这个网址了:http://www.aso.fr/massevents/resultats/index.php?langue=us&course=mar16&version=2

    3. 但您仍然可以获得结果。然后,您可以使用 Chrome 开发者工具 > 网络 > XHR。刷新页面时,可以看到数据是从这个url加载的(当你选择Men类别时):http://www.aso.fr/massevents/resultats/ajax.php?course=mar16&langue=us&version=2&action=search&fields%5Bsex%5D=F&limiter=&order=

    4. 现在你可以得到结果了!

    5. 如果你想要第二页等你可以点击页码,然后使用开发者工具看看会发生什么!

    【讨论】:

    • 谢谢,这解决了我的问题!对于未来的问题,你能告诉我你是如何获得这个网址的吗?我在源代码中找不到它。
    猜你喜欢
    • 2020-07-07
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多