式で XPath 1.0 の substring-before または -after を使用すると、後続の xmlValue 呼び出しでエラーが発生します。以下のコードは、XPath 式が httr では正常に機能するが、RCurl では機能しないことを示しています。
require(XML)
require(httr)
doc <- htmlTreeParse("http://www.cottonbledsoe.com/CM/Custom/TOCContactUs.asp", useInternal = TRUE)
(string <- xpathSApply(doc, "substring-before(//div[@id = 'contactInformation']//p, 'Phone')", xmlValue, trim = TRUE))
require(RCurl)
fetch <- GET("http://www.cottonbledsoe.com/CM/Custom/TOCContactUs.asp")
contents <- content(fetch)
locsnodes <- getNodeSet(contents, "//div[@id = 'contactInformation']//p")
sapply(locsnodes, xmlValue)
[1] "500 West Illinois, Suite 300\r\n Midland, Texas 79701\r\n Phone: 432-897-1440\r\n Toll Free: 866-721-6665\r\n Fax: 432-682-3672"
上記のコードは正常に動作しますが、substring-before を使用して、次のように結果をクリーンアップしたいと考えています。
[1] "500 West Illinois, Suite 300\r\n Midland, Texas 79701\r\n "
locsnodes <- getNodeSet(contents, "substring-before(//div[@id = 'contactInformation']//p, 'Phone')")
sapply(locsnodes, xmlValue)
Error in UseMethod("xmlValue") :
no applicable method for 'xmlValue' applied to an object of class "character"
substring-
RCurl は、後で使用するより複雑な操作のために選択されたパッケージであるため、RCurlを使用するにはどうすればよいですか?
ガイダンスに感謝します(または、私が望むものを達成するためのより良い方法