0

我尝试为大型日志文件创建过滤器,例如

Fri Oct  5 00:55:45 2012 [pid 2766] CONNECT: Client "157.82.250.217"
Fri Oct  5 00:55:45 2012 [pid 2765] [ftp] OK LOGIN: Client "157.82.250.217", anon password "mozilla@example.com"
Fri Oct  5 00:56:05 2012 [pid 2767] [ftp] FAIL DOWNLOAD: Client "157.82.250.217", "/pub/10.5524/100001_101000/100036/Gene_catalogue/Gene_catalogue.pep", 1638400 bytes, 81.81Kbyte/sec
Fri Oct  5 00:57:27 2012 [pid 3056] CONNECT: Client "157.82.250.217"
Fri Oct  5 00:57:27 2012 [pid 3055] [ftp] OK LOGIN: Client "157.82.250.217", anon password "-wget@"

我想将 IP 地址放入 http://www.ip-adress.com以获取组织名称,例如 157.82.250.217 --> University of Tokyo 然后在 IP 地址后面添加 University of Tokyo

该网站是http://www.ip-adress.com/ip_tracer/157.XX.xxx.xx ,源代码如下:

    <th>ISP of this IP [<a href="/isp" target="_self">?</a>]:</th>

    <td>

      University of Tokyo</td>

    </tr>

  <tr class="odd">

  <th>Organization:</th>

 <td>

  University of Tokyo</td>

  </tr>

  <tr class="even">

我是python的新手,我写了一些过滤时间,你能帮我在里面添加函数吗

import time
f= open("/opt/CLiMB/Storage1/log/vsftp.log")
def OnlyRecent(line):
if  time.strptime(line.split("[")[0].strip(),"%a %b %d %H:%M:%S %Y")>  time.gmtime(time.time()-(60*60*24*7)): 
    return True
return False
filename= time.strftime('%Y%m%d')+'.log'
f1= open(filename,'w')
for line in f:
if OnlyRecent(line):
        print line
        f1.write(line)
f.close()
f1.close()

谢谢!!

4

1 回答 1

0

我(通常在 StackOverflow 没有人)会编写你的代码。看看http://docs.python.org/library/urllib2.html。构建您的网址,将其传递给urllib2.urlopen并将结果读入字符串。然后提取您想要的数据。而已!

在您的情况下,简单的字符串操作应该足以提取数据,但您也可以检查http://www.crummy.com/software/BeautifulSoup/

于 2012-10-08T09:13:32.010 回答