Python3 使用cookiejar管理cookie

这次我们使用cookiejar来完成一个登录学校model平台,并查看登陆后的其他页面的任务

from urllib import request
from urllib import parse
from http import cookiejar

if __name__ == '__main__':
    # 创建cookie管理
    cookie_jar = cookiejar.CookieJar()
    handler = request.HTTPCookieProcessor(cookie_jar)
    opener = request.build_opener(handler)
    # 创建post访问request
    url = 'http://moodle.zwu.edu.cn/login/index.php'
    data = {
        'username': '填写学号',
        'password': '填写密码'
    }
    headers = {
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    }
    post_data = parse.urlencode(data).encode('utf-8')
    request = request.Request(url, post_data, headers)
    # 访问
    html = opener.open(request).read().decode('utf-8')
    print(html)

猜你喜欢

转载自blog.csdn.net/zjsxxzh/article/details/77914478
今日推荐