测试脚本
import cookielib,urllib2 cj=cookielib.CookieJar() from pyquery import PyQuery as pq opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) field_list=["topic","name","telephone","school","department","classes","title","email","type1"] class Form_data(object): topic=' ' name=' ' telephone=' ' school=' ' department=' ' classes=' ' email= ' ' type1=' ' title=' ' def change(self,*arg,**kwarg): # for key in kwarg.keys(): # setattr(self,key,kwarg[key]) result_dict={} for i in range(len(arg)-1): setattr(self,field_list[i],arg[i]) result_dict.setdefault(field_list[i],arg[i]) return result_dict def convert_to_urlparam(form_data): urlparam_result='' for i in field_list: if getattr(form_data,i) ==" ": continue if i =='classes': urlparam_result=urlparam_result+'class=%s&'%getattr(form_data,'classes') continue if i=='type1': urlparam_result=urlparam_result+'type=%s&'%getattr(form_data,'type1') continue urlparam_result=urlparam_result+'%s=%s&'%(i,getattr(form_data,i)) return urlparam_result def load_data(path): try: data_file=open(path) except Exception: print "can not open the file" else: test_data_list=[] for i in data_file.readlines()[1:]: data=i.split('\t') data[-1]=data[-1][:-1] test_data_list.append(data) return test_data_list finally: data_file.close() def main(): form_data= Form_data() data_set=load_data('data.txt') index=0 for data in data_set: index=index+1 result_dict= form_data.change(*data) base_url='http://test.emanoni.com/contributor' home=opener.open(base_url,data=convert_to_urlparam(form_data)[:-1]) d=pq(home.read().decode('utf-8')) error=d('.error').text() print "=======================================" # for field in field_list: # print field+"="+result_dict[field] print "INDEX:"+str(index) print "POST:"+base_url+'?'+convert_to_urlparam(form_data) if error==None: error="OK" print "RESULT:"+error print "=======================================" if __name__=='__main__': main()