您的位置:首页 > 编程语言 > Python开发

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

2017-07-22 17:57 417 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: