如何设置机械化页面编码? [英] How to set the mechanize page encoding?

查看:140
本文介绍了如何设置机械化页面编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ISO-8859-1编码的页面,点击一个链接,所以代码与此类似:

I'm trying to get a page with an ISO-8859-1 encoding clicking on a link, so the code is similar to this:

page_result = page.link_with( :text => 'link_text' ).click

到目前为止,我得到一个错误的编码结果,所以我看到的字符像:

So far I get the result with a wrong encoding, so I see characters like:

'T�tulo:' instead of 'Título:'

我尝试了几种方法,包括:

I've tried several approaches, including:


  • 使用代理程序在第一个请求中说明编码,如:

  • Stating the encoding in the first request using the agent like:

@page_search = @agent.get(
  :url => 'http://www.server.com',
  :headers => { 'Accept-Charset' => 'ISO-8859-1' } )


  • 说明页面本身的编码

  • Stating the encoding for the page itself

      page_result.encoding = 'ISO-8859-1'
    


  • 但是我必须做错事:一个简单的put总是显示错误的char acters。

    But I must be doing something wrong: a simple puts always show the wrong characters.

    您是否知道如何陈述编码?

    Do you know how to state the encoding?

    提前感谢

    添加:可执行示例:

    require 'rubygems'
    require 'mechanize'
    
    WWW::Mechanize::Util::CODE_DIC[:SJIS] = "ISO-8859-1"
    
    @agent = WWW::Mechanize.new
    
    @page = @agent.get(
      :url => 'http://www.mcu.es/webISBN/tituloSimpleFilter.do?cache=init&layout=busquedaisbn&language=es',
      :headers => { 'Accept-Charset' => 'utf-8' } )
    
    puts @page.body
    


    推荐答案

    嘿,你可以做一个:

    agent.page.encoding = 'utf-8'
    

    希望它有帮助!

    这篇关于如何设置机械化页面编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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