本文介绍了如何在使用Ruby mechanize加载页面之前设置Referer标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Mechanize 2.3设置自定义标头有直接的方法吗?

Is there a straightforward way to set custom headers with Mechanize 2.3?

我试过但得到:

$agent = Mechanize.new
$agent.pre_connect_hooks << lambda { |p|
  p[:request]['Referer'] = 'https://wwws.mysite.com/cgi-bin/apps/Main'
}

# ./mech.rb:30:in `<main>': undefined method `pre_connect_hooks' for nil:NilClass (NoMethodError)


推荐答案

对于这个问题,我注意到人们似乎在使用:

For this question I noticed people seem to use:

page = agent.get("http://www.you.com/index_login/", :referer => "http://www.you.com/")

顺便说一句,现在我测试了这个答案,似乎这不是我实际问题背后的问题:每次访问我正在抓取的网站都需要尽管我总是以yaml格式加载并保存完整的cookie jar,但是在第一次登录访问后几秒钟再次浏览登录序列页面。但这当然会导致另一个问题。

As an aside, now that I tested this answer, it seems this was not the issue behind my actual problem: that every visit to a site I'm scraping requires going through the login sequence pages again, even seconds later after the first logged-in visit, despite that I'm always loading and saving the complete cookie jar in yaml format. But that would lead to another question of course.

这篇关于如何在使用Ruby mechanize加载页面之前设置Referer标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:51