用下载的函数(例如download.file)下载网页下来,然后读取出内容,用stringi扩展包中的stri_enc_detect探测出字符串的编码(例如GBK),然后用stri_encode将源编码的转换为UTF8,然后就可以正常处理了。
f <- tempfile()
download.file("http://dealer.autohome.com.cn/shanghai", f)
fchars <- readChar(f, file.info(f)$size)
stringi::stri_enc_detect(fchars)
# 发现文档是 GB18030 编码
futf8 <- stringi::stri_encode(fchars, "GB18030", "UTF8")
fhtml <- rvest::html(futf8)
fhtml