如何使用Ruby获取缩短URL的目标URL?

时间:2022-08-23 10:36:54

How do I take this URL http://t.co/yjgxz5Y and get the destination URL which is http://nickstraffictricks.com/4856_how-to-rank-1-in-google/

如何获取此URL http://t.co/yjgxz5Y并获取目标网址http://nickstraffictricks.com/4856_how-to-rank-1-in-google/

4 个解决方案

#1


23  

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/" 

#2


8  

I've used open-uri for this, because it's nice and simple. It will retrieve the page, but will also follow multiple redirects:

我已经使用了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/>

The docs show a nice example for using the lower-level Net::HTTP to handle redirects.

文档显示了使用较低级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')

Of course this all breaks down if the page isn't using a HTTP redirect. A lot of sites use meta-redirects, which you have to handle by retrieving the URL from the meta tag, but that's a different question.

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

#3


3  

For resolving redirects you should use a HEAD request to avoid downloading the whole response body (imagine resolving a URL to an audio or video file).

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

Working example using the Faraday 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


1  

You would have to follow the redirect. I think that would help :

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

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

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

#1


23  

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/" 

#2


8  

I've used open-uri for this, because it's nice and simple. It will retrieve the page, but will also follow multiple redirects:

我已经使用了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/>

The docs show a nice example for using the lower-level Net::HTTP to handle redirects.

文档显示了使用较低级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')

Of course this all breaks down if the page isn't using a HTTP redirect. A lot of sites use meta-redirects, which you have to handle by retrieving the URL from the meta tag, but that's a different question.

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

#3


3  

For resolving redirects you should use a HEAD request to avoid downloading the whole response body (imagine resolving a URL to an audio or video file).

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

Working example using the Faraday 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


1  

You would have to follow the redirect. I think that would help :

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

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

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