如何使用Mechanize登录和爬网站点 [英] How to login and crawl a site using Mechanize
问题描述
我正在尝试使用Mechanize登录和抓取网站.
I'm trying to use Mechanize to login and crawl a site.
由于某种原因,我似乎无法使登录功能正常工作.有什么想法吗?
For some reason, I can't seem to get the login function to work. Any ideas?
这是我的代码:
require 'nokogiri'
require 'open-uri'
require 'mechanize'
a = Mechanize.new
a.get('https://jackthreads.com/')
form = a.page.form_with(:class => 'jt-form')
form.field_with(:name => "email").value = "email"
form.field_with(:name => "password21").value = "password"
page = a.submit(form, form.buttons.first)
推荐答案
表单上的操作设置为"#
",因此您的提交将被忽略.实际上是通过AJAX对https://www.jackthreads.com/login?method=ajax
进行POST调用.也许,如果您在提交之前使用Mechanize更新表单的action属性,它将可以解决问题.
The action on the form is set to "#
", so your submit is being ignored. The POST call is actually being made to https://www.jackthreads.com/login?method=ajax
via AJAX. Perhaps if you update the form's action attribute with Mechanize before submitting, it will do the trick.
对于它的价值,我用Chrome Web Inspector弄清楚了.看到该值设置为"#
"后,我进入了网络"选项卡,通过XHR进行了过滤,然后尝试提交一些内容.
For what it's worth, I figured this out with the Chrome Web Inspector. After seeing the value was set to "#
", I went to the network tab, filtered by XHR, then tried submitting something.
这篇关于如何使用Mechanize登录和爬网站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!