如何使用R从php网站抓取大表

问题描述

我正在尝试从“ 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()

A是“ 0列表”

我应该如何解决代码,或者有更好的方法吗?

谢谢。

解决方法

页面源由JS生成。这是您的工作:

  1. 打开浏览器的开发工具,然后转到“网络”标签。 enter image description here
  2. 单击页面之一,查看发生了什么(我单击到第4页)。您可以看到该页面向images/q3.png发送了POST请求并获取了其内容。 enter image description here 以下是参数: enter image description here
  3. 通过https://www.metabolomicsworkbench.org/data/mb_structure_tableonly.php模拟POST请求。以下是刮取所有页面的代码:
rvest