您的位置:首页

爬中国联通通话记录、身份认证、上网记录等信息

2017-08-15 17:11 239 查看
#coding=utf8
import requests
import re
import time
import calendar
import gevent
import json
import chardet
import traceback

from Logger import Logger
Logger(logname='myrequests.txt', loglevel=1, logger="myrequests").getlog()
logger=Logger(logname='log5.txt', loglevel=1, logger="unicome").getlog()

from myrequests import Myrequests

from gevent import monkey
monkey.patch_socket()
monkey.patch_select()
# monkey.patch_all(socket=True, select=True)
from gevent.pool import Pool,Group

import sys
reload(sys)
sys.setdefaultencoding('utf8')

class ChinaUnicom(Myrequests):

headerx={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'}
login_url='https://uac.10010.com/portal/Service/MallLogin'
info_url='https://uac.10010.com/cust/infomgr/anonymousInfoAJAX'

def __init__(self,phone,server_passwd):
#self.myreq=Myrequests()
logger.info('传入的账号和密码分别是:%s  %s' % (phone, server_passwd))
super(ChinaUnicom,self).__init__()
self.session=requests.session()
self.phone=phone
self.server_passwd=server_passwd

self.group=Group()

self.dates={}

self.info={}
self.info.update({'phone':self.phone})
self.bill=[]

def deco(func):
def wrapper(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except Exception, e:
logger.error('\n%s\n方法 %s发生错误,原因是: %s\n' % ('-'*100,func.__name__, traceback.format_exc()))
return wrapper

@deco
def login(self,times=1):

datax={
'callback':'jQuery17208315958887266249_1502520335284',
'req_time':'1502520347528',
'redirectURL':'http://www.10010.com',
'userName':self.phone,
'password':self.server_passwd,
'pwdType':'01',
'productType':'01',
'redirectType':'01',
'rememberMe':'1',
'_':'1502520347531',
}

resp=self.get(self.login_url,params=datax,headers=self.headerx)  ##可以不要真实浏览器ua

logger.debug('登录返回的内容是:%s'% resp.content)

if 'resultCode:"0000"' in resp.content:
logger.debug('\033[5;31;0m%s登陆成功\033[0m'%self.phone)
return 0,resp.content
elif   'resultCode:"7007" ' in resp.content:
logger.warning(resp.content)
return 7007,resp.content
elif 'resultCode:"7001"' in resp.content:
logger.warning('验证码错误')
return 7001,resp.content
elif 'resultCode:"7009"' in resp.content:    #系统忙,请稍后再试7009,重试10次
logger.warning('系统忙,请稍后再试')
if times<10:
return self.login(times+1)
else:
return 7009,resp.content
elif 'resultCode:"7005"' in resp.content:
logger.warning('您的号码所属省份系统正在升级,请稍后再试。')
return 7005,resp.content
else:
logger.error('登录异常返回的 内容是:%s'%resp.content)
resultCode=re.search('resultCode:"(.*?)"',resp.content).group(1)
return  resultCode,resp.content

@deco
def get_e3(self,times=1):
###检查是否登录了,非常重要的步骤
t1 = time.time()
resp = self.post('http://iservice.10010.com/e3/static/check/checklogin/', data={'_': '1502766200122'})  ####这个十分重要,即使登录成功了,不请求这个会造成请求不到通话记录。要在第一次登录后立马抓包才能抓到,后面情况下无论怎么刷新通话记录页面,fiddler都抓不到这个了。
#resp=self.get('http://iservice.10010.com/e3/static/common/mall_info?callback=jQuery17208521103973034769_1502784024036&_=1502784025299')

logger.debug('checklogin返回:%s' % resp.content)
try:
json.loads(resp.content)['userInfo']
except:
logger.debug('第%s次checklogin返回不ok'%times)
if times<5:
return self.get_e3(times+1)
else:
return 'check login error'

dictx = json.loads(resp.content)['userInfo']
self.info.update({'custlvl': dictx['custlvl'], 'custName': dictx['nickName'], 'sex': dictx['custsex'],
'certnum': dictx['certnum'], 'certaddr': dictx['certaddr'],
"packageName": dictx["packageName"], "paytype": dictx["paytype"],
"subscrbstat": dictx["subscrbstat"], "opendate": dictx["opendate"][:8],
"custName": dictx["custName"],
"brand_name": dictx["brand_name"],
})
logger.debug('请求e3接口消耗时间:%s' % (time.time() - t1))

@deco
def get_infomgrInit(self):
'''获取个人信息'''
resp=self.post(self.info_url)
logger.info('%s的个人信息:%s'%(self.phone,resp.content))

@deco
def get_phone_info(self):
'''获取号码信息'''
resp=self.post('http://iservice.10010.com/e3/static/query/searchPerInfoDetail/?_=1504753169884')
logger.info('%s的号码信息:%s' % (self.phone, resp.content))
dictx=json.loads(resp.content)
self.info.update({
"landlvl":dictx["result"]["MyDetail"]["landlvl"],"pukcode":dictx["result"]["pukcode"],
})
#return resp.content

@deco
def get_contract_period_info(self,times=1):  ###有时候会获取出错没得到合约信息,重试times次
'''获取合约期信息'''
resp = self.post('http://iservice.10010.com/e3/static/query/contractPeriodQuery?_=1504753170508&accessURL=http://iservice.10010.com/e4/query/basic/personal_xx_iframe.html')
logger.info('%s的合约期信息:%s' % (self.phone, resp.content))
if '尊敬的客户,您的号码无合约' in resp.content:
self.info.update({
"activityName":"",
"effectTime":"",
"endTime":"",
})
elif '"errMessage":"尊敬的客户,暂时无法为您提供服务' in resp.content:
if times<5:                                                        ###重试5次,再提示无法提供服务,就不再重试了。
return self.get_contract_period_info(times+1)
else:
self.info.update({
"activityName": "",
"effectTime": "",
"endTime": "",
})
else:
dictx = json.loads(resp.content)
self.info.update({
"activityName": dictx["result"]["activityInfo"][0]["activityName"],
"effectTime": dictx["result"]["activityInfo"][0]["effectTime"][:8],
"endTime": dictx["result"]["activityInfo"][0]["endTime"][:8],
})

@deco
def get_caller_location(self):
'''手机归属地查询,用了第三方的接口查询归属地,联通官网自带的归属地查询有次数限制'''
datax={
'm': self.phone,
'output': 'json',
'callback': 'querycallback',
'timestamp': '1504690861415',
}
resp=self.post('http://v.showji.com/Locating/showji.com2016234999234.aspx?m=13148804508&output=json&callback=querycallback×tamp=1504690861415',data=datax)
matchx=re.search('"Province":"(.*?)","City":"(.*?)"',resp.content).groups()
logger.info('手机号码 %s 的归属地是: %s %s'%(self.phone,matchx[0],matchx[1]))
self.info.update({'caller_location':matchx[1]})
return matchx

@staticmethod
def get_year_month(n):
'''获取n个月前的年份和月份和天数'''
day_now = time.localtime()
year0 = day_now.tm_year
month0 = day_now.tm_mon

monthx = month0 - n
yearx = year0
if monthx < 1:
yearx = year0 - n
monthx = month0 + 12 - n

day_begin = '%d%02d01' % (yearx, monthx)  # 月初肯定是1号
wday, monthRange = calendar.monthrange(yearx, monthx)  # 得到本月的天数 第一返回为月第一日为星期几(0-6), 第二返回为此月天数
day_end = '%d%02d%02d' % (yearx, monthx, monthRange)

day_begin2='%d-%02d-01' % (yearx, monthx)
day_end2 = '%d-%02d-%02d' % (yearx, monthx, monthRange)

return yearx,monthx,day_begin,day_end,monthRange,day_begin2,day_end2

@deco
def get_all_call_detail(self):
'''获取通话记录'''
day_now = time.localtime()
year0=day_now.tm_year
month0=day_now.tm_mon

self.call_details = []

def get_call_month(m):
yearx, monthx, day_begin, day_end, monthRange, day_begin2, day_end2 = self.get_year_month(m)

datax={
'pageNo': '1',
'pageSize': '200',
'beginDate': day_begin,
'endDate':day_end,
}
url='http://iservice.10010.com/e3/static/query/callDetail?_=1502703492095&accessURL=http://iservice.10010.com/e4/query/bill/call_dan-iframe.html?menuCode=000100030001&menuid=000100030001'
get_times=0
while (get_times<5):
resp = self.post(url, data=datax )
if '尊敬的客户,出了一点点问题,请您稍候' in resp.content:
logger.warning( '尊敬的客户,出了一点点问题,请您稍候')
get_times+=1
else:
break
logger.info('通话记录:%s'%resp.content)
dictx = json.loads(resp.content)
if '"respDesc":"无详单记录' in resp.content:
pass
else:
self.call_details.append(dictx)
logger.debug('月初日期为:%s 月末日期为:%s' % (day_begin, day_end))

# poolx=Pool(6)
# poolx.map(get_call_month,[i for i in range(6)])
[self.group.add(gevent.spawn(get_call_month,m)) for m in range(6)]
#return self.call_details

@deco
def get_current_month_bill(self,times=1):
'''获取当前月份的账单'''   #http://iservice.10010.com/e4/index_server.html
headerx={'Accept':'application/json, text/javascript, */*; q=0.01'}
resp = self.post('http://iservice.10010.com/e3/static/query/userinfoquery?_=1504092545814',headers=headerx)
logger.info('获取当前月份的账单信息是:%s'%resp.content)
if  'balance'  not  in  resp.content:                 ####可能出现没查到
if times<6:
logger.info('第%s次获取当前月份的账单信息是:%s' % (times,resp.content))
return self.get_current_month_bill(times+1)
else:
logger.warning('超过5次没有获取到获取当前月份的账单信息')
dictx = json.loads(resp.content)
mydict={
'acctBalance':dictx['resource'].get('acctBalance','--'),
'creditBalance':dictx['resource'].get('creditBalance','--'),
'totalFlow':dictx['resource'].get('totalFlow','--'),
'remainFlow':dictx['resource'].get('remainFlow','--'),
'totalVoice': dictx['resource'].get('totalVoice', '--'),
'remainVoice': dictx['resource'].get('remainVoice', '--'),
}
listx, totalFee=self.get_current_month_consumption()
mydict.update({
'billdata':'current_month',
'feeList':listx,
'totalFee':totalFee,
})
self.bill.append(mydict)

@deco
def get_current_month_consumption(self):
'''获取当前月份的消费统计'''  # http://iservice.10010.com/e4/index_server.html resp = self.post('http://iservice.10010.com/e3/static/query/consumptionAnalysis?_=1504763190181&accessURL=http://iservice.10010.com/e4/index_server.html')
logger.info('获取当前月份消费统计信息是:%s' % resp.content)
dictx = json.loads(resp.content)
listx=[]
for i in dictx['maplist']:
listx.append({'name':i['name'],'value':i['y']})
totalFee=dictx.get('totalfee','--')
return listx,totalFee

@deco
def get_all_history_bill(self):
'''获取历史月份的账单'''     ###http://iservice.10010.com/e4/skip.html?menuCode=000100020001&menuCode=000100020001
history_bill=[]

def get_histoty_month(m):
yearx, monthx, day_begin, day_end, monthRange, day_begin2, day_end2=self.get_year_month(m)
datax={
'querytype': '0001',
'querycode': '0001',
'billdate': '%d%02d'%(yearx,monthx),
'flag': '1',
}

resp = self.post('http://iservice.10010.com/e3/static/query/queryHistoryBill?_=1504093213145&accessURL=http://iservice.10010.com/e4/skip.html',data=datax)
logger.info('获取%s年%s月的账单信息是:%s\n'%(yearx,monthx,resp.content))
dictx=json.loads(resp.content)
###需要进行判断,2g用户和4g用户的返回数据不一样,也可能是后付费的原因
listx=[]
if 'historyResultList' in dictx:
for i  in  dictx['historyResultList']:
listx.append({'name':i['name'],'value': i['value']})
mydict={'totalFee':dictx['nowFee'],
'feeList':listx,
'billdate': '%d%02d' % (yearx, monthx),}
elif 'result' in dictx:
for i in dictx['result']['billinfo']:
listx.append({'name':i['integrateitem'] ,'value':i['fee']})
mydict={'totalFee': dictx['result']['allfee'],
'feeList': listx,
'billdate': '%d%02d' % (yearx, monthx),}
elif '"service":"fail"' in resp.content:
logger.warning('%s号码%s年%s月的账单信息获取返回的状态是fail'%(self.phone,yearx,monthx))
mydict = {'totalFee': '--',
'feeList': '--',
'billdate': '%d%02d' % (yearx, monthx), }
else:
logger.warning('%s号码%s年%s月的账单信息是其他类型的账单'%(self.phone,yearx,monthx))
mydict = {'totalFee': '--',
'feeList': '--',
'billdate': '%d%02d' % (yearx, monthx), }
self.bill.append(mydict)

[self.group.add(gevent.spawn(get_histoty_month,m)) for m in range(1,6)]

@deco
def get_all_business_record(self):
'''获取业务办理记录'''
self.business_record=[]

def get_business_month(m,times=1):
yearx, monthx, day_begin, day_end, monthRange, day_begin2, day_end2 = self.get_year_month(m)
datax={
'queryContent': 'acceptedRecord',
'beginDate': '%d-%02d-01'%(yearx,monthx),
'endDate': '%d-%02d-%d'%(yearx,monthx,monthRange),
}
resp = self.post('http://iservice.10010.com/e3/static/transact/handleRecord/?_=1504094856266&accessURL=http://iservice.10010.com/e4/transact/basic/handle_record_iframe.html?menuCode=000300070001&menuid=000300070001',data=datax)
logger.info('获取%s年%s月的业务办理记录是:%s'%(yearx,monthx,resp.content))

if '神马?你要帮它交费?' in resp.content:  ##网站有时候抽风了
if times<6:
logger.info('第%s次获取%s年%s月的业务办理记录是:%s' % (times,yearx, monthx, resp.content))
return get_business_month(i,times+1)
else:
logger.info('超过5次获取%s年%s月的业务办理记录是:%s' % ( yearx, monthx, resp.content))
return

dictx=json.loads(resp.content)

if 'acceptedRecordResult' in dictx:
for i in dictx['acceptedRecordResult']['record']:
mydict={
'acptsitename':i['acptsitename'],
'itemname':i['itemname'],
'optrdate':i['optrdate'],
}
self.business_record.append(mydict)

if '无受理日志记录' in resp.content:
pass

for m in range(6):
self.group.add(gevent.spawn(get_business_month,m))

@deco
def get_footprint(self):
resp = self.post('https://uac.10010.com/cust/myfootmark/myfootmarkQuery')
logger.info('获取足迹记录是:%s' % ( resp.content))
self.footprint=[]
dictx=json.loads(resp.content)
if dictx['result_code']=='0000':
for i in dictx["markList"]:
footTime=i['footTime']
footDescribe=i['footDescribe']
self.footprint.append({'fooTtime':footTime,'footDescribe':footDescribe  })
else:
logger.waning('获取足迹记录不成功:%s' % (resp.content))

return self.footprint

@deco
def get_all_sms(self):
self.sms=[]
def get_sms_month(m,times=1):
yearx, monthx, day_begin, day_end, monthRange, day_begin2, day_end2 = self.get_year_month(m)
if m==0:
day_end=time.strftime('%Y%m%d')
datax={
'pageNo': 1,
'pageSize': 10000,
'begindate': day_begin,
'enddate': day_end,
}

headerx={'Accept':'application/json, text/javascript, */*; q=0.01'}
resp=self.post('http://iservice.10010.com/e3/static/query/sms?_=1504098130310&accessURL=http://iservice.10010.com/e4/query/calls/call_sms-iframe.html?menuCode=000100030002&menuid=000100030002',data=datax,headers=headerx)
logger.info('第%s次获取%s号码第%s年%s月的短信记录是%s'%(times,self.phone,yearx,monthx,resp.content))
dictx=json.loads(resp.content)
if '尊敬的客户,由于同一时间使用该业务人数较多,暂时无法为您提供服务,为您带来的不便敬请谅解' in resp.content:  ##小概率出现这个
if times<5:
return get_sms_month(i,times+1)

elif '详单查询最大支持近6个月的查询' in resp.content:
pass
elif 'mmsCount' in dictx:
self.sms.append(dictx)

[self.group.add(gevent.spawn(get_sms_month,m)) for m in range(6)]

@deco
def get_all_flow_records(self):
'''获取流量使用记录,在哪里上的网'''   ###http://iservice.10010.com/e4/query/basic/call_flow_iframe1.html?menuCode=000100030004
self.flow_records=[]
def get_flow_month(m,times=1):
yearx, monthx, day_begin, day_end, monthRange, day_begin2, day_end2 = self.get_year_month(m)
datax = {
'pageNo': '1',
'pageSize': 10000,
'beginDate': day_begin2,
'endDate': day_end2,
}
datax = {
'pageNo': '1',
'pageSize': 1000,
'beginDate': '2017-08-01',
'endDate': '2017-08-01',
}
url='http://iservice.10010.com/e3/static/query/callFlow?_=1504506035718&accessURL=http://iservice.10010.com/e4/query/basic/call_flow_iframe1.html?menuCode=000100030004'
resp=self.post(url,data=datax)

if '神马?你要帮它交费?' in resp.content:
logger.warning('第%s次获取%s号码第%s年%s月的l流量记录返回到神马页面了' % (times, self.phone, yearx, monthx))
dictx=json.loads(resp.content)

if "errormessage" in dictx :
logger.warning('第%s次获取%s号码第%s年%s月的l流量记录异常,返回是:%s' % (times, self.phone, yearx, monthx, resp.content))
else:
logger.info('第%s次获取%s号码第%s年%s月的l流量记录是%s' % (times, self.phone, yearx, monthx, resp.content))
self.flow_records.append(dictx)

[self.group.add(gevent.spawn(get_flow_month, m)) for m in range(6)]

@deco
def get_all_internet_records(self):
''' 获取手机上网记录,不获取wifi上网记录 '''    ##http://iservice.10010.com/e4/query/basic/call_phont_record_new.html?menuId=000100030009&menuCode=000100030009
self.internet_records=[]
def get_internet_month(m):
yearx, monthx, day_begin, day_end, monthRange, day_begin2, day_end2 = self.get_year_month(m)
datax={'pageNo':1,
'pageSize':10000,
'beginDate':day_begin2,
'endDate':day_end2,
}
resp = self.post('http://iservice.10010.com/e3/static/query/callNetPlayRecord?_=1504144646940&accessURL=http://iservice.10010.com/e4/query/basic/call_phont_record-iframe.html&menuid=000100030009',data=datax)
logger.info('获取%s号码的%s年%s月上网记录是:%s'%(self.phone,yearx,monthx,resp.content))
dictx=json.loads(resp.content)
if "errorMessage" in dictx:
if '"respDesc":"尊敬的客户,出了一点点问题,请您稍候再试或立即反馈我们处理,给您带来不便敬请谅解' in resp.content:                ###有些月份的上网记录可能会返回这个json
self.internet_records.append(dictx)
pass
else:
self.internet_records.append(dictx)
[self.group.add(gevent.spawn(get_internet_month,m)) for m in range(4)]

@deco
def get_all_payment_records(self):
'''获取交费记录'''   ##http://iservice.10010.com/e4/query/calls/paid_record-iframe.html?menuCode=000100010003
self.payment_records=[]
def get_payment_month(m,times=1):
yearx, monthx, day_begin, day_end, monthRange, day_begin2, day_end2 = self.get_year_month(m)
datax={'pageNo':1,
'pageSize':10000,
'beginDate':day_begin,
'endDate':day_end,
}
resp=self.post('http://iservice.10010.com/e3/static/query/paymentRecord?_=1504151974387&accessURL=http://iservice.10010.com/e4/query/calls/paid_record-iframe.html',data=datax)

dictx=json.loads(resp.content)

if "errorMessage" in dictx:
if '业..务..连..接..超....时....过一会再来吧' in resp.content:
logger.warning('获取%s号码%s年%s月的交费记录业..务..连..接..超....时,返回的内容是:%s' % (self.phone, yearx, monthx, resp.content))
if times<6:
return get_payment_month(m,times+1)
else :
logger.warning('获取%s号码%s年%s月的交费记录返回结果异常,返回的内容是:%s' % (self.phone, yearx, monthx, resp.content))
else:
logger.info('获取%s号码%s年%s月的交费记录ok,返回的内容是:%s' % (self.phone, yearx, monthx, resp.content))
for d in dictx['pageMap']['result']:
self.payment_records.append(d)
[self.group.add(gevent.spawn(get_payment_month, m)) for m in range(6)]

def dict2chinese(dictx):
return json.dumps(dictx, ensure_ascii=False)

def main():
cu = ChinaUnicom(132xxxxxxx,3xxxxx)
if cu.login()[0] == 0:
cu.get_e3()
cu.group.spawn(cu.get_caller_location)
cu.group.spawn(cu.get_infomgrInit)
cu.group.spawn(cu.get_phone_info)
cu.group.spawn(cu.get_contract_period_info)

cu.group.spawn(cu.get_current_month_bill)
cu.get_all_history_bill()

cu.get_all_business_record()

cu.group.spawn(cu.get_footprint)

cu.get_all_call_detail()

cu.get_all_sms()

cu.get_all_flow_records()

cu.get_all_internet_records()
cu.get_all_payment_records()

cu.group.join()
print '全部交费记录是:   ', json.dumps(cu.payment_records, ensure_ascii=False)
print '全部流量记录是:   ', json.dumps(cu.flow_records, ensure_ascii=False)
print '全部上网记录是:   ', json.dumps(cu.internet_records, ensure_ascii=False)
print '全部短信记录是:   ', json.dumps(cu.sms, ensure_ascii=False)
print '全部通话记录是:   ', json.dumps(cu.call_details, ensure_ascii=False)
print '足迹记录是:   ', json.dumps(cu.footprint, ensure_ascii=False)
print '业务办理记录是:   ', json.dumps(cu.business_record, ensure_ascii=False)
print '个人信息是:   ', json.dumps(cu.info, ensure_ascii=False)
print '账单信息是:   ', dict2chinese(cu.bill)

else:
return 'login fail'

if __name__=="__main__":

main()


附上引入的文件一:

Logger.py

#coding=utf-8
import logging,time,os
from logging.handlers import TimedRotatingFileHandler,RotatingFileHandler

format_dict = {
1 : logging.Formatter('%(asctime)s - %(name)s - %(lineno)d - %(levelname)s -  %(message)s\n',"%Y-%m-%d %H:%M:%S"),
2 : logging.Formatter('%(asctime)s - %(name)s - %(lineno)d - %(levelname)s -  %(message)s\n',"%Y-%m-%d %H:%M:%S"),
3 : logging.Formatter('%(asctime)s - %(name)s - %(lineno)d - %(levelname)s -  %(message)s\n',"%Y-%m-%d %H:%M:%S"),
4 : logging.Formatter('%(asctime)s - %(name)s - %(lineno)d - %(levelname)s -  %(message)s\n',"%Y-%m-%d %H:%M:%S"),
5 : logging.Formatter('%(asctime)s - %(name)s - %(lineno)d - %(levelname)s -  %(message)s\n',"%Y-%m-%d %H:%M:%S"),
}

class Logger():

def __init__(self, logname, loglevel, logger):
if not os.path.exists('logs'):
os.mkdir('logs')
self.logname=logname

self.logger = logging.getLogger(logger)
if  loglevel==1:
lv=logging.DEBUG
elif loglevel == 2:
lv = logging.INFO
elif loglevel == 3:
lv = logging.WARNING
elif loglevel == 4:
lv = logging.ERROR
elif loglevel == 5:
lv = logging.CRITICAL

self.logger.setLevel(lv)

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
self.formatter = format_dict[int(loglevel)]
ch.setFormatter(self.formatter)
self.logger.addHandler(ch)

'''
filename='logs\\'+self.logname
tmfh=TimedRotatingFileHandler(filename,"M",10,0)  ##按时间切割
self.formatter = format_dict[int(loglevel)]
tmfh.setFormatter(self.formatter)
self.logger.addHandler(tmfh)
self.logger.setLevel(logging.DEBUG)
'''
filename = 'logs/' + self.logname
tmfh = RotatingFileHandler(filename, maxBytes=10*1024*1024,backupCount=100)  ##按大小切割
ch.setLevel(logging.DEBUG)
self.formatter = format_dict[int(loglevel)]
tmfh.setFormatter(self.formatter)
self.logger.addHandler(tmfh)

def getlog(self):
return self.logger


附上引入的文件二

myrequests.py文件,继承这个类,这样在ChinaUnicom中的self.post就是session类的post,每个请求都携带了登录和请求e3 checklogin接口的cookie。

#coding=utf8
import requests
import logging
logger=logging.getLogger('myrequests')

MAX_RETYIES=5

class Myrequests(object):

def __init__(self):
self.session=requests.session()

def post(self,url,data=None,**kwargs):
for i  in range(MAX_RETYIES):
try:
resp=self.session.post(url,data=data,**kwargs)
if self.check_content(resp.content):
logger.warning('对%s再请求post一次%s'%(url,data))
elif '尊敬的客户您好,系统检测您的访问过于频繁,为保障您的账户安全请明天再试'  in resp.content:
return resp
else:
return resp
except Exception,e:
logger.error('网络连接出错:%s'%str(e))

def get(self,url,**kwargs):
for i in range(MAX_RETYIES):
try:
resp = self.session.get(url,**kwargs)
if self.check_content(resp.content):
logger.warning('对%s再请求get一次' % (url))
else:
return resp
except Exception, e:
logger.error('网络连接出错:%s'%str(e))

@staticmethod
def check_content(content):
if '业..务..连..接..超....时....过一会再来吧' in content or '尊敬的客户,出了一点点问题,请您稍候再试或立即反馈我们处理' in content \
or '神马?你要帮它交费' in  content:
return 1
else:
return 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: