91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python實現從log日志中提取ip的方法【正則提取】

發布時間:2020-08-23 20:53:04 來源:腳本之家 閱讀:499 作者:開心果汁 欄目:開發技術

本文實例講述了Python實現從log日志中提取ip的方法。分享給大家供大家參考,具體如下:

log日志內容如下(myjob.log):

124.90.53.68 - - [05/Feb/2018 11:37:07] "GET /favicon.ico HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:44] "GET / HTTP/1.1" 200 -
61.148.245.145 - - [05/Feb/2018 12:37:44] "GET /apple-touch-icon-120x120-precomposed.png HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:44] "GET /apple-touch-icon-120x120.png HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:45] "GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:45] "GET /apple-touch-icon.png HTTP/1.1" 404 -
61.148.245.145 - - [05/Feb/2018 12:37:45] "GET /static/favicon.ico HTTP/1.1" 200 -
101.226.33.218 - - [05/Feb/2018 13:07:39] "GET / HTTP/1.1" 200 -
101.226.33.219 - - [05/Feb/2018 13:09:46] "GET / HTTP/1.1" 200 -
101.226.33.219 - - [05/Feb/2018 13:09:46] "GET /static/youkulogo.png HTTP/1.1" 200 -
101.226.33.219 - - [05/Feb/2018 13:09:46] "GET /static/iqiyi.png HTTP/1.1" 200 -
101.226.33.219 - - [05/Feb/2018 13:09:46] "GET /static/qqlogo.png HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:45] "GET / HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:47] "GET /static/youkulogo.png HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:48] "GET /static/qqlogo.png HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:48] "GET /static/iqiyi.png HTTP/1.1" 200 -
124.202.223.62 - - [05/Feb/2018 14:29:49] "GET /static/favicon.ico HTTP/1.1" 200 -

提取ip:

# encoding: utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import pandas as pd
import re
import time
import requests
time1=time.time()
######函數功能:能夠提取ip地址,并且去重################
def read_file(input_file_name,output_file_name):
  _fLog = open(input_file_name)
  sep = '\n'
  ip_list=[]
  for each in _fLog:
    ip=re.findall(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])',str(each),re.S)
    ip_list.append(ip[0])
  # 列表去重:通過set方法進行處理
  ids = list(set(ip_list))
  print "共解析ip個數:%s "% len(ids)
  ##寫出數據到本地
  # 設置輸出文件路徑
  out = open(output_file_name, "a")
  # out.write("ip" + sep)
  for each in ids:
    print each
    out.write(each + sep)
  ##關閉連接
  out.close()
  _fLog.close()
  print "ip提取完畢~~"
####主函數################
if __name__ == '__main__':
  input_file_name = "C:/myjob.log"
  output_file_name = "c:/myjob.txt"
  read_file(input_file_name, output_file_name)
  time2 = time.time()
  print u'總共耗時:' + str(time2 - time1) + 's'

運行結果:

共解析ip個數:5
61.148.245.145
124.90.53.68
124.202.223.62
101.226.33.219
101.226.33.218
ip提取完畢~~
總共耗時:0.000999927520752s
Process finished with exit code 0

PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:

JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript

正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg

更多關于Python相關內容可查看本站專題:《Python正則表達式用法總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

邮箱| 卢湾区| 肇庆市| 旺苍县| 新津县| 东乡族自治县| 麦盖提县| 兖州市| 类乌齐县| 建阳市| 金秀| 涞源县| 噶尔县| 阿拉善盟| 永顺县| 都江堰市| 枣强县| 潜山县| 崇州市| 修文县| 封开县| 大英县| 庆云县| 邵武市| 绩溪县| 黎城县| 柏乡县| 琼结县| 金川县| 剑阁县| 包头市| 日土县| 将乐县| 六枝特区| 遂平县| 沽源县| 旺苍县| 青浦区| 会理县| 和田县| 农安县|