2017-10-06 68 views
4

我想刮這個website上的數據表。颳去頁面源中不存在的數據表

我檢查這個頁面的頁面源碼,這個表格在頁面源碼中不存在。

然後我檢查網絡信息刷新的網站時,似乎數據表通過發送POST請求到這個網址獲得:

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

然後我試圖發送POST請求,只是沒有什麼用狀態500.

我想知道有無論如何通過使用R刮這張表嗎?

謝謝。

回答

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