使用Mechanize登录表单?

时间:2014-03-14 14:03:49

标签: ruby forms login web-scraping mechanize

我一直在尝试用Ruby编写一个webscraper来从公司事件数据网站中搜集,我指的是Flickr example on the Mechanize docs页面:

当我运行corp_act_scrape.rb时:

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
home_page = agent.get("http://www.eventsdata.com/main.php#")

mypage = home_page.form_with(:name => 'loginForm') do |form|
    form.myusrname = ARGV[0]
    form.mypasswrd = ARGV[1]
end.submit

rows = page.css('#recentEventsDiv > div.RecentEventsDisplay > table > tbody > tr')

nextLink = page.link_with(:text => 'Next')
hasNextLink = nextLink?

while page.hasNextLink do
    puts rows
    page = agent.click(page.nextLink)
end

我收到错误:

corp_act_scrape.rb:9:in `block in <main>': undefined method `myusrname=' for nil:
NilClass (NoMethodError)
    from (eval):23:in `form_with'
    from corp_act_scrape.rb:7:in `<main>'

复制Flickr示例,似乎我应该能够输入我的用户名和密码作为方法,但它似乎在实践中不起作用。此外,代码的这一部分对我来说非常混乱。如果您有另一种提交表格的方法,请告诉我。

0 个答案:

没有答案