当使用 rvest 进行抓取时,预计 html_node 不会出现
When scraping with rvest expected html_node not appearing
ITTO 网站生成 table 木材产品,并在提交查询后(在同一页面上)直接在搜索表单下流动。使用我从 Chrome 的 SelectorGadget 获得的信息,我希望 table 显示为 css 元素 "td"。使用 rvest 抓取 2014 年阿尔巴尼亚的信息...
library(rvest)
session <- html_session("http://www.itto.int/annual_review_output/?mode=searchdata")
form <- html_form(session)[[2]]
form <- set_values(form, "countries[]" = "8", "products[]" = "1" ,"flows[]" = "1", "years[]" = "2014")
query <- submit_form(session, form, submit = NULL)
page <- read_html(query) %>% html_nodes("td")
page
这导致 table "td" 不存在:
{xml_nodeset (0)}
使用 html_nodes() 检查页面的其他元素表明 submit_form() 以其他方式按预期执行。
所以我的问题是预期的 table 在哪里?
可能更容易(在长 运行 中)抓取 select 框选项并直接提供 POST
调用:
library(httr)
library(rvest)
res <- POST(url = "http://www.itto.int/annual_review_output/?mode=searchdata",
body = list(`countries[]` = "76",
`products[]` = "1", `flows[]` = "1",
`years[]` = "2014"),
encode = "form")
pg <- content(res, as="parsed")
html_nodes(pg, "td")
## {xml_nodeset (7)}
## [1] <td>Brazil</td>
## [2] <td>Ind. roundwood</td>
## [3] <td>Exports Quantity</td>
## [4] <td>1000 m3</td>
## [5] <td>2014</td>
## [6] <td style="text-align:right;">204.59</td>
## [7] <td>I</td>
ITTO 网站生成 table 木材产品,并在提交查询后(在同一页面上)直接在搜索表单下流动。使用我从 Chrome 的 SelectorGadget 获得的信息,我希望 table 显示为 css 元素 "td"。使用 rvest 抓取 2014 年阿尔巴尼亚的信息...
library(rvest)
session <- html_session("http://www.itto.int/annual_review_output/?mode=searchdata")
form <- html_form(session)[[2]]
form <- set_values(form, "countries[]" = "8", "products[]" = "1" ,"flows[]" = "1", "years[]" = "2014")
query <- submit_form(session, form, submit = NULL)
page <- read_html(query) %>% html_nodes("td")
page
这导致 table "td" 不存在:
{xml_nodeset (0)}
使用 html_nodes() 检查页面的其他元素表明 submit_form() 以其他方式按预期执行。
所以我的问题是预期的 table 在哪里?
可能更容易(在长 运行 中)抓取 select 框选项并直接提供 POST
调用:
library(httr)
library(rvest)
res <- POST(url = "http://www.itto.int/annual_review_output/?mode=searchdata",
body = list(`countries[]` = "76",
`products[]` = "1", `flows[]` = "1",
`years[]` = "2014"),
encode = "form")
pg <- content(res, as="parsed")
html_nodes(pg, "td")
## {xml_nodeset (7)}
## [1] <td>Brazil</td>
## [2] <td>Ind. roundwood</td>
## [3] <td>Exports Quantity</td>
## [4] <td>1000 m3</td>
## [5] <td>2014</td>
## [6] <td style="text-align:right;">204.59</td>
## [7] <td>I</td>