【发布时间】:2015-12-26 08:07:47
【问题描述】:
我正在试用 Hadley Wickham 的“新”Rvest 包。
我过去使用过它,所以我希望一切顺利。
但是,我一直看到这个错误:
> TV_Audio_Video_Marca <- read_html(page_source[[1]], encoding = "ISO-8859-1")
Error: Input is not proper UTF-8, indicate encoding !
Bytes: 0xCD 0x20 0x53 0x2E [9]
正如您在代码中看到的,我使用了编码:ISO-8859-1。在此之前我使用“UTF-8”,但函数guess_encoding(page_source[[1]]) 表示编码为:ISO-8859-1。我尝试了guess_encoding 提供的所有选项,但都没有奏效。
有什么问题?
我的代码:
library(RSelenium)
library(rvest)
#start RSelenium
checkForServer()
startServer()
remDr <- remoteDriver()
remDr$open()
#navigate to your page
remDr$navigate("http://www.linio.com.pe/tv-audio-y-video/televisores/")
#scroll down 5 times, waiting for the page to load at each time
for(i in 1:5){
remDr$executeScript(paste("scroll(0,",i*10000,");"))
Sys.sleep(3)
}
#get the page html
page_source<-remDr$getPageSource()
#parse it
TV_Audio_Video_Marca <- read_html(page_source[[1]], encoding = "UTF-16LE")
更新 1
我在 Google 上搜索过“现在如何对网页进行编码?”。
从 W3C 找到了这个 Makrup 验证工具,但没有太大帮助:
http://validator.w3.org/check?uri=http://www.w3.org/2003/10/empty/emptydoc.html
【问题讨论】:
-
试试:
TV_Audio_Video_Marca <- read_html(iconv(page_source[[1]], to="UTF-8"), encoding = "utf8") -
有效,请发布一个完整的答案并解释 iconv(),它在这种情况下使用。文档没有提到这个“技巧”,或者是这样吗?
标签: r encoding utf-8 web-scraping rvest