《精通Scrapy网络爬虫》第10章 第10.1 登录的本质

10.1 登录实质

测试网站:http://example.webscraping.com/places/default/user/login
登录网站后按 F12 打开开发者工具。选择 Elements 点击 form 表单
在这里插入图片描述

10.1.1 分析:
例:
 <form action="#" enctype="application/x-www-form-urlencoded" method="post"> 
<form>的 method 属性决定了 HTTP 请求的方法 (例为 POST);
<form>的 action 属性决定了 HTTP 请求的 url (例为 # ,也就是当前页);
<form>的 enctype 属性决定了表单数据的编码类型(例为 x-www-form-urlencoded);

扩展阅读:post使用form-data和x-www-form-urlencoded的本质区别

<from>中的<input>元素决定了表单数据的内容
例: <input class="string" id="auth_user_email" name="email" type="text" value=""/>
用户填写用户名

<div style="display:none;">
   <input name="_next" type="hidden" value="/places/default/index"/>
   <input name="_formkey" type="hidden" value="70322c27-786e-4c62-b2d7-40a5dfdf903d"/>
   <input name="_formname" type="hidden" value="login"/>
  </div>
 <div style="display:none">XXX</div> 说明:
 在<div style="display:none">XXX</div>中的百XXX内容不显示度可以通过JS使改DIV的display属性变成block则这些知内容显示出来此效果用途很广,比如有的目录树,版比如论坛的回复查看内容等权等(百度)
 
 在<div style="display:none;">中包含了3个隐藏的 <input type="hidden">,它们的值在 value 属性中,虽然不需要用户填写,
 但提交表单数据中缺少它们的话可能会导致登录验证失败,这些隐藏的<input>有其他用途,比如:
 <input name="_next"> 用来告诉服务器,登录成功后跳转的地址。
 例:/places/default/index 为 http://example.webscraping.com/places/default/index
 <input name="_formkey">用来防止CSRT跨域攻击。
10.1.2 登录:

填入账号、密码登录,点击 Log In 在 NetWork 中查看文件 (login)不同文件不同
10.1
10.2
Form Data (提交表单数据)
email: zk_zhang1986@sina.com
password: zzk123
_next: /places/default/index
_formkey: 51ee44dd-d8ed-4963-a58c-6e607db9f298
_formname: login
实际post提交是数据
email=zk_zhang1986%40sina.com&
password=zzk123&
_next=%2Fplaces%2Fdefault%2Findex&
_formkey=51ee44dd-d8ed-4963-a58c-6e607db9f298&
_formname=login
(主要是分析 Form Data 数据以及构造)

10.3

登录成功后 Response Headers 中 Set_Cookies 字段就是网站服务器保存在客户端(浏览器)的 Cookie 信息,之后对该网站发送的其他 HTTP 请求
都会带上这个“身份证”(session信息),服务器程序通过这个“身份证”识别出发送请求的用户,从而决定响应怎样的页面。
另外响应状态码是303,代表页面重定向,浏览器会读取响应头部中的 Location 字段,依据其中描述的路径(本例为/places/default/index),再次发送一个GET请求。转跳后如下图

10.4

小结:

主要就是对表单的分析,以及表单数据的构造。


<div id="web2py_user_form">
<form action="#" enctype="application/x-www-form-urlencoded" method="post">
	<table>
		<tbody>
			<tr id="auth_user_email__row">
				<td class="w2p_fl"><label class="" for="auth_user_email" id="auth_user_email__label">电子邮件: </label></td>
				<td class="w2p_fw"><input class="string" id="auth_user_email" name="email" type="text" value=""></td>
				<td class="w2p_fc"></td>
			</tr>
			<tr id="auth_user_password__row">
				<td class="w2p_fl"><label class="" for="auth_user_password" id="auth_user_password__label">密码: </label></td>
				<td class="w2p_fw"><input class="password" id="auth_user_password" name="password" type="password" value=""></td>
				<td class="w2p_fc"></td>
			</tr>
			<tr id="auth_user_remember_me__row">
				<td class="w2p_fl"><label class="" for="auth_user_remember_me" id="auth_user_remember_me__label">记住我(30): </label></td>
				<td class="w2p_fw"><input class="boolean" id="auth_user_remember_me" name="remember_me" type="checkbox" value="on"></td>
				<td class="w2p_fc"></td>
			</tr>
			<tr id="submit_record__row">
				<td class="w2p_fl"></td>
				<td class="w2p_fw"><input type="submit" value="Log In" class="btn"><button class="btn w2p-form-button" onclick="window.location='/places/default/user/register?_next=%2Fplaces%2Fdefault%2Findex';return false">注册</button></td>
				<td class="w2p_fc"></td>
			</tr>
		</tbody>
	</table>
	<div style="display:none;">
		<input name="_next" type="hidden" value="/places/default/index">
		<input name="_formkey" type="hidden" value="9fcc7ce6-be55-4a29-81d1-f5cd72674dce">
		<input name="_formname" type="hidden" value="login">
	</div>
</form>
</div>

C:\Users\Administrator>scrapy shell http://example.webscraping.com/places/default/user/login
>>> sel = response.xpath('//div[@style]/input')
>>> sel
[<Selector xpath='//div[@style]/input' data='<input name="_next" type="hidden" v
alue='>, <Selector xpath='//div[@style]/input' data='<input name="_formkey" type
="hidden" val'>, <Selector xpath='//div[@style]/input' data='<input name="_formn
ame" type="hidden" va'>]
>>> fd = dict(zip(sel.xpath('./@name').extract(),sel.xpath('./@value').extract()
))
>>> fd
{'_next': '/places/default/index', '_formkey': 'e47b0c25-273d-4939-98d2-6d0d2c28
47c2', '_formname': 'login'}
>>> fd['email']='zk_zhang1986@sina.com'
>>> fd['password']='123'
>>> fd
{'_next': '/places/default/index', '_formkey': 'e47b0c25-273d-4939-98d2-6d0d2c28
47c2', '_formname': 'login', 'email': 'zk_zhang1986@sina.com', 'password': '1
23'}
>>> from scrapy.http import FormRequest
>>> request = FormRequest('http://example.webscraping.com/places/default/user/lo
gin',formdata=fd)
>>> fetch(request)
2020-02-28 15:49:18 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (
303) to <GET http://example.webscraping.com/places/default/index> from <POST htt
p://example.webscraping.com/places/default/user/login>
2020-02-28 15:49:19 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://exampl
e.webscraping.com/places/default/index> (referer: None)
>>> fetch('http://example.webscraping.com/places/default/user/profile')
2020-02-28 15:52:41 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://exampl
e.webscraping.com/places/default/user/profile> (referer: None)
>>> view(response)
True
>>>

------------------
# FormRequest  隐藏的<input>信息会自动填入
>>> fd = {'email':'zk_zhang1986@sina.com','password':'123'}
>>> fd
{'email': 'zk_zhang1986@sina.com', 'password': '123'}
>>> from scrapy.http import FormRequest
>>> request = FormRequest('http://example.webscraping.com/places/default/user/lo
gin',formdata=fd)
>>> fetch(request)
2020-02-28 16:26:25 [scrapy.core.engine] DEBUG: Crawled (200) <POST http://examp
le.webscraping.com/places/default/user/login> (referer: None)
>>> view(response)
True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值