2013-03-14 46 views
2

我有一個網址,如:如何從ruby中的url獲取sitename?

http://www.relevantmagazine.com/life/relationship/blog/23317-pursuing-singleness 

,並想從中提取只是relevantmagazine

目前我有:

@urlroot = URI.parse(@link.url).host 

但它返回www.relevantmagazine.com任何人都可以幫我嗎?

回答

5

爲此使用寶石可能是矯枉過正,但無論如何:有一個方便的寶石名爲domainatrix,可以在處理諸如兩個元素頂級域等事物時爲您提取sitename。

url = Domainatrix.parse("http://www.pauldix.net") 
url.url  # => "http://www.pauldix.net" (the original url) 
url.public_suffix  # => "net" 
url.domain # => "pauldix" 
url.canonical # => "net.pauldix" 

url = Domainatrix.parse("http://foo.bar.pauldix.co.uk/asdf.html?q=arg") 
url.public_suffix  # => "co.uk" 
url.domain # => "pauldix" 
url.subdomain # => "foo.bar" 
url.path  # => "/asdf.html?q=arg" 
url.canonical # => "uk.co.pauldix.bar.foo/asdf.html?q=arg" 
+0

謝謝你的回答,這對於這個有點矯枉過正,但在未來可能會有用 – 2013-03-14 21:18:59

+0

它肯定是這樣,直到你想解析'pilot.consulting.aero'只是爲了認識到'consulting.aero'是頂級域名。 :-D – 2013-03-14 21:21:44

+1

你需要矯枉過正...域名很複雜...你需要一個帶有每個已知tld的圖書館,以便你知道在你的「域名」之前可能會出現的所有事情,而不可能找出有多少實體來在你的域名之前..但你知道你總是離開你的域名一到兩步.. – 2013-03-14 21:29:47

0

也許你可以把它分開?

URI.parse(@link.url).host.split('.')[1] 

請記住,一些註冊的域名可能有一個以上的組件來註冊國家域名,像.co.uk.co.jp.com.au例如。

+2

你的回答也假定該URL包含一個主機名。 – 2013-03-14 20:38:19

+0

是的,這是行不通的,如果網址進來像 - http://relevantmagazine.com/life/relationship/blog/23317-pursuing-singleness,因爲我會** relatedmagazine.com **返回 – 2013-03-14 21:10:22

+0

相關鏈接會也是有問題的,像'/ example'而不是'http:// example.com/example'。 – tadman 2013-03-15 02:06:03

0

我發現tadman的回答激發了答案,the answer in another question

@urlroot = URI.parse(item.url).host 
@urlroot = @urlroot.start_with?('www.') ? @urlroot[4..-1] : @urlroot 
@urlroot = @urlroot.split('.')[0] 

第一線得到主機的,第二行得到去除WWW。如果他們是第一和第三行,則在下一個點之前獲取所有內容。

+0

這還沒有回答,因爲另一個答案中的評論指出 - 如果鏈接是http://子域名。 domain.com我會得到子域而不是域部分。 – 2013-03-14 21:29:33

1

如何約

@urlroot = URI.parse(@ link.url).host.gsub( 「WWW」, 「」).split( 「」)[0]

+0

謝謝,但這並沒有剝離.com,.co.uk等。 – 2013-03-14 21:21:56

+0

oo我錯過了這個要求 – 2013-03-14 21:22:44

+1

修正了ish .....還是如果你有www.blah.goo.arch.boom.myactualdomain.co.uk你需要一個超級強大的正則表達式......這需要所有這些考慮到如果你想超級瘋狂我會根據你的實際需求來設置它......你是否期望這樣的超級瘋狂子域名? – 2013-03-14 21:24:13

1

嘗試正則表達式:

regex = %r{http://[w]*[\.]*[^/|$]*} 

如果你有以下的URL字符串,它提供了以下:

url = 'http://www.google.com/?q=blah' 
url.scan(regex) => ["http://www.google.com"] 

url = 'http://google.com/?q=blah' 
url.scan(regex) => ["http://google.com"] 

url = 'http://google.com' 
url.scan(regex) => ["http://google.com"] 

url = 'http://foo.bar.pauldix.co.uk/asdf.html?q=arg' 
url.scan(regex) => ["http://foo.bar.pauldix.co.uk"] 

它並不完美,但它會去掉一切但前綴和主機名。然後,您可以使用其他代碼輕鬆清除前綴,因爲現在您只需要在字符串的開頭查找http://http://www.。另一個想法是,如果你也要解析https://,你可能需要調整我給你的正則表達式。我希望這可以幫助你開始!

編輯:

我重讀的問題,並實現了我的回答並沒有真正做你的要求。我想這可能會有助於知道您是否知道您正在解析的網址是否具有設置的格式,例如始終擁有www。如果是這樣,您可以使用正則表達式來提取網址中第一個和第二個時段之間的所有內容。如果沒有,也許你可以調整我的正則表達式,以便它是/或www之間的所有內容。和第一期。這可能是最簡單的方法來獲得沒有任何www的網站名稱。或.com或.au.uk等。

修正則表達式:

regex = %r{http://[w]*[\.]*[^\.]*} 
url = 'http://foo.bar.pauldix.co.uk/asdf.html?q=arg' 
url.scan(regex) => ["http://foo"] 

這將是不可思議。如果你使用正則表達式的東西,你可能不得不逐步做到清理網址來提取你想要的部分。