Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Ruby获取缩短URL的目标URL?_Ruby_Web Crawler_Http Redirect - Fatal编程技术网

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

如何使用Ruby获取缩短URL的目标URL?,ruby,web-crawler,http-redirect,Ruby,Web Crawler,Http Redirect,如何获取此URL并获取目标URL,即,您必须遵循重定向。我认为这将有助于: 我使用了openuri,因为它既漂亮又简单。它将检索页面,但也将遵循多个重定向: 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://nickstraffi

如何获取此URL并获取目标URL,即

,您必须遵循重定向。我认为这将有助于:


我使用了
openuri
,因为它既漂亮又简单。它将检索页面,但也将遵循多个重定向:

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

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

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

使用法拉第宝石的工作示例:

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

技术上不正确。您不需要“遵循”重定向,只需要读取发送来导致重定向的位置头,就像姆拉登·贾布拉诺维奇的回答一样。重定向可以被重定向。除非底层代码自动处理,而它不会使用Net::HTTP,否则重定向也必须遵循,直到您确定重定向太深,或者它们最终在最终URL处解析。链接到的特定页面比Net::HTTP文档中的示例更复杂。根据文档,Net::HTTP不执行递归重定向,如果重定向被重定向,这是必需的。看起来它只能处理第一个。是的。你需要一个循环。但不管怎样,这就是你在Ruby中遵循重定向的方式,我相信这就回答了这个问题。谢谢!非常有帮助。。执行h.base_uri.to_s将呈现目标url。我认为您可以跳过block的使用,只需调用
open(url)。base_uri
Net::HTTP版本应该是公认的答案,因为它处理SSL以及递归重定向(大多数示例似乎只处理其中一个)。做得好!试试这个gem。gem final\u redirect\u url正是您想要的-->真正的url,没有麻烦,没有混乱+1.
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')
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