【问题标题】:Scraping data table that doesn't exist in page source抓取页面源中不存在的数据表
【发布时间】:2017-10-06 11:06:55
【问题描述】:
我想在这个website上抓取数据表。
我检查了该页面的页面源,该表在页面源中不存在。
然后我在刷新网站的时候查看了网络信息,似乎是通过向这个url发送POST请求获取数据表:
http://datacenter.mep.gov.cn:8099/ths-report/report!list.action
然后我尝试发送 POST 请求,但没有收到任何状态为 500 的请求。
我想知道有没有用 R 来刮掉这张桌子?
谢谢。
【问题讨论】:
标签:
r
post
web-scraping
web-crawler
【解决方案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