会话对象让你能够跨请求保持某些参数。它也会在同一个 Session 实例发出的所有请求之间保持 cookie

#!usr/bin/env python
#-*- coding:utf-8 -*-
"""
@author:Administrator
@file: requests_session_use.py
@time: 2018/10/14
"""

import requests

def login():

    data = {"email": "18791076614",
            'icode': '',
            'origURL': 'http://www.renren.com/home',
            'domain': 'renren.com',
            'key_id': '1',
            'captcha_type': 'web_login',
            'password': '16574db0fa740d05cec88c505f09d59f67081723c18cad5459e60f7a0e240b4e',
            'rkey': '2565f530e36a171c975a82de7b6c46dc',
            'f': 'http%3A%2F%2Fzhibo.renren.com%2Ftop'
            }
    headers = {"Content-Type": "application/x-www-form-urlencoded",
               "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
               "Referer": "http://www.renren.com/"}

    url = "http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2018961914878"

    #创建session实例对象
    s = requests.session()
    #通过session实例发送登录请求
    r = s.post(url=url, data=data,headers=headers)
    return s

def get_info():
    """获取个人主页,"""
    url = "http://www.renren.com/968343753/profile"
    #通过session实例对象,将会话保持
    r = login().get(url=url)
    print(r.text)

get_info()

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐