cookie,http.cookiejar模块的应用

标题:cookie原理及格式详解演练:

# encoding=utf-8
# 大鹏董成鹏主页:http://www.renren.com/880151247/profile
# 人人网登陆 url:http://www.renren.com/SysHome.do
from  urllib import request
# 1.不使用 cookie 去请求大鹏的主页
dapeng_url = 'http://www.renren.com/880151247/profile'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                 'Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400',
    'cookie':'anonymid=k805vyka6msfgo; depovince=GW; _r01_=1; ick_login=c1c20473-21f7-40bf-aca4'
             '-bd6cafeb7231; taihe_bi_sdk_uid=494a7a5f47996d0663f2cbbe195e51b6; taihe_bi_sdk_session='
             'c404b9a4c48dd5eef5c25cbec4109b1e; loginfrom=null; jebe_key=2917afcc-bf62-4e2c-aa14'
             '-00647781466d%7C605e2cdef60691d604242042f182566a%7C1584707502073%7C1%7C1584707462270;'
             ' wp_fold=0; t=ea0b60b27ac015398681319cafc76f929;'
             ' societyguester=ea0b60b27ac015398681319cafc76f929; id=974051029; xnsid=80834ee;'
             ' jebecookies=8b1c7cf5-73db-4f12-bc2a-c31344e07dae|||||; ver=7.0'
}
req = request.Request(url=dapeng_url,headers=headers)
resp = request.urlopen(req)
with open ('renren.html','w',encoding='utf-8') as fp:
    # write 函数必须写入一个 str 的数据类型
    # resp.read()读出来的是一个 byte 数据类型
    # bytes -> decode -> str
    # str  -> encode -> bytes
    fp.write(resp.read().decode('utf-8'))

标题:http.cookiejar模块:

cookie,http.cookiejar模块的应用_第1张图片
图片来源:https://www.cnblogs.com/fu-yong/p/9032850.html

# encoding=utf-8
# 大鹏董成鹏主页:http://www.renren.com/880151247/profile
# 人人网登陆 url:http://www.renren.com/SysHome.do
from urllib import request
from urllib import parse
from http.cookiejar import CookieJar

handers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                 'Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400',
}
def get_opener():

    # 1.登录
    # 1.1 创建一个 cookiejar 对象
    cookiejar = CookieJar()
    # 1.2 使用 cookiejar 创建一个 HTTPCookieProcess对象
    handler = request.HTTPCookieProcessor(cookiejar)
    # 1.3 使用上一步骤创建的 handler 创建一个 opener
    opener = request.build_opener(handler)
    return opener

def login_renren(opener):


    # 1.4 使用 opener 发送登陆的请求 (人人网邮箱和密码)

    data = {
        'email':'[email protected]',
        'password':'pythonsprider'
    }
    login_url = 'http://www.renren.com/Plogin.do'
    req = request.Request(login_url,data=parse.urlencode(data).encode('utf-8')
                          ,headers=handers)
    opener.open(req)
def visit_profile(opener):
    # 2.访问个人主页
    dapeng_url = 'http://www.renren.com/880151247/profile'
    # 发送获取个人页面的时候,不要新建一个operner,因为之前的那个opener已经包含登陆所需要的cookie信息
    req = request.Request(dapeng_url,headers=handers)
    resp = opener.open(req)
    with open('renren2.html','w',encoding='utf-8') as fp:
        fp.write(resp.read().decode('utf-8'))


if '_name_ '== '_main_':
    opener = get_opener()
    login_renren(opener)
    visit_profile(opener)

你可能感兴趣的:(cookie,http.cookiejar模块的应用)