代码之家  ›  专栏  ›  技术社区  ›  Ninja2k

为什么机械化没有遵循链接

  •  1
  • Ninja2k  · 技术社区  · 12 年前

    我正试图遵循机械化的链接,但它似乎不起作用,语法似乎是正确的,我是引用错误了还是需要做其他事情?

    问题区域

    agent.page.links_with(:text => 'VG278H')[2].click
    

    完整代码

    require 'rubygems'
    require 'mechanize'
    require 'open-uri'
    
    agent = Mechanize.new
    
    agent.get ("http://icecat.biz/en/")
    
    #Show all form fields belonging to the first form
    form = agent.page.forms[0].fields
    
    #Enter VG278H into the text box lookup_text, submit the data
    agent.page.forms[0]["lookup_text"] = "VG278H"
    agent.page.forms[0].submit  #Results of this is stored in Mechanize agent.page object
    
    #Call agent.page with our results and assign them to a variable page
    page = agent.page
    
    agent.page.links_with(:text => 'VG278H')[2].click
    
    doc = page.parser
    puts doc
    
    1 回复  |  直到 12 年前
        1
  •  0
  •   rainkinz    12 年前

    你应该找一本查尔斯的书( http://www.charlesproxy.com/ )或者可以让你从浏览器中查看提交表单时发生的事情。无论如何,你的问题是这部分:

    agent.page.forms[0]["lookup_text"] = "VG278H"
    agent.page.forms[0].submit
    

    正在返回一个html片段,该片段如下所示:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>self.location.href="http://icecat.us/index.cgi?language=en&new_search=1&lookup_text=VG278H"</script>
    

    因此,您实际上需要直接调用此命令,或者取消self.locationhref,并让您的代理执行get:

    page = agent.get("http://icecat.us/index.cgi?language=en&new_search=1&lookup_text=VG278H")
    

    如果你打算这样做,这是有效的:

    require 'rubygems'
    require 'mechanize'
    require 'open-uri'
    
    agent = Mechanize.new 
    
    agent.get ("http://icecat.biz/en/")
    
    page = agent.get("http://icecat.us/index.cgi?language=en&new_search=1&lookup_text=VG278H")
    
    page = page.links_with(:text => 'VG278H')[2].click
    
    doc = page.parser
    puts doc
    

    快乐的刮擦