(strs)
return domain
可以处理的格式
    1, http://abc.baidu.com/asdas
    2, abc.baidu.com
    3, 1.1.1.1  return ''
可以处理的格式
    1, http://abc.baidu.com/asdas
    2, abc.baidu.com
    3, 1.1.1.1  return ''
可以处理的格式 1 http : // abc . baidu . com / asdas 2 abc . baidu . com 3 1 . 1 . 1 . 1 return
def set_domain(strs):
    '''
    可以处理的格式
    1, http://abc.baidu.com/asdas
    2, abc.baidu.com
    3, 1.1.1.1  return ''
    '''
    host = ''
    domain = ''
    if 'http://' in strs:
        host = urlparse.urlparse(strs)[1].split(':')[0]
    else:
        host = strs
    keyword_list = host.split('.')
    if len(keyword_list) == 2:
        domain = host

    elif len(keyword_list) == 3:
        if 'com.cn' in host:
            domain = host
        elif 'net.cn' in host:
            domain = host
        else:
            domain = '.'.join(host.split('.')[1:])

    elif len(keyword_list) > 3:
        count = 0
        for i in keyword_list:
            try:
                int(i)
                count += 1
            except Exception, e:
                break
        if count == 4:
            domain = ''
        else:
            if keyword_list[-1] == 'cn' and keyword_list[-2] in ['com', 'edu', 'gov', 'org', 'net']:
                domain = '.'.join(keyword_list[-3:])
            elif keyword_list[-1] in ['com', 'net', 'org','cc','me']:
                domain = '.'.join(keyword_list[-2:])
            elif keyword_list[-1] == 'cn':
                domain = '.'.join(keyword_list[-2:])
            else:
                domain = host
    return domain
def set_domain ( strs ) : host = '' domain = '' if 'http://' in strs : host = urlparse . urlparse ( strs ) [ 1 ] . split ( ':' ) [ 0 ] else : host = strs keyword_list = host . split ( '.' ) if len ( keyword_list ) == 2 : domain = host elif len ( keyword_list ) == 3 : if 'com.cn' in host : domain = host elif 'net.cn' in host : domain = host else : domain = '.' . join ( host . split ( '.' ) [ 1 : ] ) elif len ( keyword_list ) > 3 : count = 0 for i in keyword_list : try : int ( i ) count += 1 except Exception , e : break if count == 4 : domain = '' else : if keyword_list [ - 1 ] == 'cn' and keyword_list [ - 2 ] in [ 'com' , 'edu' , 'gov' , 'org' , 'net' ] : domain = '.' . join ( keyword_list [ - 3 : ] ) elif keyword_list [ - 1 ] in [ 'com' , 'net' , 'org' , 'cc' , 'me' ] : domain = '.' . join ( keyword_list [ - 2 : ] ) elif keyword_list [ - 1 ] == 'cn' : domain = '.' . join ( keyword_list [ - 2 : ] ) else : domain = host return domain