from urllib.parse import urlsplit
# On Py2, from urlparse import urlsplit
url = "http://www.nba.com/sports"
domain = urlsplit(url).hostname
# split off the last two components, then join them back together to make
# the second level domain
secondlevel = '.'.join(domain.rsplit('.', 2)[-2:])
print(secondlevel)
在每个结果后打印
url
,您将看到需要执行的操作:在这里之后,很明显我们需要做的就是在
/
处拆分。不要过于复杂:)正如注释中提到的@Mark,您还可以使用^{} :
如果需要的话,你可以从第一个
.
中去掉所有的内容,但是根据你正在做的事情,你可能不需要这样做。你知道吗注意,如果您使用的是python2,那么模块是^{} 。你知道吗
正确的解决方法:不要重新发明轮子,尽可能多地使用the existing libraries:
这让你
nba.com
。你知道吗相关问题 更多 >
编程相关推荐