(字典、子域名)合并去重 Python 脚本

前言

一般在做渗透测试的时候,前期对目标资产子域名进行信息搜集时,往往会从多个在线或者离线子域名采集工具中导出结果。然而每个工具平台导出的结果中都会有很多重复的子域名,如果靠手工对这些子域名结果进行合并去重的话,是非常的繁琐且低效率的,因此可以借助脚本工具替我们去完成这一复杂的整理工作,提高渗透效率。

0x01 脚本使用

将(字典、子域名)逐行保存到*.txt文件中,并且与脚本放在同一️目录下,然运行脚本即可:

 ~/ ls
a.com.txt       b.com.txt       merge_unique.py
 ~/ python merge_unique.py
查找当前目录下的txt文件:
共发现2个txt文件!
开始合并:
合并完毕!
开始去重:
去重完成!
 ~/ ls
a.com.txt             b.com.txt             merge_unique.py       out_merged_result.csv

执行完毕后,会将所有txt文件中的内容合并去重到新的 out_merged_result.csv 文件中。

0x02 脚本源码

Python合并去重脚本源码如下:

#coding=utf-8
import os
import pandas as pd
import glob
 
outFileName = 'out_merged_result.csv'
 
def merge():
    txt_list = glob.glob('*.txt')
    print(u'共发现%s个txt文件!'% len(txt_list))
    print(u'开始合并:')
    for i in txt_list:
        fr = open(i,'r').read()
        with open(outFileName,'a') as f:
            f.write(fr)
    print(u'合并完毕!')
 
 
def unique(file):
    df = pd.read_csv(file, header=0, error_bad_lines=False, quotechar=None, quoting=3)
    dataList = df.drop_duplicates()
    dataList.to_csv(file)
 
 
if __name__ == '__main__':
    print(u'查找当前目录下的txt文件:')
    merge()
    print(u'开始去重:')
    unique(outFileName)
    print(u'去重完成!')

参考文章

  • https://blog.csdn.net/cc1949/article/details/105690966
引用\[1\]中提到了一个函数`list_dict_duplicate_removal`,它可以用于列表中字典去重。这个函数使用了`reduce`函数和一个匿名函数来实现去重的功能。具体来说,它通过比较字典的内容来判断是否重复,并将不重复的字典添加到一个新的列表中。这个函数可以解决列表中字典去重的问题。\[1\] 另外,引用\[2\]提到了在使用集合(set)或字典(dict)时,要求元素是可哈希的。可哈希的意思是元素具有唯一的标识符,可以用来进行索引。而列表(list)中的元素是不可哈希的,因此不能直接使用集合或字典进行去重操作。\[2\] 如果你想对字典的键名进行去重,可以使用以下方法: 1. 遍历字典的键名,将其添加到一个集合(set)中,这样重复的键名会被自动去重。 2. 将去重后的键名重新构建成一个新的字典。 下面是一个示例代码: ```python def remove_duplicate_keys(dictionary): unique_keys = set() new_dictionary = {} for key in dictionary.keys(): if key not in unique_keys: unique_keys.add(key) new_dictionary\[key\] = dictionary\[key\] return new_dictionary ``` 这个函数会遍历字典的键名,将不重复的键名添加到一个集合中,并同时构建一个新的字典。最后返回新的字典,其中的键名是去重后的结果。\[1\] #### 引用[.reference_title] - *1* *2* [Python字典列表去重](https://blog.csdn.net/aloalololo/article/details/116932973)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【小方法】python列表套字典去重、列表套列表去重](https://blog.csdn.net/wangzhuanjia/article/details/125007156)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Luckysec

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值