我正在尝试从 Watir 下载文件,但我不想使用这种loop sleep
方法。
我宁愿在交互的最后一刻,重新创建 Watir 在网页上的会话并使用另一个库,例如 Typhoeus。
Typhoeus 使用 curl 并且可以使用文件中的 cookie,尽管如此,Watir 会生成一个哈希数组,如果我要求保存它,它会将其保存为 YAML 文件。
有没有更快的转换方法?
StackOverflow 中的另一篇文章说 curl 使用Mozilla 风格的 cookie 文件。
所以,如果你的 Watir 实例是browser
并且你要写入的文件是file
你可以做
browser.cookies.to_a.each do |ch|
terms = []
terms << ch[:domain]
terms << ch[:same_site].nil? ? 'FALSE' : 'TRUE'
terms << ch[:path]
terms << ch[:secure] ? 'TRUE' : 'FALSE'
terms << ch[:expires].to_i.to_s
terms << ch[:name]
terms << ch[:value]
file.puts terms.join("\t")
end
然后,您可以告诉 Typhoeus 使用 的内容file
继续使用相同的 cookie 进行导航。