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