gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:12:42 24 4
gpt4 key购买 nike

我想从这个页面获取数据:

http://www.canadapost.ca/cpotools/apps/track/personal/findByTrackNumber?trackingNumber=0656887000494793

但该页面转发到:
http://www.canadapost.ca/cpotools/apps/track/personal/findByTrackNumber?execution=eXs1

所以,当我使用 open ,从 OpenUri 尝试获取数据,它抛出一个 RuntimeError错误提示 HTTP redirection loop:
我不太确定如何在重定向并抛出该错误后获取该数据。

最佳答案

您需要一个类似 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

你已经准备好摇滚了。

关于ruby-on-rails - 使用 OpenUri,如何获取重定向页面的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2759073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com