为什么我在 Python 中使用 Lars 框架会遇到这个问题?

时间:2021-05-18 14:34:03

标签: python lars

我使用的是 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']

当然有数千行,但有人知道为什么会这样吗? 我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

很明显,在您的数据中,row.requestNone。您需要添加一个防范措施:

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)