Skip to contents

Extracts the paragraphs of the main content of a page, each with the last heading above it. Use it when the page is known and a selector can target its body. read_html_blocks() reads a whole saved file instead.

Usage

html_paragraphs(page, body, exclude = NULL)

Arguments

page

An xml2 document. Elements matching exclude are removed from it.

body

Character. CSS selector of the main content.

exclude

Character or NULL. CSS selector of the elements to drop (share buttons, footnotes...).

Value

A data.table with paragraph, section and text.

Details

Nested matches (a <p> inside a <li>) would duplicate text, so that only the innermost text-bearing elements are kept.

Examples

page <- xml2::read_html(
  "<main><h2>Intro</h2><p>First.</p><ul><li>Item.</li></ul></main>"
)
html_paragraphs(page, "main")
#>    paragraph section   text
#>        <int>  <char> <char>
#> 1:         1   Intro First.
#> 2:         2   Intro  Item.