1

全部

我一直在研究用于谷歌搜索数据挖掘的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()方法,但出现编码问题......

另外,这里的中文我指的是繁体中文

任何意见或建议都非常感谢!
提前致谢。

4

1 回答 1

2

我发现了错误!我!
所以我是来回答这个问题的。

问题是给定url 链接中的参数!

由于中文的编码是UTF-8,
所以有两个参数是必然需要的,
oe=UTF-8&ie=UTF-8!!
(在谷歌的开发者网站上,
它说oe=UTF-8不需要指定,
这就是我跳过这部分的原因......)

于 2012-05-22T13:19:59.687 回答