python 提取主域名和子域名代码——先根据规则提取,如果有问题,则使用tldextract
2023-09-14 09:11:54 时间
import tldextract def extract_domain(domain): suffix = {'.com','.la','.io', '.co', '.cn','.info', '.net', '.org','.me', '.mobi', '.us', '.biz', '.xxx', '.ca', '.co.jp', '.com.cn', '.net.cn', '.org.cn', '.mx','.tv', '.ws', '.ag', '.com.ag', '.net.ag', '.org.ag','.am','.asia', '.at', '.be', '.com.br', '.net.br', '.name', '.live', '.news', '.bz', '.tech', '.pub', '.wang', '.space', '.top', '.xin', '.social', '.date', '.site', '.red', '.studio', '.link', '.online', '.help', '.kr', '.club', '.com.bz', '.net.bz', '.cc', '.band', '.market', '.com.co', '.net.co', '.nom.co', '.lawyer', '.de', '.es', '.com.es', '.nom.es', '.org.es', '.eu', '.wiki', '.design', '.software', '.fm', '.fr', '.gs', '.in', '.co.in', '.firm.in', '.gen.in', '.ind.in', '.net.in', '.org.in', '.it', '.jobs', '.jp', '.ms', '.com.mx', '.nl','.nu','.co.nz','.net.nz', '.org.nz', '.se', '.tc', '.tk', '.tw', '.com.tw', '.idv.tw', '.org.tw', '.hk', '.co.uk', '.me.uk', '.org.uk', '.vg'} domain = domain.lower() names = domain.split(".") if len(names) >= 3: if ("."+".".join(names[-2:])) in suffix: return ".".join(names[-3:]), ".".join(names[:-3]) elif ("."+names[-1]) in suffix: return ".".join(names[-2:]), ".".join(names[:-2]) print "New domain suffix found. Use tld extract domain..." pos = domain.rfind("/") if pos >= 0: # maybe subdomain contains /, for dns tunnel tool ext = tldextract.extract(domain[pos+1:]) subdomain = domain[:pos+1] + ext.subdomain else: ext = tldextract.extract(domain) subdomain = ext.subdomain if ext.suffix: mdomain = ext.domain + "." + ext.suffix else: mdomain = ext.domain return mdomain, subdomain print extract_domain("baidu.com") == ("baidu.com", "") print extract_domain("www.baidu.com") == ("baidu.com", "www") print extract_domain("www.xx.com.cn") == ("xx.com.cn", "www") print extract_domain("www.xxx.gov.cn") == ("gov.cn", "www.xxx") print extract_domain("abc.www.xxx.net.co") == ("xxx.net.co", "abc.www") print extract_domain("abcwwwxxx.local") == ("local", "abcwwwxxx") print extract_domain("abcwwwxxxlocal") == ("abcwwwxxxlocal", "") print extract_domain("attack/www.baidu.com") == ("baidu.com", "attack/www") print extract_domain("xx.attack/xxx.baidu.com") == ("baidu.com", "xx.attack/xxx") print extract_domain("attack/xxx.baidu.com") == ("baidu.com", "attack/xxx") print extract_domain("xxx.baidu.new_suffix") == ("new_suffix", "xxx.baidu") print extract_domain("attack/xxx.baidu.new_suffix") == ("new_suffix", "attack/xxx.baidu")
相关文章
- [Python]架设python虚拟环境以及部署PythonWeb服务
- Python字符串切片操作知识详解
- python-django项目-每次重启电脑需要启动的虚拟机服务_20191124
- 你没见过的python语法
- Python: 爬虫入门-python爬虫入门教程(非常详细)
- Python语言学习之文件夹那些事:python和文件夹的使用方法之详细攻略
- Python语言学习:python语言代码调试—异常处理之详细攻略
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(LabelFrame/Checkbutton/Radiobutton)
- Python之sklearn:LabelEncoder函数简介(编码与编码还原)、使用方法、具体案例(在数据缺失和test数据内存在新值(train数据未出现过)环境下的数据LE化)之详细攻略
- 成功解决tensorflowcontriblearnpythonlearndatasetsbase._internal_retry.<locals>.wrap.<locals>.wrapp
- 520晚上,我用python破解了前女友发来的加密pdf文件,结果却发现...
- 〖Python接口自动化测试实战篇⑧〗- 小案例 - 使用python实现接口请求 [查询天行数据]
- 从零开始学习python | 实例讲解如何制作Python模式程序
- python-arima模型statsmodels库实现-有数据集
- python基础===Python 迭代器模块 itertools 简介
- python : itertools 中的 islice : 获取迭代器结果的切片,消耗迭代器
- 【基础知识】4、python-opencv 入门基础知识