这篇文章给大家分享的是有关如何使用python实现切割url得到域名、协议、主机名等各个字段的例子的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
实现代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
功能: 对URL进行分割,基于urlparse, publicsuffix, urllib编写
'''
from urlparse import urlparse
import codecs
from publicsuffix import PublicSuffixList
from urllib import splitport
import re
def domain_split(server_domain):
'''
server_domain为网站所用服务名+域名
分割域名, 得到前缀(服务名)、主机域名、后缀(顶级域名)
输入www.baidu.com,输出'www', 'baidu', 'com'
输入172.31.137.240,输出'', '172.31.137.240', ''
'''
PSL_FILE = codecs.open('public_suffix_list.dat', encoding='utf8')
psl = PublicSuffixList(PSL_FILE)
domain = psl.get_public_suffix(server_domain)
# 取域名的第一个字段,即第一个'.'之前的为主机域名, 后面为顶级域名,前面为所使用的服务
if '.' in domain:
server = server_domain[:-len(domain)]
host = domain[:domain.index('.')]
top = domain[domain.index('.'):]
hostname = server + host + top
else: # 说明提取域名失败,例如172.31.137.240等IP形式,此时全部当作主机域名
server = ''
host = server_domain
top = ''
hostname = server_domain
return server, host, top, hostname
def url_split_new(url):
'''
url分割
'''
if not url.startswith('http'): # 补全协议,否则urlparse出错
url = 'http://' + url
parts = urlparse(url)
# 服务+域名'www.baidu.api.com.cn'切分
server, host, top, hostname = domain_split(parts.netloc)
host, port = splitport(host)
if port == None: port = ''
return {'protocol': parts.scheme, 'hostname': hostname, 'path': parts.path}
if __name__ == '__main__':
print url_split_new('http://www.baidu.com/')
print url_split('http://www.baidu.com/')
感谢各位的阅读!关于“如何使用python实现切割url得到域名、协议、主机名等各个字段的例子”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。