【问题标题】:When scraping with rvest expected html_node not appearing使用 rvest 进行抓取时,预期的 html_node 不会出现
【发布时间】:2016-07-12 18:25:59
【问题描述】:

一旦提交查询(在同一页面上),ITTO 网站会生成一个木材产品表并直接在搜索表单下流动。使用我从 Chrome 的 SelectorGadget 获得的信息,我希望表格显示为 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 

这导致表“td”不存在:

{xml_nodeset (0)}

使用 html_nodes() 检查页面的其他元素表明 submit_form() 按预期执行。

所以我的问题是预期的桌子在哪里?

【问题讨论】:

    标签: html r web-scraping rvest


    【解决方案1】:

    刮取选择框选项并直接提供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>
    

    【讨论】:

    • 感谢指导!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2017-03-05
    • 1970-01-01
    • 2018-10-13
    • 2019-10-11
    相关资源
    最近更新 更多