Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用R从php网站中刮取一个大表_R_Web Scraping_Rvest_Scrape - Fatal编程技术网

如何使用R从php网站中刮取一个大表

如何使用R从php网站中刮取一个大表,r,web-scraping,rvest,scrape,R,Web Scraping,Rvest,Scrape,我想把桌子从桌子上刮下来https://www.metabolomicsworkbench.org/data/mb_structure_ajax.php" 我在网上找到的代码(rvest)不起作用 library(rvest) url <- "https://www.metabolomicsworkbench.org/data/mb_structure_ajax.php" A <- url %>% read_html() %>% html_no

我想把桌子从桌子上刮下来https://www.metabolomicsworkbench.org/data/mb_structure_ajax.php"

我在网上找到的代码(rvest)不起作用

library(rvest)
url <- "https://www.metabolomicsworkbench.org/data/mb_structure_ajax.php"
A <- url %>%
  read_html() %>%
  html_nodes(xpath='//*[@id="containerx"]/div[1]/table') %>%
  html_table()
库(rvest)
url%
html_节点(xpath='/*[@id=“containerx”]/div[1]/table')%>%
html_表()
A是“0的列表”

我应该如何修复此代码,或者有更好的方法吗


提前感谢。

页面源代码由JS生成。以下是您要做的:

  • 打开浏览器的开发工具并转到网络选项卡。
  • 点击其中一页,看看发生了什么(我点击了第4页)。您可以看到页面向
    https://www.metabolomicsworkbench.org/data/mb_structure_tableonly.php
    并获取它的内容。 以下是参数:
  • 通过
    rvest
    模拟POST请求。以下是清除所有页面的代码:
  • 库(rvest)
    
    url页面源代码由JS生成。以下是您要做的:

  • 打开浏览器的开发工具并转到网络选项卡。
  • 点击其中一页,看看发生了什么(我点击了第4页)。您可以看到页面向
    https://www.metabolomicsworkbench.org/data/mb_structure_tableonly.php
    并获取它的内容。 以下是参数:
  • 通过
    rvest
    模拟POST请求。以下是清除所有页面的代码:
  • 库(rvest)
    
    网址谢谢!成功了!因此,如果我在网络中看到一个jsquery,这意味着web源代码是由js.nodes?@codingExplorer No生成的。查看页面源代码,而不是开发工具(Inspect),是实际读取的内容
    rvest
    。如果你在那里找不到你的内容,这意味着内容是由JS生成的。如果对您有帮助,请将我的答案标记为已回答。@xwhitelight,写得很好的答案,非常好的走查提示似乎我会出现以下错误:
    错误:无法组合“…1$Studies”和“…13$Studies”
    。250页之后。你有什么建议吗?Thanks@codingExplorer这意味着某些元素不是整数而是字符。尝试将代码更新为
    read_html(pg)%%>%html\u节点(“表”)%%>%html\u表()%%>%dplyr::mutate(Studies=as.character(Studies))
    并查看它是否已修复。谢谢!成功了!因此,如果我在网络中看到一个jsquery,这意味着web源代码是由js.nodes?@codingExplorer No生成的。查看页面源代码,而不是开发工具(Inspect),是实际读取的内容
    rvest
    。如果你在那里找不到你的内容,这意味着内容是由JS生成的。如果对您有帮助,请将我的答案标记为已回答。@xwhitelight,写得很好的答案,非常好的走查提示似乎我会出现以下错误:
    错误:无法组合“…1$Studies”和“…13$Studies”
    。250页之后。你有什么建议吗?Thanks@codingExplorer这意味着某些元素不是整数而是字符。尝试将代码更新为
    read\u html(pg)%%>%html\u节点(“表”)%%>%html\u table()%%>%dplyr::mutate(Studies=as.character(Studies))
    并查看它是否已修复。
    library(rvest)
    
    url <- "https://www.metabolomicsworkbench.org/data/mb_structure_tableonly.php"
    pg <- html_session(url)
    data <- 
      purrr::map_dfr(
        1:4288, # you might wanna change it to a small number to try first or scrape multiple times and combine data frames later, in case something happens in the middle
        function(i) {
          pg <- rvest:::request_POST(pg,
                                     url,
                                     body = list(
                                       page = i
                                     ))
          read_html(pg) %>%
            html_node("table") %>%
            html_table() 
        }
      )