How to convert a Net::HTTP response to a certain encoding in Ruby 1.9.1?

前端 未结 2 1343
你的背包
你的背包 2020-12-31 12:02

I have a Sinatra application (http://analyzethis.espace-technologies.com) that does the following

  1. Retrieve an HTML page (via net/http)
  2. Create a Nokog
2条回答
  •  萌比男神i
    2020-12-31 12:36

    Because Net::HTTP does not handle encoding correctly. See http://bugs.ruby-lang.org/issues/2567

    You can parse response['content-type'] which contains charset instead of parsing whole response.body.

    Then use force_encoding() to set right encoding.

    response.body.force_encoding("UTF-8") if site is served in UTF-8.

提交回复
热议问题