python在json文件中提取IP和域名

# qianxiao996精心制作
#博客地址:https://blog.csdn.net/qq_36374896
import re

def openjson(path):
    f = open(path, "r")
    data = f.read()
    f.close()
    return data

def query(path):
    URL = []
    IP = []
    data = openjson(path)
    #print(data)

    #提取域名
    url = re.findall(r'"rrname":.*",\n',data)
    for j in url:
        j=j.split('"')
        if j[3] not in URL:  #列表去重
            URL.append(j[3])

    #提取IP地址
    iplist = re.findall(r'\d+\.\d+\.\d+\.\d+', data)
    for i in iplist:
        if  i not in IP:  #列表去重
            IP.append(i)

    #域名写入文件
    URLfile = open(path[:-4]+'URL.txt', 'w', encoding='utf-8')
    for i in URL:
        URLfile.write(str(i)+'\n')
    URLfile.close()


    #IP 写入文件
    IPfile = open(path[:-4]+'IP地址.txt', 'w', encoding='utf-8')
    for i in IP:
        IPfile.write(str(i)+'\n')
    IPfile.close()




if __name__=="__main__":
    #filepath = input('请输入json文件路径:')
    filepath = "jsonfile.txt"
    data = query(filepath)




#print(a)

posted @ 2018-12-13 20:49  浅笑996  阅读(375)  评论(0编辑  收藏  举报