如何在 R 中查看所有 xml_nodeset 类对象(rvest::html_nodes 的输出)? [英] How to view all xml_nodeset class object (output of rvest::html_nodes) in R?

查看:42
本文介绍了如何在 R 中查看所有 xml_nodeset 类对象(rvest::html_nodes 的输出)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们使用 rvesthtml_nodes() 创建一个 xml_nodes 类的对象,我们如何查看所有输出R 控制台

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

示例

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)

推荐答案

您可以输入 print.AsIs(a) 来打印整个列表.

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"

reprex 包 (v0.3.0) 于 2019 年 11 月 13 日创建

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

您还可以更改 print() 函数以压缩方式打印所有内容.

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>

reprex 包 (v0.3.0) 于 2019 年 11 月 13 日创建

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

这篇关于如何在 R 中查看所有 xml_nodeset 类对象(rvest::html_nodes 的输出)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆