https://github.com/cran/XML
Raw File
Tip revision: 89e6494d4b3572d033b44942a0b324c221773f47 authored by CRAN Team on 28 October 2022, 09:15:39 UTC
version 3.99-0.12
Tip revision: 89e6494
HTMLText.R
# The following illustrates how we can get the text
# Michael Conklin.

# Also see ./foo.html as an example with javascript content
# and a pseudo/fake css node.

doc = htmlParse("http://www.omegahat.net/")
txt = xpathSApply(doc, "//body//text()", xmlValue)

#The result is a character vector that contains all the text.

#By limiting the nodes to the body, we avoid the content in <head>
#such as inlined JavaScript or CSS.

#It is also possible that a document may have <script> elements
#in the document containing JavaScript that you don't want.
#You can omit these

  txt = xpathSApply(doc, "//body//text()[not(ancestor::script)]", xmlValue)

# And if there were other elements we wanted to ignore, then you could use

 txt = xpathSApply(doc,
                   "//body//text()[not(ancestor::script) and not(ancestor::otherElement)]",
                   xmlValue)



back to top