2014-12-02 5 views
0

웹 사이트에서 외국어 텍스트를 추출하려고합니다.RcURL/getURL이 브라우저와 다른 문자

require(RCurl) 
require(XML) 
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))) 
agent="Chrome 39.0.2171.71 (64-bit)" 
curl = getCurlHandle() 
curlSetOpt(cookiejar = 'cookies.txt' ,useragent = agent,followlocation = TRUE , autoreferer = TRUE , curl = curl) 

html <-getURL('http://164.100.47.132/LssNew/psearch/result12.aspx?dbsl=1008', maxredirs = as.integer(20), followlocation = TRUE, curl = curl) 
work <- htmlTreeParse(html, useInternal = TRUE) 
table <- xpathApply(work, "//table[@id = 'ctl00_ContPlaceHolderMain_DataList1' ]//font|//table[@id = 'ctl00_ContPlaceHolderMain_DataList1' ]//p", xmlValue) #this one captured some mess in 13 
table[[2]] 

콘솔 출력 문자의 첫 번째 무리가 ¸Ã\u0089Ã\u0092 iÃ\u0089{Ã\u0089xÃ\u0089 Ã\u008aºÃ\u0089Eònù®ú 나를 위해 나타납니다 : 다음 코드 (희망 자체 포함)이 문제를 시연 할 예정이다.

주 내가 실제 페이지 (http://bit.ly/1AcE9Gs)로 이동하여 페이지 소스를 확인하고 제 2 개구 <font 태그 찾기 (내 table에서 두 번째 목록 항목에 해당를, 또는 최초의 힌디어 문자에 가까운 요소를 검사)하는 경우 그 페이지 소스의 렌더링은 다음과 같습니다. ¸ÉÒ iÉ{ÉxÉ ÊºÉEònù®ú (nù¨Énù¨É): 이것이 내가 원하는 것입니다.

왜 이런 일이 발생할 수 있으며 어떻게 수정해야하는지 알 수 있습니다. R 또는 RcURL의 인코딩과 관련이 있습니까? 나는 getURL 문자가 이처럼 다르다는 것을 처음부터 알 수 있기 때문에 html 텍스트에서 xpathApply으로 넘어 가지 않아도됩니다.

MAC OSX 10.9.3, Chrome 브라우저 (실제 페이지보기 용), R 3.1.1을 사용하고 있습니다.

관심이 있다면, xpathApply 여기에 관련 질문을 참조하십시오 R and xpathApply -- removing duplicates from nested html tags

감사합니다! htmlParsegetURL

+0

소리가 인코딩 문제와 같습니다. 'Encoding (html)'은 무엇을 반환합니까? 'Encoding (table [[2]])'은 무엇을 반환합니까? – MrFlick

답변

1

추가 인코딩 옵션 :

require(RCurl) 
require(XML) 
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))) 
agent="Chrome 39.0.2171.71 (64-bit)" 
curl = getCurlHandle() 
curlSetOpt(cookiejar = 'cookies.txt' ,useragent = agent,followlocation = TRUE , autoreferer = TRUE , curl = curl) 

html <-getURL('http://164.100.47.132/LssNew/psearch/result12.aspx?dbsl=1008' 
       , maxredirs = as.integer(20), followlocation = TRUE, curl = curl 
       , .encoding = 'UTF-8') 
work <- htmlParse(html, encoding = 'UTF-8') 
table <- xpathApply(work, "//table[@id = 'ctl00_ContPlaceHolderMain_DataList1' ]//font|//table[@id = 'ctl00_ContPlaceHolderMain_DataList1' ]//p", xmlValue) #this one captured some mess in 13 
> table[[2]] 
[1] "¸ÉÒ iÉ{ÉxÉ ÊºÉEònù®ú (nù¨Énù¨É):\r\nºÉ¦ÉÉ{ÉÊiÉ ¨É½þÉänùªÉ, {ɽþ±Éä ÊnùxÉ ¨ÉèÆ ¤ÉÉä±É\r\n®ú½þÉ lÉÉ iÉÉä ¨ÉèÆxÉä =iiÉ®ú {ÉÚ´ÉÒÇ ¦ÉÉ®úiÉ Eòä\r\n+ÉiÉÆEò´ÉÉnù {É®ú =ºÉ ÊnùxÉ nùÉä {ɽþ±ÉÖ+ÉäÆ EòÉ =±±ÉäJÉ\r\nÊEòªÉÉ lÉÉ* +ÉVÉ ¦ÉÒ ¨ÉèÆ, ÊVÉºÉ EòÉ®úhÉ ºÉä +ÉiÉÆEò´ÉÉnù\r\n{ÉènùÉ ½þÖ+É, =ºÉEòä Ê´É¹ÉªÉ ¨ÉäÆ lÉÉäc÷É ºÉÉ =±±ÉäJÉ\r\nEò°üÆMÉÉ*" 
+0

네, 그게 그랬습니다 - 나는 getURL에서 인코딩하는 것이 엉망 이었지만 htmltreeparse에서는 그렇지 못했습니다 - 고마워요! – SOConnell

+0

도와 드리겠습니다 ... – jdharrison

0

여기 rvest를 사용하여 대안 구현입니다. 코드가 더 간단 할뿐만 아니라 인코딩과 관련하여 아무 것도 할 필요가 없습니다.

library("rvest") 
url <- "http://164.100.47.132/LssNew/psearch/result12.aspx?dbsl=1008" 

search <- html(url) 
search %>% 
    html_node("#ctl00_ContPlaceHolderMain_DataList1") %>% 
    html_nodes("font, p") %>% 
    html_text() %>% 
    .[[2]] 
#> [1] "¸ÉÒ iÉ{ÉxÉ ÊºÉEònù®ú (nù¨Énù¨É):\r\nºÉ¦ÉÉ{ÉÊiÉ ¨É½þÉänùªÉ, ...