我找到了一些解决方案post_connect_hook
and pre_connect_hook
,但似乎它们不起作用。我正在使用最新的 Mechanize 版本 (2.1)。没有[:response]
新版本中的字段,我不知道在新版本中从哪里获取它们。
- https://gist.github.com/search?q=pre_connect_hooks https://gist.github.com/search?q=pre_connect_hooks
- https://gist.github.com/search?q=post_connect_hooks https://gist.github.com/search?q=post_connect_hooks
是否可以使 Mechanize 返回 UTF8 编码版本,而不必使用手动转换它iconv
?
自机械化 2.0 以来,pre_connect_hooks()
and post_connect_hooks()
被改变了。
See the 机械化 http://mechanize.rubyforge.org/Mechanize.html文档:
pre_connect_hooks()
在检索响应之前要调用的挂钩列表。使用代理、URI、响应和响应正文来调用挂钩。
post_connect_hooks()
检索响应后要调用的挂钩列表。使用代理、URI、响应和响应正文来调用挂钩。
现在您无法更改内部响应主体值,因为参数不是数组。因此,下一个最佳方法是用您自己的解析器替换内部解析器:
class MyParser
def self.parse(thing, url = nil, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_HTML, &block)
# insert your conversion code here. For example:
# thing = NKF.nkf("-wm0X", thing).sub(/Shift_JIS/,"utf-8") # you need to rewrite content charset if it exists.
Nokogiri::HTML::Document.parse(thing, url, encoding, options, &block)
end
end
agent = Mechanize.new
agent.html_parser = MyParser
page = agent.get('http://somewhere.com/')
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)