使用 rvest,如何从 submit_form() 返回的对象中提取 html 内容
With rvest, how to extract html contents from the object returned by submit_form()
我正在尝试从 pems.dot.ca.gov 下载一些流量数据,跟随 。
rm(list=ls())
library(rvest)
library(xml2)
library(httr)
url <- "http://pems.dot.ca.gov/?report_form=1&dnode=tmgs&content=tmg_volumes&tab=tmg_vol_ts&export=&tmg_station_id=74250&s_time_id=1369094400&s_time_id_f=05%2F21%2F2013&e_time_id=1371772740&e_time_id_f=06%2F20%2F2013&tod=all&tod_from=0&tod_to=0&dow_5=on&dow_6=on&tmg_sub_id=all&q=obs_flow&gn=hour&html.x=34&html.y=8"
pgsession <- html_session(url)
pgform <-html_form(pgsession)[[1]]
filled_form <- set_values(pgform,
'username' = 'omitted',
'password' = 'omitted')
resp = submit_form(pgsession, filled_form)
resp_2 = resp$response
cont = resp_2$content
我检查了这些项目的class()
,发现resp是'session',resp_2是'response',cont是'raw' .我的问题是:如何正确提取 html 内容,以便我可以继续使用 XPath 从该页面中挑选出我想要的实际数据?我的直觉是我应该解析 resp_2 这是一个响应,但我就是无法让它工作。非常感谢您的帮助!
您需要 httr::content
,它将响应解析为内容,在本例中为 HTML,可以使用 rvest
:
轻松解析
resp_2 %>% content()
## {xml_document}
## <html style="height: 100%">
## [1] <head>\n <!-- public -->\n <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/ ## ...
## [2] <body class="yui-skin-sam public">\n <div id="maincontainer" style="height: 100%">\n\n \n\ ## ...
应该这样做:
pg <- content(resp$response)
html_nodes(pg, "table.inlayTable") %>%
html_table() -> tab
head(tab[[1]])
## X1 X2 X3 X4
## 1 Data Quality Data Quality
## 2 Hour 8 Lanes % Observed % Estimated
## 3 05/24/2013 00:00 1,311 50 0
## 4 05/24/2013 01:00 729 50 0
## 5 05/24/2013 02:00 399 50 0
## 6 05/24/2013 03:00 487 50 0
(您显然需要修改列名)
我正在尝试从 pems.dot.ca.gov 下载一些流量数据,跟随
rm(list=ls())
library(rvest)
library(xml2)
library(httr)
url <- "http://pems.dot.ca.gov/?report_form=1&dnode=tmgs&content=tmg_volumes&tab=tmg_vol_ts&export=&tmg_station_id=74250&s_time_id=1369094400&s_time_id_f=05%2F21%2F2013&e_time_id=1371772740&e_time_id_f=06%2F20%2F2013&tod=all&tod_from=0&tod_to=0&dow_5=on&dow_6=on&tmg_sub_id=all&q=obs_flow&gn=hour&html.x=34&html.y=8"
pgsession <- html_session(url)
pgform <-html_form(pgsession)[[1]]
filled_form <- set_values(pgform,
'username' = 'omitted',
'password' = 'omitted')
resp = submit_form(pgsession, filled_form)
resp_2 = resp$response
cont = resp_2$content
我检查了这些项目的class()
,发现resp是'session',resp_2是'response',cont是'raw' .我的问题是:如何正确提取 html 内容,以便我可以继续使用 XPath 从该页面中挑选出我想要的实际数据?我的直觉是我应该解析 resp_2 这是一个响应,但我就是无法让它工作。非常感谢您的帮助!
您需要 httr::content
,它将响应解析为内容,在本例中为 HTML,可以使用 rvest
:
resp_2 %>% content()
## {xml_document}
## <html style="height: 100%">
## [1] <head>\n <!-- public -->\n <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/ ## ...
## [2] <body class="yui-skin-sam public">\n <div id="maincontainer" style="height: 100%">\n\n \n\ ## ...
应该这样做:
pg <- content(resp$response)
html_nodes(pg, "table.inlayTable") %>%
html_table() -> tab
head(tab[[1]])
## X1 X2 X3 X4
## 1 Data Quality Data Quality
## 2 Hour 8 Lanes % Observed % Estimated
## 3 05/24/2013 00:00 1,311 50 0
## 4 05/24/2013 01:00 729 50 0
## 5 05/24/2013 02:00 399 50 0
## 6 05/24/2013 03:00 487 50 0
(您显然需要修改列名)