从URL获取子域

从URL获取子域,url,parsing,dns,subdomain,shell,bash,posix,Url,Parsing,Dns,Subdomain,Shell,Bash,Posix,从URL获取子域一开始听起来很容易 http://www.domain.example 扫描第一个句点,然后返回“http://”后面的任何内容 那你还记得吗 http://super.duper.domain.example http://super.duper.domain.co.uk 哦。然后你会想,好吧,找到最后一节课,回去一句话,把所有的东西都记下来 那你还记得吗 http://super.duper.domain.example http://super.duper.doma

从URL获取子域一开始听起来很容易

http://www.domain.example
扫描第一个句点,然后返回“http://”后面的任何内容

那你还记得吗

http://super.duper.domain.example
http://super.duper.domain.co.uk
哦。然后你会想,好吧,找到最后一节课,回去一句话,把所有的东西都记下来

那你还记得吗

http://super.duper.domain.example
http://super.duper.domain.co.uk
你又回到原点了。除了存储所有TLD的列表之外,还有什么好主意吗

除此之外还有什么好主意吗 是否存储所有TLD的列表

不,因为每个TLD在子域、二级域等方面有所不同

请记住,有顶级域、二级域和子域。从技术上讲,除TLD之外的所有内容都是子域

在domain.com.uk示例中,“domain”是子域,“com”是二级域,“uk”是TLD

因此,问题仍然比乍一看更加复杂,这取决于如何管理每个TLD。您需要一个包含所有TLD的数据库,其中包括它们的特定分区,以及第二级域和子域。虽然TLD不太多,因此该列表可以合理地管理,但收集所有这些信息并不容易。可能已经有了这样一份清单

看起来是这样一个列表,列表中所有常用后缀(.com、.co.uk等)都适合搜索。解析它仍然不容易,但至少您不必维护列表

“公共后缀”是指 互联网用户可以直接注册 名字。公共服务的一些例子 后缀为“.com”、“.co.uk”和 “pvt.k12.wy.us”。公共后缀 列表是所有已知公共资源的列表 后缀

公共后缀列表是一个 Mozilla基金会的倡议。 它可以在任何场合使用 软件,但最初是创建的 满足浏览器的需求 制造商。它允许浏览器, 例如:

(parse "sub1.sub2.domain.co.uk") 
;=> {:public-suffix "co.uk", :domain "domain.co.uk", :rule-used "*.uk"}
  • 避免设置损害隐私的“超级图书” 高级域名后缀
  • 突出显示用户名中最重要的部分 接口
  • 按站点准确排序历史记录条目

,你可以看到这不是一个小问题。我认为列表是实现这一目标的唯一正确方法…

正如亚当所说,这并不容易,目前唯一实用的方法是使用列表

即使如此,也存在例外情况-例如在
.uk
中,有少数域在该级别立即有效,但不在
.co.uk
中,因此这些域必须作为例外情况添加

目前主流浏览器就是这样做的-有必要确保
example.co.uk
不能为
.co.uk
设置Cookie,然后将Cookie发送到
.co.uk
下的任何其他网站

好消息是,网站上已经有了一个列表


中还做了一些工作,以创建某种标准,允许TLD声明其域结构的外观。这有点复杂,尽管像
.uk.com
,它的操作就好像它是一个公共后缀一样,但是
.com
注册中心并不出售它。

它没有精确地计算出来,但是你可以通过尝试逐块获取域并检查响应(即,获取“”,然后获取“”)来获得有用的答案,然后是“”。当您得到一个非错误响应时,您就得到了域,其余的是子域

有时候你只需要试试:)

编辑:

Tom Leys在评论中指出,一些域仅在www子域上设置,这将在上述测试中给我们一个不正确的答案。说得好!也许最好的方法是使用“”和“http://”检查每个部分,并将其中一部分的命中数计算为域名的该部分的命中数?我们仍然会错过一些“替代”的安排,比如“web.domain.com”,但我已经有一段时间没有遇到这样的安排了:)

常见后缀的列表(.co.uk、.com等等)与http://一起去掉,然后你就只有“sub.domain”可以使用,而不是“”,或者至少我可能会这样做


最大的问题是可能的后缀列表

快速查看publicsuffix.org列表后,您似乎可以通过从域中删除最后三个段(此处“段”表示两个点之间的段)来做出合理的近似,其中最后一个段为两个字符长,假设它是国家代码,并将进一步细分。如果最后一段是“us”,并且倒数第二段也是两个字符,请删除最后四段。在所有其他情况下,删除最后两段。e、 g:

“示例”不是两个字符,请删除“domain.example”,留下“www”

“示例”不是两个字符,请删除“domain.example”,留下“super.duper”

“uk”是两个字符(但不是“us”),所以删除“domain.co.uk”,留下“super.duper”

“us”是两个字符,是“us”,加上“wy”也是两个字符,所以删除“pvt.k12.wy.us”,留下“foo”


请注意,尽管这适用于我迄今为止在回答中看到的所有示例,但它仍然只是一个合理的近似值。这并不完全正确,尽管我怀疑它与你可能得到的最接近,而不需要制作/获得一个实际的列表以供参考。

Publicsuffix.org似乎是一种方法。有很多实现可以轻松解析publicsuffix数据文件的内容:

  • Perl:
  • 爪哇:
  • PHP:
  • C#/.NET:
  • Python:
  • 鲁比:
使用URIBuilder 然后获取URIBUilder.host属性 将其拆分为“”上的数组 现在,您有了一个拆分了域的数组。

$extract = new LayerShifter\TLDExtract\Extract();

$result = $extract->parse('super.duper.domain.co.uk');
$result->getSubdomain(); // will return (string) 'super.duper'
$result->getSubdomains(); // will return (array) ['super', 'duper']
$result->getHostname(); // will return (string) 'domain'
$result->getSuffix(); // will return (string) 'co.uk'
tldjs.getDomain('mail.google.co.uk');
// -> 'google.co.uk'
var KEY = '__rT_dM__' + (+new Date());
var R = new RegExp('(^|;)\\s*' + KEY + '=1');
var Y1970 = (new Date(0)).toUTCString();

module.exports = function getRootDomain() {
  var domain = document.domain || location.hostname;
  var list = domain.split('.');
  var len = list.length;
  var temp = '';
  var temp2 = '';

  while (len--) {
    temp = list.slice(len).join('.');
    temp2 = KEY + '=1;domain=.' + temp;

    // try to set cookie
    document.cookie = temp2;

    if (R.test(document.cookie)) {
      // clear
      document.cookie = temp2 + ';expires=' + Y1970;
      return temp;
    }
  }
};
import requests url = 'https://publicsuffix.org/list/public_suffix_list.dat' page = requests.get(url) domains = [] for line in page.text.splitlines(): if line.startswith('//'): continue else: domain = line.strip() if domain: domains.append(domain) domains = [d[2:] if d.startswith('*.') else d for d in domains] print('found {} domains'.format(len(domains))) import re _regex = '' for domain in domains: _regex += r'{}|'.format(domain.replace('.', '\.')) subdomain_regex = r'/([^/]*)\.[^/.]+\.({})/.*$'.format(_regex) domain_regex = r'([^/.]+\.({}))/.*$'.format(_regex) FILE_NAME = '' # put CSV file name here URL_COLNAME = '' # put URL column name here import pandas as pd df = pd.read_csv(FILE_NAME) urls = df[URL_COLNAME].astype(str) + '/' # note: adding / as a hack to help regex df['sub_domain_extracted'] = urls.str.extract(pat=subdomain_regex, expand=True)[0] df['domain_extracted'] = urls.str.extract(pat=domain_regex, expand=True)[0] df.to_csv('extracted_domains.csv', index=False)
wget -O - https://publicsuffix.org/list/public_suffix_list.dat |
    grep '^[^/]' |
    tac > tld-list.txt
splitDom() {
    local tld
    while read tld;do
        [ -z "${1##*.$tld}" ] &&
            printf "%s : %s\n" $tld ${1%.$tld} && return
    done <tld-list.txt
}
splitDom super.duper.domain.co.uk
co.uk : super.duper.domain

splitDom super.duper.domain.com
com : super.duper.domain
tlds=($(<tld-list.txt))
splitDom() {
    local tld
    local -n result=${2:-domsplit}
    for tld in ${tlds[@]};do
        [ -z "${1##*.$tld}" ] &&
            result=($tld ${1%.$tld}) && return
    done
}
splitDom super.duper.domain.co.uk myvar
declare -p myvar
declare -a myvar=([0]="co.uk" [1]="super.duper.domain")

splitDom super.duper.domain.com
declare -p domsplit
declare -a domsplit=([0]="com" [1]="super.duper.domain")
declare -A TLDS='()'
while read tld ;do
    if [ "${tld##*.}" = "$tld" ];then
        TLDS[${tld##*.}]+="$tld"
      else
        TLDS[${tld##*.}]+="$tld|"
    fi
done <tld-list.txt
shopt -s extglob 
splitDom() {
    local domsub=${1%%.*(${TLDS[${1##*.}]%\|})}
    local -n result=${2:-domsplit}
    result=(${1#$domsub.} $domsub)
}
for dom in dom.sub.example.{,{co,adm,com}.}{com,ac,de,uk};do
    splitDom $dom myvar
    printf "%-40s %-12s %s\n" $dom ${myvar[@]}
done
dom.sub.example.com                      com          dom.sub.example
dom.sub.example.ac                       ac           dom.sub.example
dom.sub.example.de                       de           dom.sub.example
dom.sub.example.uk                       uk           dom.sub.example
dom.sub.example.co.com                   co.com       dom.sub.example
dom.sub.example.co.ac                    ac           dom.sub.example.co
dom.sub.example.co.de                    de           dom.sub.example.co
dom.sub.example.co.uk                    co.uk        dom.sub.example
dom.sub.example.adm.com                  com          dom.sub.example.adm
dom.sub.example.adm.ac                   ac           dom.sub.example.adm
dom.sub.example.adm.de                   de           dom.sub.example.adm
dom.sub.example.adm.uk                   uk           dom.sub.example.adm
dom.sub.example.com.com                  com          dom.sub.example.com
dom.sub.example.com.ac                   com.ac       dom.sub.example
dom.sub.example.com.de                   com.de       dom.sub.example
dom.sub.example.com.uk                   uk           dom.sub.example.com
                Posix version     $tldS (array)      $TLDS (associative array)
File read   :       0.04164          0.55507           18.65262
Split loop  :     114.34360         88.33438            3.38366
Total       :     114.34360         88.88945           22.03628
example.com.tr: com.tr
private String getSubDomain(Uri url) throws Exception{
                        String subDomain =url.getHost();
                        String fial=subDomain.replace(".","/");
                        String[] arr_subDomain =fial.split("/");
                        return arr_subDomain[0];
                    }