Python3 使用cookiejar管理cookie的方法
这次我们使用cookiejar来完成一个登录学校model平台,并查看登陆后的其他页面的任务
fromurllibimportrequest
fromurllibimportparse
fromhttpimportcookiejar
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;MSIE5.5;WindowsNT)'
}
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)
以上这篇Python3使用cookiejar管理cookie的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。