问题
I'm trying to download a vector of links, but I get an error message that I don't know what to do with. Code included, hoping someone has a workaround.
CODE:
library(RCurl)
library(XML)
url <- "http://www.etfs.bmo.com/bmo-etfs/"
url.parsed <- htmlParse(url)
links <- xpathSApply(url.parsed, "//table//td/a/@href")[-c(1:3)]
links <- paste0("http://www.etfs.bmo.com", links)
pages <- getURI(links)
ERROR MESSAGE:
Error in curlMultiPerform(multiHandle) :
embedded nul in string: ' \r\n </nobr>\r\n </td>\r\n\t\t\t </tr>\r\n\t\t\t \r\n\t\t\t\t\t \r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t \r\n\t\t\t\t \t<tr valign="top" >\r\n\t \t\t\t\t<td class="highlightText"><strong>Annualized Distribution Yield \r\n\t\t \t\t\t\r\n\t\t \t \t\t\t\t\r\n\t\t \t \t\t\t(Jul 07, 2016)\r\n\t\t \t \t\t\t\t\r\n\t\t \t \t\t\t \r\n\t\t \t\t\t\t\t<sup>1</sup></strong>\r\n\t\t \t\t\t\t</td>\r\n\t\t\t \t\t<td>\r\n \t\t<nobr>\r\n \t \t\t\r\n \t \t\t\t\r\n \t \t\t\t\r\n\t\t\t \t \t\t\t\t2.41%\r\n \t \t\t\t\r\n \t\t\t\t \r\n \t \t</nobr>\r\
回答1:
Ok, this took a while but I think i've figure it out.
It turns out that webpage is improperly encoded. It claims to be "ISO-8859-1", but on some pages there is trademark symbol encoded as \x99
which means it probably really is using the "Windows-1252" codepage. This symbol outside the normal ASCII range kicks off multi-byte character reading and the file quickly becomes messed up.
As far as I can tell, RCurl does not support this encoding natively. But you can still download the file as binary data and then convert it using iconv
which has more conversion options. This should work
raw <- lapply(links, getURLContent, binary=TRUE)
pages <- lapply(lapply(raw,readBin,"characer"),
iconv, from="WINDOWS-1252", to="UTF-8")
Now I tested this on my Mac. The exact from/to strings may vary by platform. Check the list from iconvlist()
for a possible replacement for the from=
value should this not work on your machine.
来源:https://stackoverflow.com/questions/25090225/error-in-curlmultiperformmultihandle-embedded-nul-in-string