全部
我一直在研究用于谷歌搜索数据挖掘的R程序。
到目前为止,我的代码运行良好,除了繁体中文编码问题。我在linux环境下工作...
Google <- function(input)
{
require(XML)
require(stringr)
require(RCurl)
hits <- GoogleHits(input)
if( hits >= 1000){
start.num = seq(0,900,100)
}else if(hits < 1000){
start.num = seq(0,hits,100)
}
for(i in 1:length(start.num)){
start = start.num[i]
url <-paste("https://www.google.com/search?as_epq=",input,
"&as_occt=title&num=100&ie=UTF-8&start=",start, sep = "")
CAINFO = paste(system.file(package="RCurl"), "/CurlSSL/ca-bundle.crt", sep = "")
script <- getURL(url, followlocation = TRUE, cainfo = CAINFO)
# using htmlParse() to re-organize the whole structure
# the Chinese encoding shows quite well here
# (but the structure is not a vector)
doc <- htmlParse(script)
# Wanna extract out the searched keyword
# which is tagged by "<b>keyword</b>"
# here, I take the keyword "統計" for example
extract <- str_extract_all(html_str, "<b>統計</b>")
# here is the problem... which extract only takes a vector as an argument
# so below will return an error
print (extract)
}
}
因此,我遇到的问题都包含在评论中。
1) 如果不使用htmlParse(),提取的数据无法呈现为可识别的汉字
2)如果我尝试将数据转换为向量(通过应用script <- lapply(url, getURL)),虽然可以使用str_extract_all()方法,但出现编码问题......
另外,这里的中文我指的是繁体中文
任何意见或建议都非常感谢!
提前致谢。