gpt4 book ai didi

ruby - 如何让 Mechanize 自动将正文转换为 UTF8?

转载 作者:数据小太阳 更新时间:2023-10-29 07:30:52 25 4
gpt4 key购买 nike

我找到了一些使用 post_connect_hookpre_connect_hook 的解决方案,但它们似乎不起作用。我正在使用最新的 Mechanize 版本 (2.1)。新版本没有[:response]字段,新版本不知道去哪里找。

是否可以让 Mechanize 返回 UTF8 编码版本,而不必使用 iconv 手动转换它?

最佳答案

从 Mechanize 2.0 开始,pre_connect_hooks()post_connect_hooks() 的参数发生了变化。

参见 Mechanize文档:

pre_connect_hooks()

A list of hooks to call before retrieving a response. Hooks are called with the agent, the URI, the response, and the response body.

post_connect_hooks()

A list of hooks to call after retrieving a response. Hooks are called with the agent, the URI, the response, and the response body.

现在您无法更改内部响应主体值,因为参数不是数组。因此,下一个最佳方法是用您自己的解析器替换内部解析器:

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/')
...

关于ruby - 如何让 Mechanize 自动将正文转换为 UTF8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8864493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com