cssApply: Apply a function to elements identified by a CSS path.

Description Usage Arguments Value Examples

View source: R/cssApply.R

Description

This function is a wrapper function for xpathSApply. It selects nodes inside an html document corresponding to a CSS path and then applies an arbitrary function to theses nodes. It is useful to extract data frome an html page.

Usage

1
cssApply(doc, path, fun, ...)

Arguments

doc

An html document parsed with htmlParse.

path

CSS path

fun

Function to extract data from the selected nodes. See cssExtract

...

Parameters passed to "fun"

Value

The result may differ based on the extarction function used. It will generally be a character or numeric vector with length equal to the number of nodes selected.

If no element in the html document corresponds to the path provided, the function will return an empty list.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
doc <- "<html>
<head></head>
  <body>
  <div id='character1' class='character'>
  <span class='name'>Mike</span>
  <span class='level digit'>10</span>
  </div>
  <div id='character2' class='character'>
  <span class='name'>Stan</span>
  </div>
  </body>
  </html>"

doc <- htmlParse(doc)

# Names of the characters
cssApply(doc, ".character>.name", cssCharacter)

# Name of character1
cssApply(doc, "#character1>.name", cssCharacter)

cuche27/CSS documentation built on Oct. 20, 2019, 1:09 a.m.