python 利用requests模块会话session模拟登录URL网址下载数据

来源:互联网 发布:手机网络时间同步软件 编辑:程序博客网 时间:2024/06/05 15:51
PASSWORD = '[{"userId":"**","password":"**"}]'


def getData():  
    s = requests.Session()#创建一个session对象
    s.keep_alive = False  #保持一个长连接
    s.headers.update({'Connection': 'keep-alive'}) 
    for i in range(0,5):
        try:
            s.post('URL',data=PASSWORD)#该URL为登录页面的URL
            s.headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
            break
        except requests.exceptions.ConnectionError:
            time.sleep(1)
            continue   


    s.headers.update({'Content-Type': 'application/x-www-form-urlencoded'}) 
      
    payload = ''   
    for count in range(0,5):
        print count
        try:
            rsp = s.post('URL',data=payload)#该URL为需要获取数据页面的F12请求正文内容
            #print "rsp",rsp
            break
        except requests.exceptions.ConnectionError:
            time.sleep(1)
            continue 
    #print "rsp",rsp
    
    exportfile ="D:/data.xls"


    with open(exportfile,'wb') as out_file:
        for chunk in rsp.iter_content():#将文本流保存到文件
            out_file.write(chunk)
    #若数据无效,则删除文件
    f = open(exportfile, 'rb')
    lines = f.read()
    isinvalid=re.search("javascript",lines)
    if (len(lines)==0) or (isinvalid is not None):
        print "this is empty data,system will delete it"
        f.close()
        os.remove(exportfile)
    else:
        print "this is valid data"
        time.sleep(40)
原创粉丝点击