Ruby:如何从屏幕上抓取 Ajax 请求的结果
我编写了一个 ruby 脚本来使用“open-uri”和“hpricot”gems 来屏幕抓取某些内容 - 到目前为止一切都很好。
但现在我必须屏幕抓取通过 javascript 函数提交表单后返回的内容(由下拉菜单中的“onchange”事件处理程序调用):
function submit_form() {
document.list.action="/some/sort/of/path";
document.list.submit();
}
据我所知,open-uri 允许您仅提交 GET 请求。如果我没记错的话,这里需要一个 POST 请求。
所以我的问题是:我需要安装和“要求”什么,然后 ruby 代码会是什么样子(以发出 POST 请求) - 抱歉,我仍然是一个 n00b...
非常感谢非常感谢您的帮助!
汤姆
I have written a ruby script to screen scrape something using the 'open-uri' and 'hpricot' gems - everything works great so far.
But now I have to screen scrape something which is returned after a form is submitted via a javascript function (called by an 'onchange' event handler from a drop-down menu):
function submit_form() {
document.list.action="/some/sort/of/path";
document.list.submit();
}
AFAIK, open-uri lets you submit only GET requests. And if I'm not mistaken, a POST request would be needed here.
So my question is: what do I need to install and to 'require' and how would the ruby code then look like (to make that POST request) - sorry, I'm still pretty much of a n00b...
Thank you very much for your help!
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你绝对应该使用Mechanize。它提供了一个漂亮的界面来与远程页面、页面上的表单等进行交互(
I think you definitely should use Mechanize. It provides a nifty interface to interact with remote pages, forms on them, and so forth (see this example).
Ruby 标准库有 http 类,这自然就支持POST操作了。
如果您发现那里的 API 不太理想,请查看 httparty gem
最后,虽然 hpricot 是伟大的宝石,它不再被积极开发。您应该考虑转向 nokogiri 它实际上取代了 hpricot 并对其进行了改进。
The Ruby standard library has the http class, which naturally supports the POST operation.
If you find the API there less than optimal, then take a look at the httparty gem
Finally, while hpricot is a great gem, it isn't actively developed any longer. You should consider moving to nokogiri which practically replaces hpricot and improves upon it.