post与get有什么区别:html
在平常爬虫中,有些网站须要登陆才能获取网站信息,这时咱们须要写一个模拟登录,才能去获取要爬取的页面,而后去分析提取。安全
前面分析了数据是如何去提交的,接下来咱们就开始模拟了,咱们写一个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