【问题标题】:Scrape help content in R在 R 中抓取帮助内容
【发布时间】:2018-10-09 07:04:58
【问题描述】:

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

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

我在网上找不到任何有关它的信息,因此我期待您的帮助。

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

【问题讨论】:

标签: r screen-scraping


【解决方案1】:

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

举个例子,假设我们想抓取这个page 并得到短语“创建带有垂直或水平条的条形图”。

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有所了解。

如果您在浏览器中检查页面(右键单击/检查),您将访问其html 代码。然后你就可以通过查看tags找到你需要刮的东西。

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

这里是guide to rvest

【讨论】:

  • 请告诉我在我的回答中可以改进的地方,而不是投反对票。像这样投反对票根本没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多