【问题标题】:How do I get the destination URL of a shortened URL using Ruby?如何使用 Ruby 获取缩短 URL 的目标 URL?
【发布时间】:2011-07-28 18:57:34
【问题描述】:

【问题讨论】:

  • 试试这个宝石final_redirect_url
  • gem final_redirect_url 正是您想要的 --> 真正的 URL,没有大惊小怪,没有大惊小怪。 +1

标签: ruby web-crawler http-redirect


【解决方案1】:
require 'net/http'
require 'uri'

Net::HTTP.get_response(URI.parse('http://t.co/yjgxz5Y'))['location']
# => "http://nickstraffictricks.com/4856_how-to-rank-1-in-google/" 

【讨论】:

  • 根据文档,Net::HTTP 不执行递归重定向,如果重定向被重定向,这是必要的。这看起来只能处理第一个。
  • 是的。你需要一个循环。但无论如何,这就是您在 Ruby 中遵循重定向的方式,我相信这可以回答问题。
【解决方案2】:

我为此使用了open-uri,因为它既好又简单。它会检索页面,但也会遵循多个重定向:

require 'open-uri'

final_uri = ''
open('http://t.co/yjgxz5Y') do |h|
  final_uri = h.base_uri
end
final_uri # => #<URI::HTTP:0x00000100851050 URL:http://nickstraffictricks.com/4856_how-to-rank-1-in-google/>

文档显示了使用较低级别的 Net::HTTP 处理重定向的一个很好的示例。

require 'net/http'
require 'uri'

def fetch(uri_str, limit = 10)
  # You should choose better exception.
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0

  response = Net::HTTP.get_response(URI.parse(uri_str))
  case response
  when Net::HTTPSuccess     then response
  when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  else
    response.error!
  end
end

puts fetch('http://www.ruby-lang.org')

当然,如果页面没有使用 HTTP 重定向,这一切都会崩溃。许多网站使用元重定向,您必须通过从元标记中检索 URL 来处理,但这是一个不同的问题。

【讨论】:

  • 谢谢!非常有帮助.. 做 h.base_uri.to_s 将呈现目标网址。
  • 我觉得你可以跳过block的使用,直接调用open(url).base_uri
  • Net::HTTP 版本应该是公认的答案,因为它处理 SSL 以及递归重定向(大多数示例似乎只处理其中一个)。干得好!
【解决方案3】:

为了解决重定向,您应该使用HEAD 请求以避免下载整个响应正文(想象一下将 URL 解析为音频或视频文件)。

使用法拉第 gem 的工作示例:

require 'faraday'
require 'faraday_middleware'

def resolve_redirects(url)
    response = fetch_response(url, method: :head)
    if response
        return response.to_hash[:url].to_s
    else
        return nil
    end
end

def fetch_response(url, method: :get)
    conn = Faraday.new do |b|
        b.use FaradayMiddleware::FollowRedirects;
        b.adapter :net_http
    end
    return conn.send method, url
rescue Faraday::Error, Faraday::Error::ConnectionFailed => e
    return nil
end

puts resolve_redirects("http://cre.fm/feed/m4a") # http://feeds.feedburner.com/cre-podcast

【讨论】:

    【解决方案4】:

    您必须遵循重定向。我认为这会有所帮助:

    http://shadow-file.blogspot.com/2009/03/handling-http-redirection-in-ruby.html

    【讨论】:

    • 技术上不正确。您不需要“关注”重定向,您只需要阅读发送来导致重定向的 Location 标头,如 Mladen Jabnović 的回答。
    • 可以重定向重定向。除非底层代码自动处理它,它不会使用 Net::HTTP,否则重定向也必须遵循,直到您确定重定向太深,或者它们最终在最终 URL 处解析。链接到的特定页面比 Net::HTTP 文档中的示例更复杂。
    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2011-05-11
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多