2014-03-29 111 views
0

我試過了,但無法這樣做。我想從鏈接中獲取基礎網址。爲了清楚我的問題,這裏的例子:從鏈接中獲取基本網址

鏈接1:http://thechangelog.com/rawler-crawl-your-website-and-find-broken-links-with-rub/

基地URL1:http://thechangelog.com

鏈路2:https://www.facebook.com/BreakingBad

基地URL2:https://www.facebook.com

+0

什麼是https:// username:[email protected]:443/path?query_string#fragment_id'的「base url」? – Johnsyweb

回答

6

你可以這樣使用URI模塊做:http://www.ruby-doc.org/stdlib-2.1.0/libdoc/uri/rdoc/URI.html

require 'uri' 
uri = URI("http://thechangelog.com/rawler-crawl-your-website-and-find-broken-links-with-rub/") 
puts "#{uri.scheme}://#{uri.host}" 

的URI API是v1.9.3一樣的,所以你應該能夠做同樣的舊版本中Ruby或JRuby,如果這就是你正在使用的。

+0

這太棒了!謝謝! :) – shivam

0

您可以使用下面的正則表達式:

'(http)(s?)(://)([^/]*)'

0
require 'uri' 

uri = URI.parse('http://thechangelog.com/rawler-crawl-your-website-and-find-broken-links-with-rub/') 
base = "#{uri.scheme}://#{uri.host}"