python搜索网站

时间:2012-10-08 09:03:20

标签: python

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

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 - >东京大学然后加入东京大学的IP地址

该网站为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()

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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