1

Mechanizeとのリンクをたどろうとしていますが、機能していないようです。構文が正しいようです。これを誤って参照していますか、それとも何か他のことをする必要がありますか?

問題領域

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
4

1 に答える 1

0

Charles( 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.location.hrefを破棄して、エージェントに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

ハッピースクレイピング

于 2013-02-25T15:27:17.327 に答える