如何用R转换网页抓取中的特殊符号?

时间:2013-07-11 07:38:43

标签: r

我正在学习如何使用XMLRCurl软件包抓取网页。除了一件事,一切顺利。像ö或č这样的特殊字符以不同的方式读入R.例如,í以ÃÂ的形式读入。我假设后者是第一种HTML编码。

我一直在寻找转换这些字符的方法,但我还没找到。我相信其他人也偶然发现了这个问题,我怀疑必须有某种功能来转换这些字符。有谁知道解决方案?提前谢谢。

以下是代码示例,抱歉我之前没有提供。

library(XML)
url <-   'http://en.wikipedia.org/wiki/2000_Wimbledon_Championships_%E2%80%93_Men%27s_Singles'
tables <- readHTMLTable(url)
Sec <- tables[[6]]
pl1R1 <- unlist(strsplit(as.character(Sec[,2]), ' '))[seq(2,32, 4)]
enc2utf8(pl1R1) # does not seem to work

1 个答案:

答案 0 :(得分:0)

首先尝试解析它,同时指定编码,然后阅读表格,如下所示:readHTMLTable and UTF-8 encoding

一个例子可能是:

library(XML)
url <- "http://en.wikipedia.org/wiki/2000_Wimbledon_Championships_%E2%80%93_Men%27s_Singles"
doc <- htmlParse(url, encoding = "UTF-8") #this will preserve characters
tables <- as.data.frame(readHTMLTable(doc, stringsAsFactors = FALSE))
Sec <- tables[[6]]
#not sure what you're trying to do here though
pl1R1 <- unlist(strsplit(as.character(Sec[,2]), ' '))[seq(2,32, 4)]