python - 通过域名将网络流量与授权列表进行比较

标签 python regex

我正在尝试解析网络流量并将流量中的域名与最常见网站的列表进行比较。目的是打印不在常见网站列表中的所有网站名称


with open('/Users/downloads/scripting_for_security/resources/top_100.txt') as f:
    safeAdd = f.readlines(),


with open('/Users/downloads/scripting_for_security/resources/traffic_log.txt') as n:
    netTraffic = n.readlines(),

domainTraffic = re.findall(r'\s(?:www.)?(\w+.com)', netTraffic)


for i in safeAdd:
    for e in domainTraffic:
        if i != e:
            print(e)

出现类型错误

TypeError Traceback (most recent call last) in 8 netTraffic = n.readlines(), 9 ---> 10 domainTraffic = re.findall(r'\s(?:www.)?(\w+.com)', netTraffic) 11 12

~/anaconda3/lib/python3.7/re.py in findall(pattern, string, flags) 221 222 Empty matches are included in the result.""" --> 223 return _compile(pattern, flags).findall(string) 224 225 def finditer(pattern, string, flags=0):

TypeError: expected string or bytes-like object

最佳答案

netTraffic 是一个列表,按照https://docs.python.org/3/tutorial/inputoutput.html

findall 需要字符串类型的第二个参数 https://docs.python.org/3/library/re.html#re.findall

关于python - 通过域名将网络流量与授权列表进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58193414/

相关文章:

python - 在大字符串列表中查找小字符串列表 (Python)

python - Pandas 根据日期组合行

javascript - 使用javascript替换特定位置的所有空格

regex - PL/SQL函数返回带有正则表达式特殊字符的字符串

regex - 如何查找包含 3 组双字母的行(这些组可能包含也可能不包含相同的字母)

javascript - 这个正则表达式前瞻有什么问题?

python - 计算 Pandas 中的元素

python - 最大子图

python - 在 Python Pandas Pivot 中使用 stats.trim_mean ass aggfunc

java - Android正则表达式模式获取金额$