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

前端 未结 7 632
没有蜡笔的小新
没有蜡笔的小新 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:18

    Here's one that works better with .co.uk and .com.fr - type domains

    domain = uri.host[/[^.\s\/]+\.([a-z]{3,}|([a-z]{2}|com)\.[a-z]{2})$/]
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
  • 2020-11-30 02:23

    Addressable is probably the right answer in 2018, especially uses the PublicSuffix gem to parse domains.

    However, I need to do this kind of parsing in multiple places, from various data sources, and found it a bit verbose to use repeatedly. So I created a wrapper around it, Adomain:

    require 'adomain'
    
    Adomain["https://toolbar.google.com"]
    # => "toolbar.google.com"
    
    Adomain["https://www.google.com"]
    # => "google.com"
    
    Adomain["stackoverflow.com"]
    # => "stackoverflow.com"
    

    I hope this helps others.

    0 讨论(0)
  • 2020-11-30 02:25

    Just a short note: to overcome the second parsing of the url from Mischas second example, you could make a string comparison instead of URI.parse.

    # Only parses once
    def get_host_without_www(url)
      url = "http://#{url}" unless url.start_with?('http')
      uri = URI.parse(url)
      host = uri.host.downcase
      host.start_with?('www.') ? host[4..-1] : host
    end
    

    The downside of this approach is, that it is limiting the url to http(s) based urls, which is widely the standard. But if you will use it more general (f.e. for ftp links) you have to adjust accordingly.

    0 讨论(0)
  • 2020-11-30 02:26

    if the URL is in format http://www.google.com, then you could do something like:

    a = 'http://www.google.com'
    puts a.split(/\./)[1] + '.' + a.split(/\./)[2]
    

    Or

    a =~ /http:\/\/www\.(.*?)$/
    puts $1
    
    0 讨论(0)
  • 2020-11-30 02:32

    Well you can write this method:

    require 'URI'
    def domain_name(url, arg={:with_dot_principal=>false})
      arg[:with_dot_principal] ? URI(url).hostname.split('.').last(2).join('.') : URI(url).hostname.split('.').last(2).first
    end
    

    And using:

    domain_name("https://www.google.com/?gws_rd=ssl&safe=active&ssui=on")
    # => "google"
    domain_name("http://google.com", with_dot_principal: true)
    # => "google.com"
    
    0 讨论(0)
提交回复
热议问题