《精通scrapy网络爬虫》第10章

请看另外一篇文章《精通Scrapy网络爬虫》第10章 第10.1 登录的本质

<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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值