File: HTMLText.R

package info (click to toggle)
r-cran-xml 3.99-0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: ansic: 6,659; xml: 2,890; asm: 486; sh: 12; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 872 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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)