How to view all xml_nodeset class object (output of rvest::html_nodes) in R?

北城以北 提交于 2021-02-17 06:25:06

问题


If we create an object of class xml_nodes using rvest's html_nodes(), how can we view all of the output in the R console

Example

library(rvest)
library(dplyr)

# Generate some sample html
a <- rep("<p></p>", 200) %>% paste0(collapse="")

a <- a %>% read_html %>% html_nodes("p")

a %>% length
# 200

# But only see first 20 (want to see all)


回答1:


You can type in print.AsIs(a) to print the entire list.

(Truncated for brevity.)

library(rvest)
#> Loading required package: xml2
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# Setup data
a <- rep("<p></p>", 200) %>% paste0(collapse="")
a <- a %>% read_html %>% html_nodes("p")

# Print everything
print.AsIs(a)
#> [[1]]
#> {html_node}
#> <p>
#> 
#> [[2]]
#> {html_node}
#> <p>
#> 
#> [[3]]
#> {html_node}
#> <p>
...
#> 
#> [[197]]
#> {html_node}
#> <p>
#> 
#> [[198]]
#> {html_node}
#> <p>
#> 
#> [[199]]
#> {html_node}
#> <p>
#> 
#> [[200]]
#> {html_node}
#> <p>
#> 
#> attr(,"class")
#> [1] "xml_nodeset"

Created on 2019-11-13 by the reprex package (v0.3.0)

You can also alter the print() function to print everything in a condensed way.

(Also truncated for brevity.)

# Alternative condensed printing
print(a, max_n = 200)
#> {xml_nodeset (200)}
#>   [1] <p></p>\n
#>   [2] <p></p>\n
#>   [3] <p></p>\n
...
#> [198] <p></p>\n
#> [199] <p></p>\n
#> [200] <p></p>

Created on 2019-11-13 by the reprex package (v0.3.0)



来源:https://stackoverflow.com/questions/58848804/how-to-view-all-xml-nodeset-class-object-output-of-rvesthtml-nodes-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!