zoukankan      html  css  js  c++  java
  • scrapy formRequest 表单提交

     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方法可以帮助我们寻找到表单提交的地址,以及预填充认证令牌。

  • 相关阅读:
    Medium | LeetCode 148. 排序链表 | 归并排序(递归)
    Hard | LeetCode 4. 寻找两个正序数组的中位数 | 二分法
    Medium | LeetCode 341. 扁平化嵌套列表迭代器 | 递归 | 栈
    Hard | LeetCode 312. 戳气球 | 递归+记忆化数组 | 动态规划
    如何删除万能输入法
    javaweb 怎么获取路径
    Controller 返回 json那些小事
    螺旋矩阵
    javaweb怎么使用html
    tomcat中文乱码
  • 原文地址:https://www.cnblogs.com/tangkaishou/p/10268067.html
Copyright © 2011-2022 走看看