使用UTF-8时最差的编码错误 [英] R rvest encoding errors with UTF-8

查看:90
本文介绍了使用UTF-8时最差的编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Wikipedia获取表.文件的来源是UTF-8:

I'm trying to get this table from Wikipedia. The source of the file clamis it's UTF-8:

> <!DOCTYPE html> <html lang="en" dir="ltr" class="client-nojs"> <head>
> <meta charset="UTF-8"/> <title>List of cities in Colombia - Wikipedia,
> the free encyclopedia</title>
> ...

但是,当我尝试使用rvest来获取表格时,它会在应该加重(标准西班牙文)的á,é等字符的地方显示奇怪的字符. 这是我尝试过的:

However, when I try to get the table with rvest it shows weird characters where there should be accented (standard spanish) ones like á, é, etc. This is what I attempted:

theurl <- "https://en.wikipedia.org/wiki/List_of_cities_in_Colombia"
file <- read_html(theurl, encoding = "UTF-8")
tables <- html_nodes(file, "table")
pop <- html_table(tables[[2]])
head(pop)

##   No.         City Population         Department
## 1   1      Bogotá  6.840.116       Cundinamarca
## 2   2    Medellín  2.214.494          Antioquia
## 3   3         Cali  2.119.908    Valle del Cauca
## 4   4 Barranquilla  1.146.359         Atlántico
## 5   5    Cartagena    892.545           Bolívar
## 6   6      Cúcuta    587.676 Norte de Santander

如其他SO问题中所建议的,我尝试使用以下方法修复编码:

I have attempted to repair the encoding, as suggested in other SO questions, with:

repair_encoding(pop)

## Best guess: UTF-8 (100% confident)
## Error in stringi::stri_conv(x, from = from) : 
##   all elements in `str` should be a raw vectors

我已经测试了几种不同的编码(latin1和guess_encoding()提供的其他编码,但是所有编码都会产生类似的错误结果.

I've tested several different encodings (latin1, and others provided by guess_encoding(), but all of them produce similarly incorrect results.

如何正确加载此表?

推荐答案

似乎您必须在字符向量上使用repair_encoding,而不是整个数据帧...

It looks like you have to use repair_encoding on a character vector, not an entire dataframe...

> repair_encoding(head(pop[,2]))
Best guess: UTF-8 (80% confident)
[1] "Bogotá"       "Medellín"     "Cali"         "Barranquilla"
[5] "Cartagena"    "Cúcuta"      

这篇关于使用UTF-8时最差的编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆