0

我是机械化的新手。我正在尝试使用搜索词“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
4

1 回答 1

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
于 2013-01-29T11:59:58.057 回答