Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-gettld_Python_Tld - Fatal编程技术网

Python-gettld

Python-gettld,python,tld,Python,Tld,我的函数有问题,应该从域中删除tld。若域中有一些子域,它将正常工作。例如: 输入:asdf.xyz.example.com 输出:asdf.xyz.example 问题是当域没有任何子域时,域前面有点 输入:example.com 输出:。示例 这是我的代码: res = get_tld(domain, as_object=True, fail_silently=True, fix_protocol=True) domain = '.'.join([res.subdomain, res.d

我的函数有问题,应该从域中删除tld。若域中有一些子域,它将正常工作。例如:

输入:
asdf.xyz.example.com

输出:
asdf.xyz.example

问题是当域没有任何子域时,域前面有点

输入:
example.com

输出:
。示例

这是我的代码:

 res = get_tld(domain, as_object=True, fail_silently=True, fix_protocol=True)
 domain = '.'.join([res.subdomain, res.domain])
函数
get\u tld
来自


有人能帮我解决这个问题吗?

通过一个非常简单的字符串操作,这就是您想要的吗

d1 = 'asdf.xyz.example.com'
output = '.'.join(d1.split('.')[:-1])
# output = 'asdf.xyz.example'

d2 = 'example.com'
output = '.'.join(d2.split('.')[:-1])
# output = 'example'

你可以使用过滤。它看起来像是
get\u tld
按预期工作,但
join
不正确

domain = '.'.join(filter(lambda x: len(x), [res.subdomain, res.domain]))

另一个简单的版本是:

def remove_tld(url):
    *base, tld = url.split(".")
    return ".".join(base)


url = "asdf.xyz.example.com"
print(remove_tld(url))    # asdf.xyz.example

url = "example.com"
print(remove_tld(url))    # example

*base,tld=url.split(“.”
将tld放入
tld
,其他所有内容放入
base
。然后你只需加入tĥat和”。加入(基本)

什么是
get_tld
实现?对不起,我忘了提到它。我正在使用tld库