在 R 中抓取帮助内容

Scrape help content in R

是否可以抓取帮助内容并在控制台中打印出来?

例如我想找到有关 barplot 的帮助,找到一个句子,然后将其打印到控制台。

我在网上找不到任何关于它的信息,因此我希望得到你的帮助。

我知道这是一个一般性问题。如果我可以改进它,请随时通知我。

我可以给你举个例子。您可以使用 rdocumentation 找到您需要的 ?help 页面,然后使用 rvest 来抓取其内容。

举个例子,假设我们想抓取这个 page 并得到短语“Creates a bar plot with vertical or horizo​​ntal bars”。

library(tidyverse)
library(rvest)

url <- "https://www.rdocumentation.org/packages/graphics/versions/3.5.1/topics/barplot"
webpage <- read_html(url)

webpage %>% 
  html_nodes("div.container") %>% # <div class="container">
  html_node("section") %>%  # <section>
  "[["(2) %>% 
  html_nodes("p") %>% 
  "["(2) %>% 
  html_text() %>% 
  str_trim() %>% 
  unlist()
  # gives:
  [1] "Creates a bar plot with vertical or horizontal bars."

使用html_nodes功能很重要,需要了解一下html

如果您在浏览器中检查页面(右侧 click/inspect),您将访问其 html 代码。然后你就可以通过查看 tags.

找到你需要抓取的内容

在我的示例中,标签是 div class="container"section 和第二个 p

这里是guide to rvest.