我是Mechanize的新手。我正在尝试使用搜索词“TAICHI 21”搜索表单,但它似乎不起作用。该页面位于http://www.asus.com/Search/
这是我犯的错误还是检测机器人的形式?
require 'nokogiri'
require 'mechanize'
agent = Mechanize.new
#User Agent masking
agent.user_agent_alias = 'Windows Mozilla'
#This handles the url
page = agent.get('http://www.asus.com/Search/')
pp page
#Lock onto the search box
asus_form = page.form('aspnetForm')
#Prepare a search for our form
asus_form.q = 'TAICHI 21'
#Submit our form
button = asus_form.button_with(:value => "Button1")
page = agent.submit(asus_form, button)
#Output our Pretty Print to text file
pp page
File.open("results.txt","w") do |f|
PP.pp(page,f)
end
答案 0 :(得分:0)
我仔细研究了结果,然后在其中找到了您的链接。在我的测试中,我改变了代码中的一些东西,但我相信这并没有真正改变任何东西。最后一行找到您的链接。
agent = Mechanize.new
#User Agent masking
agent.user_agent_alias = 'Windows Mozilla'
#This handles the url
search = 'TAICHI 21'
page = agent.get("http://www.asus.com/Search/?SearchKey=#{search}")
#Output our Pretty Print to text file
File.open("results.txt","w") do |f|
PP.pp(page.links.find_all{|l| l.text =~ /#{search}/i},f)
end