开发者

Use ruby mechanize to get data from foursquare

I am trying to use ruby and Mechanize to parse data on foursquare's website. Here is my code:

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
page = agent.get('https://foursquare.com')
page = agent.click page.link_with(:text => /Log In/) 
form = page.forms[1] 
form.F12778070592981DXGWJ = ARGV[0]
form.F1277807059296KSFTWQ = ARGV[1]
page = form.submit form.buttons.first
puts page.body

But then, when I run this code, the following error poped up:

C:/Ruby192/lib/ruby/gems/1.9.1/gems/mechanize-2.0.1/lib/mechanize/form.rb:162:in
`method_missing': undefined method `F12778070592981DXGWJ=' 
for #<Mechanize::Form:0x2b31f70> (NoMethodError)
    from four.rb:10:in `<main>'

I checked and found that these two variables for the form object "F12778070592981DXGWJ" and "F127780开发者_开发技巧7059296KSFTWQ" are changing every time when I try to open foursquare's webpage.

Does any one have the same problem before? your variables change every time you try to open a webpage? How should I solve this problem?

Our project is about parsing the data on foursquare. So I need to be able to login first.


Mechanize is useful for sites which don't expose an API, but Foursquare has an established REST API already. I'd recommend using one of the Ruby libraries, perhaps foursquare2. These libraries abstract away things like authentication, so you just have to register your app and use the provided keys.


Instead of indexing the form fields by their name, just index them by their order. That way you don't have to worry about the name that changes on each request:

form.fields[0].value = ARGV[0]
form.fields[1].value = ARGV[1]
...

However like dwhalen said, using the REST API is probably a much better way. That's why it's there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜