How would you parse a url in Ruby to get the main domain?

前端 未结 7 654
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 02:03

I want to be able to parse any url with ruby to get the main part of the domain without the www (just the XXXX.com)

7条回答
  •  不知归路
    2020-11-30 02:20

    Please note there is no algorithmic method of finding the highest level at which a domain may be registered for a particular top-level domain (the policies differ with each registry), the only method is to create a list of all top-level domains and the level at which domains can be registered.

    This is the reason why the Public Suffix List exists.

    I'm the author of PublicSuffix, a Ruby library that decomposes a domain into the different parts.

    Here's an example

    require 'uri/http'
    
    uri = URI.parse("http://toolbar.google.com")
    domain = PublicSuffix.parse(uri.host)
    # => "toolbar.google.com"
    domain.domain
    # => "google.com"
    
    uri = URI.parse("http://www.google.co.uk")
    domain = PublicSuffix.parse(uri.host)
    # => "www.google.co.uk"
    domain.domain
    # => "google.co.uk"
    

提交回复
热议问题