从 ruby​​ 中的字符串中删除子域

编程入门 行业动态 更新时间:2024-10-23 21:25:25
本文介绍了从 ruby​​ 中的字符串中删除子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历一系列 URL 并想要清理它们.我有以下代码:

I'm looping over a series of URLs and want to clean them up. I have the following code:

# Parse url to remove http, path and check format
o_url = URI.parse(node.attributes['href'])

# Remove www
new_url = o_url.host.gsub('www.', '').strip

如何扩展它以删除某些 URL 中存在的子域?

How can I extend this to remove the subdomains that exist in some URLs?

推荐答案

我刚刚编写了一个名为 Domainatrix 的库来执行此操作.你可以在这里找到它:http://github/pauldix/domainatrix

I just wrote a library to do this called Domainatrix. You can find it here: http://github/pauldix/domainatrix

require 'rubygems'
require 'domainatrix'

url = Domainatrix.parse("http://www.pauldix")
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"

这篇关于从 ruby​​ 中的字符串中删除子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-26 23:13:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/670309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   ruby

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!