爬蟲之普通的模擬登陸

小杰哥001發表於2018-08-24

準備知識

post與get有什麼區別:

  1. 根據HTTP規範,GET一般用於獲取/查詢資源資訊,應該是安全的和冪等。而POST一般用於更新資源資訊
  2. get是在url中傳遞資料,資料放在請求頭中。 post是在請求體中傳遞資料
  3. get傳送的資料量較小,只能在請求頭上傳送資料。post傳送的資料量較大,一般被預設為不受限制。
  4. get安全性非常低,post安全性較高。但是執行效率卻比Post方法好。 建議: 1、get方式的安全性較Post方式要差些,包含機密資訊的話,建議用Post資料提交方式; 2、在做資料查詢時,建議用Get方式;而在做資料新增、修改或刪除時,建議用Post方式;

概述

在日常爬蟲中,有些網站需要登入才能獲取網站資訊,這時我們需要寫一個模擬登陸,才能去獲取要爬取的頁面,然後去分析提取。

模擬登陸抽屜網

1.我們首先開啟抽屜網站,選擇登陸(沒註冊的先註冊),我們按要求輸入賬號密碼,這時我們故意輸錯密碼,開啟f12開發者工具,點選network選項,然後點選登陸,截圖如下:

爬蟲之普通的模擬登陸
我們通過請求頭資訊發現,裡面有一個form表單,提交了3項資料,我們也可以注意到它返回的response,
爬蟲之普通的模擬登陸
這在頁面上已經反饋給我們了

模擬登陸

前面分析了資料是如何去提交的,接下來我們就開始模擬了,我們寫一個post請求:

import requests

response = requests.post(
    url='https://dig.chouti.com/login',
    data = {
        'phone':'8613185007919',
        'password':'155560',
        'oneMonth':'1'
    }

)
print(response.text)
複製程式碼

這時我們來看結果:

<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>網站防火牆</title>
<style>
p {
	line-height:20px;
}
ul{ list-style-type:none;}
li{ list-style-type:none;}
</style>
</head>

<body style=" padding:0; margin:0; font:14px/1.5 Microsoft Yahei, 宋體,sans-serif; color:#555;">

 <div style="margin: 0 auto; width:1000px; padding-top:70px; overflow:hidden;">
  
  
  <div style="width:600px; float:left;">
    <div style=" height:40px; line-height:40px; color:#fff; font-size:16px; overflow:hidden; background:#6bb3f6; padding-left:20px;">網站防火牆 </div>
    <div style="border:1px dashed #cdcece; border-top:none; font-size:14px; background:#fff; color:#555; line-height:24px; height:220px; padding:20px 20px 0 20px; overflow-y:auto;background:#f3f7f9;">
      <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600; color:#fc4f03;">您的請求帶有不合法引數,已被攔截!請勿在惡意提交。</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">可能原因:您提交的內容包含危險的攻擊請求, 自動記錄 ip 相關資訊通知管理員</p>
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:1; text-indent:0px;">如何解決:</p>
<ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1)檢查提交內容;</li>
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2)普通網站訪客,請聯絡網站管理員;</li></ul>
    </div>
  </div>
</div>
</body></html>

複製程式碼

網站給我們反饋這個資訊,我們看到這個資訊應該想到,這是網站的反爬措施,這時我們應該在post請求裡面加上‘User—Agent’,更新最後程式碼如下:

import requests

response = requests.post(
    url='https://dig.chouti.com/login',
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    },
    data = {
        'phone':'8613185007919',
        'password':'155560',
        'oneMonth':'1'
    }

)
print(response.text)
複製程式碼

我們執行一下看看結果:

{"result":{"code":"9999", "message":"", "data":{"complateReg":"0","destJid":"cdu_53360741818"}}}
複製程式碼

說明成功了,這時我們的模擬登陸就完成了

相關文章