由于项目需要处理crash文件,crash记录是存储在一张csv表中,具体的内容在网页上,因此需要用python写一个脚本,主要是可以从csv表中抽出crash数据,然后从网上可以通过爬虫爬取数据,这里记录下,以防以后忘记
# -*- coding: cp936 -*-
import urllib2 #网络爬虫库
import csv #excel表库
import time #时间库
crash_data_dict = {}
index = 1
reader = csv.reader(file('C:\Users\zhouwei\Desktop\crash_data.csv', 'rb'))
for line in reader:
if reader.line_num == 1:
continue
if len(line) == 5:
crash_data_dict[index] = {"id":line[0], "count":line[2], "md5":line[4]}
index = index + 1
url_prefix = 'http://ip:port/reports/api/v1/reports/'
filename = 'crash_data' + time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time())) + '.txt'
for k in crash_data_dict:
url = url_prefix + crash_data_dict[k]["id"]
response = urllib2.urlopen(url)
crash_content = response.read()
# print crash_content
fp = open(filename, 'a')
fp.write("\n-------------------------" + crash_data_dict[k]["count"] + "\n")
fp.writelines(crash_content)
fp.close()
由于公司机密,这里就直接将实际ip和port隐匿了
整体处理还是比较简单,这里还是要根据实际项目中crash_data.csv格式来进行处理