urllib库利用cookie实现模拟登陆慕课网

思路

1.首先在网页中使用帐户和密码名登陆慕课网php

2.其次再分析请求头,以下图所示,获取到请求URL,并提取出cookie信息,保存到本地python

3.最后在代码中构造请求头,使用urllib.request发送包含cookie信息的请求web

 

源码

# !/usr/bin/env python
# -*- coding:utf-8 -*-

"""
使用Cokie模拟登陆
"""

import urllib.request

url="http://www.imooc.com/u/2346025"
cookie="本身的Cookie字符串"
request_header={
    "Accept":"image/webp,image/apng,image/*,*/*;q=0.8",
    # Accept-Encoding:gzip, deflate, br
    "Accept-Language":"zh-CN,zh;q=0.9",
    "Connection":"keep-alive",
    "Cookie":cookie,
    # Host:hm.baidu.com",
    # "Referer":"http://www.imooc.com/u/2346025",
    "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
}

req=urllib.request.Request(url,headers=request_header)

resp=urllib.request.urlopen(req)

data=resp.read().decode('utf-8')

print(data)

  

 

运行结果

入下图,能够看到,已经能够获取到登陆后的一些信息了cookie

 

网址:http://blog.csdn.net/topleeyap/article/details/78841383网络

其它:url

Python爬虫之模拟登陆总结:http://blog.csdn.net/churximi/article/details/50917322.net

Python 模拟登陆知乎:http://blog.csdn.net/Marksinoberg/article/details/69569353code

Python 网络爬虫--简单的模拟登陆:http://blog.csdn.net/M_WBCG/article/details/70243372blog

相关文章
相关标签/搜索