【问题标题】:Regex to get URL from log file and store it in a dictionary in Python正则表达式从日志文件中获取 URL 并将其存储在 Python 中的字典中
【发布时间】:2021-03-08 01:02:42
【问题描述】:
import re

filename = "access.log"

path = ""

with open (path + filename, "r") as logfile:
  count = 0
  for line in logfile:                            # Loops through the log file
    regex = ('(?:(GET|POST) )(\S+)')              # Stores the regex
    url = re.findall(regex, line)                 # Uses the findall method and stores it in url variable
    print(url[0][1])                              # Prints out a list of URLs

这是一个日志文件的例子

access.log

209.160.24.63 - - [01/Feb/2021:18:22:17] "GET /product.screen?productId=BS-AG-G09&JSESSIONID=SD0SL6FF7ADFF4953 HTTP 1.1" 200 2550 " http://www.google.com/productid=12wdef" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5" 422

我得到了粗体的 URL,但我现在想将其拆分并存储在 python 的字典中。

【问题讨论】:

    标签: python regex dictionary


    【解决方案1】:

    既然你已经得到了加粗的字符串,你可以用字符串中出现的第一个空格来分割它

    s = "GET /product.screen?productId=BS-AG-G09&JSESSIONID=SD0SL6FF7ADFF4953"
    s.split(" ", 1)
    

    应该返回

    ['GET', '/product.screen?productId=BS-AG-G09&JSESSIONID=SD0SL6FF7ADFF4953']
    

    您可以在之后相应地转换数据。

    【讨论】:

      【解决方案2】:
      import re
      
      filename = "access.log"
      dictionary = {}
      list_resources = []
      count = 0
      
      with open (filename, "r") as logfile:
      
        for line in logfile:                            # Loops through the log file
          regex = ('(?:(GET|POST) )(\S+)')              # Stores the regex
          url = re.findall(regex, line)[0][1]           # Uses the findall method and stores it in url variable
          list_resources.append(url)
                
          resource = re.split("\?", url)[0]
          parameters = re.split("\?", url)[1]
      
          parameter = re.split("&", parameters)
          param_dict = {}
      
          for i in parameter:
            key = re.split('=', i)[0]
            value = re.split('=', i)[1]
            param_dict[key] = value
      
          dictionary[count] = {'resource': resource, 'parameters': param_dict}
          count += 1
      
      # print(list_resources)
      
      print(dictionary)
      
      

      想出了我想做的事情,拆分 URL 并将资源和参数存储在字典中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-06
        • 2016-02-03
        • 1970-01-01
        • 2019-12-22
        • 1970-01-01
        • 2019-11-19
        • 1970-01-01
        相关资源
        最近更新 更多