[R] Extracting text from html code using the RCurl package.
mtmorgan at fhcrc.org
Tue Oct 7 17:57:19 CEST 2008
Hi Tony --
Tony Breyal <tony.breyal at googlemail.com> writes:
> Dear R-help,
> I want to download the text from a web page, however what i end up
> with is the html code. Is there some option that i am missing in the
> RCurl package? Or is there another way to achieve this? This is the
> code i am using:
>> html.file <- getURI(my.url, ssl.verifyhost = FALSE, ssl.verifypeer = FALSE, followlocation = TRUE)
> I thought perhaps the htmlTreeParse() function from the XML package
> might help, but I just don't know what to do next with it:
> Many thanks for any help you can provide,
Sounds like you're on the right track. One way is to parse the html
file into its 'internal' representation, and then use xpathApply to
extract relevant information (e.g., the third 'p' (paragraph) element
from the XML mark-up
> html = htmlTreeParse(getURL(my.url), useInternal=TRUE)
Opening and ending tag mismatch: td and font
Unexpected end tag : p
Unexpected end tag : form
> xpathApply(html, "//p", xmlValue)
 "You can subscribe to the list, or change your existing\r\n\t subscription, in the sections below.\r\n\t"
the 'xpath' is the path from the root of the document through various
nested tags to tags of the specified type. "//p", says 'start at the
root ('/') and look in all sub-nodes (that this '//') for an 'p'
tag. ?xpathApply. is a good starting place, as is
> Tony Breyal
> R version 2.7.2 (2008-08-25)
> LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.
> 1252;LC_MONETARY=English_United Kingdom.
> 1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252
> attached base packages:
>  stats graphics grDevices utils datasets methods
> other attached packages:
>  XML_1.94-0 RCurl_0.9-4
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109
Location: Arnold Building M2 B169
Phone: (206) 667-2793
More information about the R-help