我使用的是Lars框架(https://lars.readthedocs.io/en/latest/index.html)分析存储在txt文件中的Apache服务器的示例日志。我想把每个日志的path_str存储在一个列表中,所以我这样做:
from lars import apache
path_logs = []
with open('sample.txt', 'r') as f:
with apache.ApacheSource(f) as source:
for row in source:
path_logs.append(row.request.url.path_str)
print(path_logs)
理论上应该是正确的,但我得到了这个错误:
'NoneType' object has no attribute 'url'
有趣的是,如果我创建一个变量来计数并停在某一行,这就行了:
with open('sample.txt', 'r') as f:
with apache.ApacheSource(f) as source:
count = 0
for row in source:
path_logs.append(row.request.url.path_str)
count += 1
if(count == 5):
break
print(paht_logs)
Out:
['/api/buscador/filtros',
'/api/buscador/busqueda',
'/api/buscador//busqueda',
'/api/buscador/filtros',
'/api/buscador/busqueda']
当然有成千上万的争吵,但有人知道为什么会发生这种情况吗?我是不是错过了什么?
很明显,在您的数据中,row.request
就是None
。你需要添加一个防范措施:
import logging
with open('sample.txt', 'r') as f:
with apache.ApacheSource(f) as source:
for row in source:
if row.request is None:
logging.warning('Skipping row %r', row)
else:
path_logs.append(row.request.url.path_str)