码迷,mamicode.com
首页 > 其他好文 > 详细

scrapy formRequest 表单提交

时间:2019-01-14 18:04:12      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:www.   请求   官方   code   表单提交   value   https   ima   模拟   

 scrapy.FormRequest 主要用于提交表单数据

先来看一下源码

技术分享图片

参数:

 formdata  (dict or iterable of tuples) – is a dictionary (or iterable of (key, value) tuples) containing HTML Form data which will be url-encoded and assigned to the body of the request.

从官方文档中可以看到默认是 post 请求

怎么用

官方例子:

FormRequest(url="http://www.example.com/post/action",
                    formdata={name: John Doe, age: 27},
                    callback=self.after_post

就是这么简单就发送了一个 post 表单请求, formdata 就是要提交的表单数据。 callback 是指定回调函数,该参数继承于 Request 

github登录例子:

class GithubSpider(scrapy.Spider):
    name = github
    allowed_domains = [github.com]
    start_urls = [https://github.com/login]

    def parse(self, response):
        authenticity_token = response.xpath("//input[@name=‘authenticity_token‘]/@value").extract_first()
        utf8 = response.xpath("//input[@name=‘utf8‘]/@value").extract_first()
        commit = response.xpath("//input[@name=‘commit‘]/@value").extract_first()
        post_data = dict(
            login="your_username",
            password="your_password",
            authenticity_token=authenticity_token,
            utf8=utf8,
            commit=commit
        )
        yield scrapy.FormRequest(
            "https://github.com/session",
            formdata=post_data,
            callback=self.after_login
        )

    def after_login(self,response):
        print(re.findall("your_username",response.body.decode()))

scrapy.FormRequest.from_response

作用:自动的从 response  中寻找form表单(表单action,表单name),并且可以预填充表单认证令牌等(例如Django框架的csrf_token)

定义说明:

技术分享图片

 技术分享图片

 

怎么用

官方例子:

通常网站通过 <input type="hidden"> 实现对某些表单字段(如数据或是登录界面中的认证令牌等)的预填充。 使用Scrapy抓取网页时,如果想要预填充或重写像用户名、用户密码这些表单字段,

可以使用 FormRequest.from_response() 方法实现。下面是使用这种方法的爬虫例子

import scrapy

class LoginSpider(scrapy.Spider):
    name = example.com
    start_urls = [http://www.example.com/users/login.php]

    def parse(self, response):
        return scrapy.FormRequest.from_response(
            response,
            formdata={username: john, password: secret},
            callback=self.after_login
        )

    def after_login(self, response):
        # check login succeed before going on
        if "authentication failed" in response.body:
            self.log("Login failed", level=scrapy.log.ERROR)
            return

        # continue scraping with authenticated session...

github登录例子

class Github2Spider(scrapy.Spider):
    name = github2
    allowed_domains = [github.com]
    start_urls = [https://github.com/login]

    def parse(self, response):
        yield scrapy.FormRequest.from_response(
            response, #自动的从response中寻找from表单
            formdata={"login":"your_username","password":"your_password"},
            callback = self.after_login
        )

    def after_login(self,response):
        print(re.findall("your_username",response.body.decode()))

 对比两次github的模拟登录例子来看,使用from_response方法可以帮助我们寻找到表单提交的地址,以及预填充认证令牌。

scrapy formRequest 表单提交

标签:www.   请求   官方   code   表单提交   value   https   ima   模拟   

原文地址:https://www.cnblogs.com/tangkaishou/p/10268067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!