我有一个链接,例如http://www.techcrunch.com/,我想只获取链接的 techcrunch.com 部分。我该如何在python中解决这个问题?
9 回答
使用urlparse获取主机名很容易:
hostname = urlparse.urlparse("http://www.techcrunch.com/").hostname
然而,获取“根域”将会有更多问题,因为它没有在句法意义上定义。“www.theregister.co.uk”的根域是什么?使用默认域的网络怎么样?“devbox12”可能是一个有效的主机名。
处理此问题的一种方法是使用Public Suffix List,它尝试对真正的顶级域(例如“.com”、“.net”、“.org”)以及像 TLD 一样使用的私有域进行编目(例如“.co.uk”甚至“.github.io”)。您可以使用publicsuffix2库从 Python 访问 PSL :
import publicsuffix
import urlparse
def get_base_domain(url):
# This causes an HTTP request; if your script is running more than,
# say, once a day, you'd want to cache it yourself. Make sure you
# update frequently, though!
psl = publicsuffix.fetch()
hostname = urlparse.urlparse(url).hostname
return publicsuffix.get_public_suffix(hostname, psl)
URL的一般结构:
scheme://netloc/path;parameters?query#fragment
作为TIMTOWDI 的座右铭:
使用urlparse,
>>> from urllib.parse import urlparse # python 3.x
>>> parsed_uri = urlparse('http://www.stackoverflow.com/questions/41899120/whatever') # returns six components
>>> domain = '{uri.netloc}/'.format(uri=parsed_uri)
>>> result = domain.replace('www.', '') # as per your case
>>> print(result)
'stackoverflow.com/'
使用tldextract,
>>> import tldextract # The module looks up TLDs in the Public Suffix List, mantained by Mozilla volunteers
>>> tldextract.extract('http://forums.news.cnn.com/')
ExtractResult(subdomain='forums.news', domain='cnn', suffix='com')
在你的情况下:
>>> extracted = tldextract.extract('http://www.techcrunch.com/')
>>> '{}.{}'.format(extracted.domain, extracted.suffix)
'techcrunch.com'
tldextract
另一方面,通过根据公共后缀列表查找当前存在的gTLD [通用顶级域] 和 ccTLD [国家代码顶级域],了解所有 gTLD [通用顶级域] 和 ccTLD 的外观。因此,给定一个 URL,它从它的域知道它的子域,从它的国家代码知道它的域。
干杯!:)
以下脚本并不完美,但可用于显示/缩短目的。如果您真的想要/需要避免任何第 3 方依赖项——尤其是远程获取和缓存一些 tld 数据,我可以建议您遵循我在项目中使用的脚本。它将域的最后两部分用于最常见的域扩展,并将最后三个部分用于其余不太为人所知的域扩展。在最坏的情况下,域将包含三个部分而不是两个部分:
from urlparse import urlparse
def extract_domain(url):
parsed_domain = urlparse(url)
domain = parsed_domain.netloc or parsed_domain.path # Just in case, for urls without scheme
domain_parts = domain.split('.')
if len(domain_parts) > 2:
return '.'.join(domain_parts[-(2 if domain_parts[-1] in {
'com', 'net', 'org', 'io', 'ly', 'me', 'sh', 'fm', 'us'} else 3):])
return domain
extract_domain('google.com') # google.com
extract_domain('www.google.com') # google.com
extract_domain('sub.sub2.google.com') # google.com
extract_domain('google.co.uk') # google.co.uk
extract_domain('sub.google.co.uk') # google.co.uk
extract_domain('www.google.com') # google.com
extract_domain('sub.sub2.voila.fr') # sub2.voila.fr
______使用 Python 3.3 而不是 2.x________
我想在 Ben Blank 的回答中添加一点小东西。
from urllib.parse import quote,unquote,urlparse
u=unquote(u) #u= URL e.g. http://twitter.co.uk/hello/there
g=urlparse(u)
u=g.netloc
到目前为止,我刚刚从urlparse获得了域名。
要删除子域,您首先需要知道哪些是顶级域,哪些不是。例如,在上面http://twitter.co.uk
-co.uk
是一个 TLD,而在http://sub.twitter.com
我们只有.com
TLD 并且sub
是一个子域。
所以,我们需要得到一个包含所有tlds的文件/列表。
tlds = load_file("tlds.txt") #tlds holds the list of tlds
hostname = u.split(".")
if len(hostname)>2:
if hostname[-2].upper() in tlds:
hostname=".".join(hostname[-3:])
else:
hostname=".".join(hostname[-2:])
else:
hostname=".".join(hostname[-2:])
def get_domain(url):
u = urlsplit(url)
return u.netloc
def get_top_domain(url):
u"""
>>> get_top_domain('http://www.google.com')
'google.com'
>>> get_top_domain('http://www.sina.com.cn')
'sina.com.cn'
>>> get_top_domain('http://bbc.co.uk')
'bbc.co.uk'
>>> get_top_domain('http://mail.cs.buaa.edu.cn')
'buaa.edu.cn'
"""
domain = get_domain(url)
domain_parts = domain.split('.')
if len(domain_parts) < 2:
return domain
top_domain_parts = 2
# if a domain's last part is 2 letter long, it must be country name
if len(domain_parts[-1]) == 2:
if domain_parts[-1] in ['uk', 'jp']:
if domain_parts[-2] in ['co', 'ac', 'me', 'gov', 'org', 'net']:
top_domain_parts = 3
else:
if domain_parts[-2] in ['com', 'org', 'net', 'edu', 'gov']:
top_domain_parts = 3
return '.'.join(domain_parts[-top_domain_parts:])
你不需要一个包,或者人们建议这样做的任何复杂性,它就像下面一样简单,并根据你的喜好进行调整。
def is_root(url):
head, sep, tail = url.partition('//')
is_root_domain = tail.split('/', 1)[0] if '/' in tail else url
# printing or returning is_root_domain will give you what you seek
print(is_root_domain)
is_root('http://www.techcrunch.com/')
这对我有用:
def get_sub_domains(url):
urlp = parseurl(url)
urlsplit = urlp.netloc.split(".")
l = []
if len(urlsplit) < 3: return l
for item in urlsplit:
urlsplit = urlsplit[1:]
l.append(".".join(urlsplit))
if len(urlsplit) < 3:
return l
这个简单的代码将从所有有效的 URL 中获取根域名。
from urllib.parse import urlparse
url = 'https://www.google.com/search?q=python'
root_url = urlparse(url).scheme + '://' + urlparse(url).hostname
print(root_url) # https://www.google.com
这对我的目的有用。我想我会分享它。
".".join("www.sun.google.com".split(".")[-2:])