使用 ruby mechanize 从 foursquare 获取数据
我正在尝试使用 ruby 和 Mechanize 来解析 foursquare 网站上的数据。这是我的代码:
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
但是,当我运行此代码时,弹出以下错误:
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>'
我检查并发现表单对象“F12778070592981DXGWJ”和“F1277807059296KSFTWQ”的这两个变量每次当我尝试打开foursquare的网页时都会发生变化。
以前有人遇到过同样的问题吗?每次尝试打开网页时,变量都会发生变化吗?我应该如何解决这个问题?
我们的项目是关于解析 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 "F1277807059296KSFTWQ" 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Mechanize 对于不公开 API 的网站很有用,但 Foursquare 拥有已建立的 REST API 已经。我建议使用 Ruby 库 之一,也许是 foursquare2。这些库抽象了身份验证等内容,因此您只需注册您的应用并使用提供的密钥。
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.
不必按名称对表单字段进行索引,只需按顺序对其进行索引即可。这样您就不必担心每个请求时名称的更改:
但是,就像 dwhalen 所说,使用 REST API 可能是更好的方法。这就是它存在的原因。
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:
However like dwhalen said, using the REST API is probably a much better way. That's why it's there.