这次我们使用cookiejar来完成一个登录学校model平台,并查看登陆后的其他页面的任务
from urllib import request
from urllib import parse
from http import cookiejar
if __name__ == '__main__':
cookie_jar = cookiejar.CookieJar()
handler = request.HTTPCookieProcessor(cookie_jar)
opener = request.build_opener(handler)
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)