抓取不存在于页面源代码中的数据表格

4

我希望能够从这个网站上爬取数据表。

我查看了该页面的源代码,但是在源代码中并没有找到该数据表。

随后,我在刷新网页时检查了网络信息,发现数据表是通过向此URL发送POST请求获取的:

http://datacenter.mep.gov.cn:8099/ths-report/report!list.action

然后我尝试发送POST请求,但状态为500且无法获得任何响应。

我想知道是否有使用R来抓取这个表格的方法?

谢谢。

1个回答

1

很好的侦查!

它为我发出了GET请求。这似乎有用。它还尝试为您选择适当的目标:

library(httr)
library(rvest)
library(stringi)

pg <- read_html("http://datacenter.mep.gov.cn/index!MenuAction.action?name=259206fe260c4cf7882462520e1e3ada")

html_nodes(pg, "div[onclick]") %>% 
  html_attr("onclick") %>% 
  stri_replace_first_fixed('load("', "") %>% 
  stri_replace_last_regex('",".*$', "") -> report_urls

head(report_urls)
## [1] "http://datacenter.mep.gov.cn:8099/ths-report/report!list.action?xmlname=1462849093743"
## [2] "http://datacenter.mep.gov.cn:8099/ths-report/report!list.action?xmlname=1462764947052"
## [3] "http://datacenter.mep.gov.cn:8099/ths-report/report!list.action?xmlname=1465594312346"
## [4] "http://datacenter.mep.gov.cn:8099/ths-report/report!list.action?xmlname=1462844293531"
## [5] "http://datacenter.mep.gov.cn:8099/ths-report/report!list.action?xmlname=1462844935563"
## [6] "http://datacenter.mep.gov.cn:8099/ths-report/report!list.action?xmlname=1462845592195"

rpt_pg <- read_html(report_urls[1])
html_table(rpt_pg)[[2]]
# SO won't let me paste the table 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接