爬虫之普通的模拟登录

准备知识

post与get有什么区别:html

  1. 根据HTTP规范,GET通常用于获取/查询资源信息,应该是安全的和幂等。而POST通常用于更新资源信息
  2. get是在url中传递数据,数据放在请求头中。 post是在请求体中传递数据
  3. get传送的数据量较小,只能在请求头上发送数据。post传送的数据量较大,通常被默认为不受限制。
  4. get安全性很是低,post安全性较高。可是执行效率却比Post方法好。 建议: 一、get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式; 二、在作数据查询时,建议用Get方式;而在作数据添加、修改或删除时,建议用Post方式;

概述

在平常爬虫中,有些网站须要登陆才能获取网站信息,这时咱们须要写一个模拟登录,才能去获取要爬取的页面,而后去分析提取。安全

模拟登录抽屉网

1.咱们首先打开抽屉网站,选择登录(没注册的先注册),咱们按要求输入帐号密码,这时咱们故意输错密码,打开f12开发者工具,点击network选项,而后点击登录,截图以下:

咱们经过请求头信息发现,里面有一个form表单,提交了3项数据,咱们也能够注意到它返回的response,
这在页面上已经反馈给咱们了

模拟登录

前面分析了数据是如何去提交的,接下来咱们就开始模拟了,咱们写一个post请求:bash

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’,更新最后代码以下:post

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"}}}
复制代码

说明成功了,这时咱们的模拟登录就完成了ui