用rvest抓取时对法语unicode进行修改 [英] Mangling of French unicode when webscraping with rvest

查看:100
本文介绍了用rvest抓取时对法语unicode进行修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rvest软件包抓取法语网站。

I'm looking at scraping a French website using the rvest package.

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


我希望看到:

I expect to see:

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)


看到变音符号被扭曲(请参阅下面的第3行):

Instead, I see the diacritic characters mangled (see line 3 below):

"Aloxe-Corton (Appellation Village, VIGNOBLE DE LA CÃ\u0094TE DE BEAUNE)"        
"Auxey-Duresses (Appellation Village, VIGNOBLE DE LA CÃ\u0094TE DE BEAUNE)"      
"Bâtard-Montrachet (Appellation Grand Cru, VIGNOBLE DE LA CÃ\u0094TE DE BEAUNE)"

页面的源html显示它是​​用utf-8编码的。在html_text()上使用guess_encoding(),它也建议使用utf-8(置信度为1.00),或建议Windows-1252置信度为0.73。将编码更改为Windows-1252并没有帮助:

The source html of the page shows it's encoded in utf-8. Using guess_encoding() on the html_text(), it suggests utf-8 as well (1.00 confidence), or windows-1252 with 0.73 confidence. Changing the encoding to windows-1252 doesn't help matters:

"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)上尝试了相同的代码:

I tried the same code on a different French website (also encoded utf-8):

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

现在我得到了变音符号,等等:

Now I get the diacritics etc:

[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. »"

关于我第一个网站出问题的任何建议?或如何解决? / p>

Any suggestions on where I am going wrong with the first website? Or how to fix?

推荐答案

这是一个怪异的网站,并非所有有效的UTF-8:

This is a weird website. It is not all valid UTF-8:

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"   

这些外观l ike注释中的JavaScript代码。我怀疑 read_html 意识到该页面不是全部有效的UTF-8,并且将编码解释为Windows-1252或某些其他8位编码方案。

These look like comments in the JavaScript code. I suspect that read_html realizes that the page is not all valid UTF-8 and interprets the encoding to be Windows-1252 or some other 8-bit coding scheme.

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

You could try to work around this by removing the offending JS segments:

content <- paste(lines[utf8::utf8_valid(lines)], collapse = "\n")
content %>% read_html() %>% html_nodes('#resultatListeAppellation .lien') %>% html_text()

这给出了预期的输出。

这篇关于用rvest抓取时对法语unicode进行修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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