R: rvest - is not proper UTF-8, indicate encoding?

时间:2015-09-29 00:40:10

标签: r encoding utf-8 web-scraping rvest

I'm trying out the "new" Rvest package from Hadley Wickham.

I've used it in the past, so I'd expected that everything run smoothly.

However, I keep seen this error:

> 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]

As you see in the code, I've use encoding: ISO-8859-1. Before that I was using "UTF-8", but function guess_encoding(page_source[[1]]) says that the encoding is: ISO-8859-1. I've tried with all the options provided by guess_encoding but none worked.

What is the problem?

My code:

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")

UPDATE 1

I've googled for "How to now the encoding of a web page?".

Found out this Makrup Validation Tool from W3C, but It wasn't of great help:

http://validator.w3.org/check?uri=http://www.w3.org/2003/10/empty/emptydoc.html

1 个答案:

答案 0 :(得分:1)

查看页面源,他们声称使用的是UTF-8编码:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

所以,问题是,他们是否真的使用了我们需要担心的不同编码,或者我们可以转换为utf-8,猜测任何错误都可以忽略不计?

如果你对快速而肮脏的方法以及一些潜在的mojibake感到满意,你可以使用iconv强制使用utf-8:

TV_Audio_Video_Marca <- read_html(iconv(page_source[[1]], to = "UTF-8"), encoding = "utf8")

一般来说,这是一个坏主意 - 最好指定它的编码。在这种情况下,可能错误是他们的,所以这种快速而肮脏的方法可能没问题。