使用Ruby通过mechanize登录表单
我正在尝试使用Ruby库以编程方式登录站点,然后以登录用户的身份访问可用的内容使用Ruby通过mechanize登录表单,ruby,web-scraping,mechanize,Ruby,Web Scraping,Mechanize,我正在尝试使用Ruby库以编程方式登录站点,然后以登录用户的身份访问可用的内容 # intantiating a Mechanize instance @agent = Mechanize.new # username and password for site log in username = "username" password = "password" #load site site page = @agent.get("https://someDomain.com/") # g
# intantiating a Mechanize instance
@agent = Mechanize.new
# username and password for site log in
username = "username"
password = "password"
#load site site
page = @agent.get("https://someDomain.com/")
# get the login form
form = @agent.page.forms.first
#fill the form with credentials
form.field_with(:name => "username").value = username
form.field_with(:name => "password").value = password
form.method = "POST"
#submit the login form
page = form.submit
表单提交后,分配给页面的Mechanize对象具有如下url属性:
{url
#<URI::HTTPS https://someDomain.com/?username=username&password=password#>
}
我认为通过使用Mechanize将用户名和密码传递到url的方式是错误的,因为当我手动登录时,网站似乎不会这样做
问题:
本地变量页面是否包含用户登录后显示的响应页面
我现在想访问某些只有登录用户才能访问的URL-我需要做什么
看起来您可能需要将方法从GET更改为POST。不看就很难说。真的。。。您知道如何指定请求类型吗?我相信它是form.method='POST'刚刚尝试过,并在上面更新了我的帖子。但不确定这是否是正确的方法