ruby-on-rails – 使用OpenUri,如何获取重定向页面的内容?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 使用OpenUri,如何获取重定向页面的内容?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从这个页面获取数据:
http://www.canadapost.ca/cpotools/apps/track/personal/findByTrackNumber?trackingNumber=0656887000494793

但该页面转发到:

http://www.canadapost.ca/cpotools/apps/track/personal/findByTrackNumber?execution=eXs1

因此,当我从OpenUri使用open来尝试获取数据时,它会抛出一个RuntimeError错误,说HTTP重定向循环:

我不确定如何在重定向并抛出该错误获取该数据.

解决方法

你需要一个像 Mechanize这样的工具.从它的描述:

The Mechanize library is used for
automating interaction with websites.
Mechanize automatically stores and
sends cookies,follows redirects,can
follow links,and submit forms. Form
fields can be populated and submitted.
Mechanize also keeps track of the
sites that you have visited as a
history.

这正是你需要的.所以,

sudo gem install mechanize

然后

require 'mechanize'
agent = WWW::Mechanize.new
page = agent.get "http://www.canadapost.ca/cpotools/apps/track/personal/findByTrackNumber trackingNumber=0656887000494793"

page.content # Get the resulting page as a string
page.body # Get the body content of the resulting page as a string
page.search(".somecss") # Search for specific elements by XPath/CSS using nokogiri

而你已经准备好摇滚了.

原文链接:https://www.f2er.com/ruby/269262.html

猜你在找的Ruby相关文章