cookie模擬登錄


cookie模擬登錄

1.適用網站幾場景

  抓取需要登錄才能訪問的頁面

2.cookie和session機制

# http協議為無連接協議
cookie: 存放在客戶端瀏覽器
session: 存放在Web服務器

人人網登錄案例

方法一.登錄網站手動抓取Cookie

1.先登錄成功1次,獲取到攜帶登錄信息的Cookie
    登錄成功-個人主頁-F12抓包-刷新個人主頁-找到主頁的包(profile)
2.攜帶着cookie發請求
    Cookie
    User-Agent
import requests

class RenRenLogin(object):
    def __init__(self):
        # url為需要登錄才能正常訪問的地址
        self.url = 'http://www.renren.com/967469305/profile'
        # headers中的cookie為登錄成功后抓取到的cookie
        self.headers = {
            # 此處注意cookie,要自己抓取
            "Cookie": "xxx",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
        }

    # 獲取個人主頁響應
    def get_html(self):
        html = requests.get(url=self.url,headers=self.headers,verify=False).text
        print(html)
        self.parse_html(html)

    # 可獲取並解析整個人人網內需要登錄才能訪問的地址
    def parse_html(self,html):
        pass

if __name__ == '__main__':
    spider = RenRenLogin()
    spider.get_html()
代碼實現

 

 方法二.requests模塊處理Cookie

原理思路及實現

# 1. 思路
requests模塊提供了session類,來實現客戶端和服務端的會話保持

# 2. 原理
1、實例化session對象
   session = requests.session()
2、讓session對象發送get或者post請求
   res = session.post(url=url,data=data,headers=headers)
   res = session.get(url=url,headers=headers)

# 3. 思路梳理
瀏覽器原理: 訪問需要登錄的頁面會帶着之前登錄過的cookie
程序原理: 同樣帶着之前登錄的cookie去訪問 - 由session對象完成
1、實例化session對象
2、登錄網站: session對象發送請求,登錄對應網站,把cookie保存在session對象中
3、訪問頁面: session對象請求需要登錄才能訪問的頁面,session能夠自動攜帶之前的這個cookie,進行請求

 

具體步驟

1、尋找Form表單提交地址 - 尋找登錄時POST的地址
   查看網頁源碼,查看form表單,找action對應的地址: http://www.renren.com/PLogin.do

2、發送用戶名和密碼信息到POST的地址
   * 用戶名和密碼信息以什么方式發送? -- 字典
     鍵 :<input>標簽中name的值(email,password)
     值 :真實的用戶名和密碼
     post_data = {'email':'','password':''}
session = requests.session()
session.post(url=url,data=data)

 

程序實現

整體思路
1、先POST: 把用戶名和密碼信息POST到某個地址中
2、再GET:  正常請求去獲取頁面信息
import requests

class RenrenLogin(object):
  def __init__(self):
    # 定義常用變量
    self.post_url = 'http://www.renren.com/PLogin.do'
    self.get_url = 'http://www.renren.com/967469305/profile'
    self.post_data = {
      'email' : 'xxx',
      'password' : 'xxx'
    }
    self.headers = {
      'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
      'Referer' : 'http://www.renren.com/SysHome.do'
    }

    # 實例化session會話保持對象
    self.session = requests.session()

  # 先post 再get
  def post_get_data(self):
    # 先POST,把用戶名和密碼信息POST到一個地址
    self.session.post(url=self.post_url,data=self.post_data,headers=self.headers)

    # 再get個人主頁
    html = self.session.get(url=self.get_url,headers=self.headers).text
    print(html)

if __name__ == '__main__':
    spider = RenrenLogin()
    spider.post_get_data()
代碼實現

 

方法三

原理

1、把抓取到的cookie處理為字典
cookies_dict = {}
cookies = 'xxx'
for kv in cookise.split('; ')
    cookies_dict[kv.split('=')[0]]=kv.split('=')[1]
2、使用requests.get()中的參數:cookies

 

import requests

class RenRenLogin(object):
    def __init__(self):
        # url為需要登錄才能正常訪問的地址
        self.url = 'http://www.renren.com/967469305/profile'
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
        }

    # 將字符串cookie轉為字典格式
    def cookie_to_dict(self,cookie_str):
        cookie_dic = {}
        for i in cookie_str.split('; '):
            cookie_dic[i.split('=')[0]] = i.split('=')[1]
        return cookie_dic

    # 獲取個人主頁響應
    def get_html(self):
        cookie_str = 'xxx'
        cookie_dict = self.cookie_to_dict(cookie_str)
        html = requests.get(url=self.url,headers=self.headers,verify=False,cookies=cookie_dict).text
        print(html)
        self.parse_html(html)

    # 可獲取並解析整個人人網內需要登錄才能訪問的地址
    def parse_html(self,html):
        pass

if __name__ == '__main__':
    spider = RenRenLogin()
    spider.get_html()
代碼實現

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM