python 網域名稱分析工具實現代碼

來源:互聯網
上載者:User

代碼如下: 複製代碼 代碼如下:import sys, urllib
import datetime,time
def getDate():
strday=datetime.datetime.now().__str__()
strday=strday.split()[0]
return strday
#url = "http://www.kingnic.com/list/2009-06-16.txt"
def getUrl(dateStr=None):
baseUrl ="http://www.kingnic.com/list/"
if dateStr:
return baseUrl+dateStr+".txt"
thisDate = getDate();
if not thisDate:
print "Error Date!"
return None;
url = baseUrl+thisDate+".txt"
return url
def getSource(url):
source = urllib.urlopen(url).read()
return source

def save(source,filename="domains.txt"):
fp = open(filename,"w")
fp.write(source)
fp.close()
return True;
def loadList(fileName="domains.txt"):
fp = open("domains.txt","r")
source = fp.readlines()
fp.close()
return source;
def getPrefix(domain):
return domain.split('.')[0]
def getPostfix(domain):
return domain.split('.')[1]
def hasMidLine(domain):
if '-' in domain:
return True
else:
return False
def parser(domains):
max =4
min =0
keyword =('sky','see','job')
result=[]

len_num =0;
mid_line_num =0;

for domain in domains:
prefix = getPrefix(domain)
postfix = getPostfix(domain)
domainlen = len(prefix)
if (domainlen < min) or (domainlen > max):
len_num +=1
continue
if hasMidLine(prefix):
mid_line_num +=1
continue
result.append(domain)

print " log : \n"
print "all: \t",len(domains)
print "len not in [%s,%s] \t: %s"%(max,min,len_num)
print "contain '-' :\t",mid_line_num
print "remain:\t",len(result)
return result;

if __name__ == "__main__":
url = getUrl()
source = getSource(url)
save(source)
domains =loadList()
result = parser(domains)
save("".join(result),"result.txt")
print("\n\n\nfinished!!")

輸出檔案:
domains.txt : kingnic.com 據當天釋放的 網域名稱;
result.txt : 符合過濾條件的網域名稱;
log輸出: 複製代碼 代碼如下:all: 55500
len not in [4,0] : 55019
contain '-' : 32
remain: 449
finished!!

對 尾碼、長度和有無“-”過濾,過濾條件有點少,其它以後如有需要再加。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.