Skip to content

Wrong encoding detection #25

Closed
Closed

Description

@artemklevtsov

Hi.

rvest::html("http://winrus.com/cpage_r.htm")

Return a broken symbols but

XML::htmlParse("http://winrus.com/cpage_r.htm")

works without any additional actions.

Activity

  1. hadley commented on Nov 24, 2014

    @hadley
    Member

    Hmmm, that's a weird one - I can't even figure out what the encoding is supposed to be.

  2. artemklevtsov commented on Nov 24, 2014

    @artemklevtsov
    Author

    It's cp1251. I have another example: http://psytests.org/.

  3. hadley commented on Nov 25, 2014

    @hadley
    Member

    A few more diagnostics:

    library("httr")
    library("rvest")
    url <- "http://psytests.org"
    
    # No encoding in http request
    r <- GET(url)
    headers(r)$`Content-Type`
    
    # So default text content from httr is bad
    content(r, "text")
    
    # stringi thinks encoding is ISO-8859-1
    as.data.frame(stringi::stri_enc_detect(content(r, "raw"))[[1]])
    
    # But it's not
    stringi::stri_encode(content(r, "raw"), "ISO-8859-1")
    
    # It's actually cp1251
    stringi::stri_encode(content(r, "raw"), "cp1251")
    
    # Which also works when we give it to content
    content(r, "text", encoding = "cp1251")
    
    # But not when we give it to rvest::html
    rvest::html("http://psytests.org", encoding = "cp1251")
  4. artemklevtsov commented on Nov 26, 2014

    @artemklevtsov
    Author
    rvest::html("http://psytests.org", encoding = "cp1251")

    Translates to

    rvest:::html.response(httr::GET("http://psytests.org"), encoding = "cp1251")

    In rvest:::html.response we can see that text <- httr::content(x, "text") called without encoding arg. Then XML::htmlParse applied on the broken string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions