在r中使用htmlparse的拉丁字符的问题
问题描述:
我在尝试使用葡萄牙语对web页面进行webscrape时存在一些编码问题。这是我的代码:在r中使用htmlparse的拉丁字符的问题
library("RCurl")
library("XML")
html = getURL("http://sei.cade.gov.br/sei/institucional/pesquisa/documento_consulta_externa.php?u0r2HDE7WIdiBH3O1y0Dr6krqmN-VVCNjJtZWrdX1mgt3CiIC_RM90F01GwwNk20muowNXaYKrI2Ob8UQUkAoA,,")
par = htmlParse(html)
x = xpathSApply(par, "//strong", xmlValue)[1]
print(x)
[1] "NOTA TÉCNICA Nº 58/2017/CGAA6/SGA2/SG/CADE"
我已经尝试了一些事情,比如增加encoding="latin1"
和encoding="UTF-8"
到htmlParse
,并添加.encoding="latin"
和.encoding="UTF-8"
到getURL
。
我的系统似乎被设置到正确的位置,为Sys.getlocale()给我
Sys.getlocale()
[1] "LC_COLLATE=Portuguese_Brazil.1252;LC_CTYPE=Portuguese_Brazil.1252;LC_MONETARY=Portuguese_Brazil.1252;LC_NUMERIC=C;LC_TIME=Portuguese_Brazil.1252"
我出出主意这里,并希望得到任何帮助。
答
我能够使用你的代码加上一个额外的工作。
## Your code
library("RCurl")
library("XML")
html = getURL("http://sei.cade.gov.br/sei/institucional/pesquisa/documento_consulta_externa.php?u0r2HDE7WIdiBH3O1y0Dr6krqmN-VVCNjJtZWrdX1mgt3CiIC_RM90F01GwwNk20muowNXaYKrI2Ob8UQUkAoA,,")
par = htmlParse(html)
x = xpathSApply(par, "//strong", xmlValue)[1]
## Addition
x2 = iconv(x, from="UTF-8", to="latin1")
print(x2)
"NOTA TÉCNICA Nº 58/2017/CGAA6/SGA2/SG/CADE"
多谢,这对我的作品! (迄今为止)这是唯一给我这个特定问题的页面,这很有趣。 –