【发布时间】:2017-03-28 12:40:29
【问题描述】:
我正在尝试从 R 中的西班牙语源中提取文本,但遇到了字符编码问题,该问题无法通过在 htmlParse 中明确指定编码来解决,建议 here。
library(XML)
library(httr)
url <- "http://www3.hcdn.gov.ar//folio-cgi-bin/om_isapi.dll?E1=&E11=&E12=&E13=&E14=&E15=&E16=&E17=&E18=&E2=&E3=&E5=ley&E6=&E7=&E9=&headingswithhits=on&infobase=proy.nfo&querytemplate=Consulta%20de%20Proyectos%20Parlamentarios&record={4EBB}&recordswithhits=on&softpage=Document42&submit=ejecutar%20"
doc <- htmlParse(rawToChar(GET(url)$content),encoding="windows-1252")
text <- xpathSApply(doc, "//text()[not(ancestor::script)][not(ancestor::style)][not(ancestor::noscript)][not(ancestor::form)]", xmlValue)
text[77]
第 77 个元素(包括重音 i)包含有问题的字符。第四行有一些额外的障碍,我必须跳过才能阅读此源代码。该文档本身声称在“windows-1252”中编码。指定“latin1”和我尝试过的其他几种编码也好不到哪里去。在我的实际应用程序中,我已经下载了许多这些文件并正在使用readLines在本地读取它们......并且我可以告诉在将文件读入R后不存在错误,所以问题必须在@987654325 @。此外,仅仅接受编码错误并在事后纠正它似乎不是一种选择,因为如果我尝试将它们复制并粘贴回脚本中,R 甚至无法识别它吐出的字符。
【问题讨论】:
标签: r character-encoding web-scraping xml-parsing