我正在尝试为亚马逊构建一个网络抓取程序,但我在第一步就被绊倒了。我这样写我的代码,只是开始四处寻找并访问亚马逊和漂亮页面,所以我知道如何继续:
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
page = agent.get('http://www.amazon.com/')
page_form = page.form('site-search')
page_form.q = 'Intermediate Microeconomics'
page = agent.submit(page_form, page_form.buttons.first)
pp page
我不断收到此错误消息:
/Users/samsunknight/.rvm/gems/ruby-2.0.0-p195/gems/mechanize-2.7.1/lib/mechanize/form.rb:217:in `method_missing': undefined method `q=' for #<Mechanize::Form:0x007f9a13012660> (NoMethodError)
from webscraperattempt1.rb:7:in `<main>'
我已经从 google.com 的示例中改编了这段代码,它可以无缝地用于agent.get('www.google.com')
and page.form('f')
,所以我想问题出在page.form('site-search')
,但是当我pp page
访问 www.amazon.com 时,“site-search”出现在完全相同的就像“f”对谷歌所做的那样。
我究竟做错了什么?/ 我该如何解决?