1.fiddler打开 FiddlerScript
2.添加OnBeforeRequest代码
---fso.OpenTextFile("E:\\Git_wbiao\\Request.txt",8 ,true, true);【地址上文件,必须在本地已经创建好】
3.添加OnBeforeResponse代码
---fso.OpenTextFile("E:\\Git_wbiao\\Response.txt",8 ,true, true);【地址上文件,必须在本地已经创建好】
备注:
oSession方法及属性
这里我需要补充一下OpenTextFile方法:
object.OpenTextFile(filename[, iomode[, create[, format]]])
参数说明:
object:必选项。 object 应为 FileSystemObject 的名称。
filename:必选项。 指明要打开文件的字符串表达式。
iomode:可选项。 可以是三个常数之一: ForReading 、 ForWriting 或 ForAppending 。
create:可选项。 Boolean 值,指明当指定的 filename 不存在时是否创建新文件。 如果创建新文件则值为 True ,如果不创建则为 False 。 如果忽略,则不创建新文件。
format:可选项。 使用三态值中的一个来指明打开文件的格式。 如果忽略,那么文件将以 ASCII 格式打开。
其中:
iomode 参数可以是下列设置中的任一种:
ForReading 1 以只读方式打开文件。 不能写这个文件。
ForWriting 2 以写方式打开文件
ForAppending 8 打开文件并从文件末尾开始写。
我在此处直接写的8,意思就是在文件尾追加内容。
format 参数可以是下列设置中的任一种:
TristateTrue 以 Unicode 格式打开文件。
TristateFalse 以 ASCII 格式打开文件。
TristateUseDefault 使用系统默认值打开文件。
编码格式需要格外注意,我在用Java代码读出这些保存下来的txt文件时,会因为编码格式而报错,如果你的请求中没有中文,保存的时候可以考虑用ASCII的格式,UTF-8兼容ASCII码
4.抓包数据进行分割,并保存数据
import pymysql.cursors,urllib.parse from datetime import datetime import random,string,requests,json,codecs #from chardet import detect import chardet,shutil def get_str_btw(s,f,b): #内容分割 par = s.partition(f) return (par[2].partition(b))[0][:] def intest_X(): # 数据截取,并调用selectDate方法 datX2 = [] #file_address = 'E:\\Git_wbiao\\hello-Gao\\other\\Flask\\abc.txt' # 本调调试 file_address = 'E:\\Git_wbiao\\s_Request.txt' for A2 in open(file_address,'r',encoding='UTF-8'): #for A2 in open("abc.txt", "r", encoding='UTF-8'): print(A2) datX2.append(A2) check_AAA = get_str_btw(A2,'&','=') Report_probe_log = get_str_btw(A2, 'probe_log', '/track') Report_event = get_str_btw(A2, 'event=', '&') Report_eventPage = get_str_btw(A2, 'eventPage=', '&') eventProps = get_str_btw(A2, 'eventProps=', '&') Report_station = get_str_btw(A2, 'station=', '&') Report_eventProps = urllib.parse.unquote(eventProps) if check_AAA == 'eventPage': if Report_event and Report_probe_log and Report_eventPage and Report_station and Report_eventProps: print('没有page',Report_event,Report_eventPage) selectDate(event=Report_event, eventPage='', eventProps=Report_eventProps, station=Report_station,probe_log=Report_probe_log) else: if Report_event and Report_probe_log and Report_station and Report_eventProps: print('page',Report_event, Report_eventPage) selectDate(event=Report_event, eventPage=Report_eventPage, eventProps=Report_eventProps, station=Report_station, probe_log=Report_probe_log) def selectDate(event,eventPage,station,eventProps,probe_log): # 查询数据是否存在,不存在调用IntData方法 print('查询条件:',event,eventPage,'123') sql = "select event,eventPage from burying where event = '" + event + "' and eventPage = '" + eventPage + "';" coon = pymysql.connect(user='xx', passwd='xx', db='fast_last', host='10.0.3.xx', charset='utf8mb4') cursor = coon.cursor() S_eat = cursor.execute(sql) info = cursor.fetchall() coon.commit() cursor.close() coon.close() if info == (): print('插入埋点1:',event,eventPage) IntData(event=event, eventPage=eventPage, eventProps=eventProps, station=station, probe_log=probe_log) else: print('相同') pass def IntData(event,eventPage,station,eventProps,probe_log): # 数据写入 #sql = "select * from Burying_aliyun ;" # sql_dele = "DELETE FROM test_member.hy_member WHERE member_code = 1514;" sql_insert= "INSERT INTO `fast_last`.`burying` (`id`, `probe_log`, `event`, `eventPage`,`eventProps`,`station`, `create_time`) " \ "VALUES (NULL,'" + probe_log + "','" + event+ "','" + eventPage + "','" + eventProps + "','" + station + "','" + str(datetime.now()) +"');" coon = pymysql.connect(user='xxx', passwd='xxx', db='fast_last', host='10.0.3.xx', charset='utf8mb4') cursor = coon.cursor() S_eat = cursor.execute(sql_insert) info = cursor.fetchmany(S_eat) coon.commit() cursor.close() coon.close() return info def encoding_utf8(): #处理源文件与处理文件编码不一致问题【复制源文件再转码】 target_encoding = 'utf-8' file_address = 'E:\\Git_wbiao\\Request.txt' copy_file = 'E:\\Git_wbiao\\s_Request.txt' shutil.copy(file_address, copy_file) content_A = codecs.open(copy_file,'rb').read() sa_encoding = chardet.detect(content_A)['encoding'] content_B = content_A.decode(sa_encoding,'ignore') codecs.open(copy_file,'w',encoding=target_encoding).write(content_B) if __name__ == '__main__': encoding_utf8() intest_X()