当使用rvest进行WebScrap时,法语unicode的混乱

当使用rvest进行WebScrap时,法语unicode的混乱,r,unicode,encoding,utf-8,rvest,R,Unicode,Encoding,Utf 8,Rvest,我正在看一个使用rvest软件包的法国网站 library(rvest) url <- "https://www.vins-bourgogne.fr/nos-vins-nos-terroirs/tous-les-bourgognes/toutes-les-appellations-de-bourgogne-a-votre-portee,2378,9172.html?&args=Y29tcF9pZD0xMzg2JmFjdGlvbj12aWV3RnVsbExpc3RlJmlkPSZ8"

我正在看一个使用rvest软件包的法国网站

library(rvest)
url <- "https://www.vins-bourgogne.fr/nos-vins-nos-terroirs/tous-les-bourgognes/toutes-les-appellations-de-bourgogne-a-votre-portee,2378,9172.html?&args=Y29tcF9pZD0xMzg2JmFjdGlvbj12aWV3RnVsbExpc3RlJmlkPSZ8"
s <- read_html(url)
s %>% html_nodes('#resultatListeAppellation .lien') %>% html_text()
相反,我看到变音字符被弄乱了(见下面第3行):

页面的源html显示它是用utf-8编码的。在html_text()上使用guess_encoding(),它还建议使用utf-8(置信度为1.00),或者建议使用置信度为0.73的windows-1252。将编码更改为windows-1252无助于解决问题:

"Aloxe-Corton (Appellation Village, VIGNOBLE DE LA CÔTE DE BEAUNE)"                                                                                
"Auxey-Duresses (Appellation Village, VIGNOBLE DE LA CÔTE DE BEAUNE)"                                                                              
"Bâtard-Montrachet (Appellation Grand Cru, VIGNOBLE DE LA CÔTE DE BEAUNE)"
我在另一个法国网站上尝试了相同的代码(也编码为utf-8):


关于我第一个网站哪里出了问题,有什么建议吗?或者如何修复?

这是一个奇怪的网站。它不是所有有效的UTF-8:

lines <- readLines(url, warn = FALSE)
all(utf8::utf8_valid(lines))
#> [1] FALSE
这些看起来像JavaScript代码中的注释。我怀疑
read_html
意识到页面并非都是有效的UTF-8,并将编码解释为Windows-1252或其他一些8位编码方案

您可以尝试通过删除有问题的JS段来解决此问题:

content <- paste(lines[utf8::utf8_valid(lines)], collapse = "\n")
content %>% read_html() %>% html_nodes('#resultatListeAppellation .lien') %>% html_text()
content%read_html()%%>%html_节点('#resultatListAttachion.lien')%%>%html_文本()
这就给出了预期的输出

x <- read_html('http://www.lemonde.fr/disparitions/article/2017/12/06/johnny-hallyday-c-etait-notre-seule-rock-star-la-france-perd-son-icone-du-rock_5225507_3382.html')
x %>% html_nodes('.taille_courante+ p , .croix_blanche , .tt2') %>% html_text()
[1] "Johnny Hallyday : « C’était notre seule rock star », « La France perd son icône du rock »"                                                                                                                                                                                           
[2] "« Comme toute la France, mon cœur est brisé, a déclaré à l’Agence France-Presse (AFP) la chanteuse Sylvie Vartan, qui fut la première épouse de Johnny Hallyday, et mère de leur fils, David, né en 1966. J’ai perdu l’amour de ma jeunesse et rien ne pourra jamais le remplacer. »"
lines <- readLines(url, warn = FALSE)
all(utf8::utf8_valid(lines))
#> [1] FALSE
lines[!utf8::utf8_valid(lines)]
#> [1] "// on supprime l'\xe9ventuel cookie"                                                                             
#> [2] "//Ouverture et fermeture de l'encart r\xe9saux sociaux lors d'un clic sur le bouton"                             
#> [3] "//Cr\xe9ation de l'iframe facebook \xe0 la premi\xe8re ouverture de l'encart pour qu'elle fasse la bonne largeur"
#> [4] "//fermeture de l'encart r\xe9saux sociaux lors d'un clic ailleurs sur la page"   
content <- paste(lines[utf8::utf8_valid(lines)], collapse = "\n")
content %>% read_html() %>% html_nodes('#resultatListeAppellation .lien') %>% html_text()