从网址中提取域名

时间:2022-08-23 10:41:24

I need to split domain name from the whole URL field. I stored the url in variable and from that variable I need to split only domain name using Python. For example http://www.google.com/ here I want to split only google from the whole URL.

我需要从整个URL字段中拆分域名。我将url存储在变量中,从该变量我需要使用Python分割域名。例如http://www.google.com/在这里,我想从整个网址中仅拆分谷歌。

1 个解决方案

#1


7  

Try urlparse:

试试urlparse:

>>> from urlparse import urlparse
>>> urlparse('http://www.google.com/').hostname
'www.google.com'
>>> urlparse('http://www.google.com/').hostname.split('.')[1]
'google'

Also, see useful comments on how things can go if you have a complicated domain name with subdomains - (just hostname.split('.')[1] won't work).

另外,如果你有一个带有子域名的复杂域名,请参阅有关如何进展的有用评论 - (只是hostname.split('。')[1]不起作用)。

Also see:

另见:

#1


7  

Try urlparse:

试试urlparse:

>>> from urlparse import urlparse
>>> urlparse('http://www.google.com/').hostname
'www.google.com'
>>> urlparse('http://www.google.com/').hostname.split('.')[1]
'google'

Also, see useful comments on how things can go if you have a complicated domain name with subdomains - (just hostname.split('.')[1] won't work).

另外,如果你有一个带有子域名的复杂域名,请参阅有关如何进展的有用评论 - (只是hostname.split('。')[1]不起作用)。

Also see:

另见: