r - 抓取页面源中不存在的数据表

标签 r post web-scraping web-crawler

我想在这个 website 上抓取数据表.

我检查了此页面的页面源,页面源中不存在该表。

然后我在刷新网站时查看了网络信息,数据表似乎是通过向这个url发送POST请求获得的:

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

然后我尝试发送 POST 请求,但状态 500 一无所获。

我想知道是否可以通过使用 R 来删除这张表?

谢谢。

最佳答案

好侦探!

它正在制作 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 

关于r - 抓取页面源中不存在的数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46604365/

相关文章:

r - 自定义轴标签

r - 在 nPlot 中为 discreteBarChart 更改颜色

R:ggplot2 条形图和误差条

javascript - 将数据发布到服务器 Angular $http post

php - 抓取网页内容

python - 使用 python 3 抓取需要登录的网站

r - html 输出上的 `str_replace_all()` (来自 `huxtable()` )

javascript - $http post在谷歌浏览器上返回400

php - 使用后清除 session 变量

python - Scrapy - 遇到重复项时停止爬虫