Как выполнить скрап-логин с помощью javascript submit - PullRequest
2 голосов
/ 29 апреля 2020

Мне нужно выполнить скрапбуляцию на веб-сайте, где выполняется xhr вход.

<form name="login-form"   id="login-form" >
                  <div class="form-group">
                     <label for="email">Email address</label>
                     <input type="email" class="form-control" name="email" id="email" placeholder="Enter email">
                  </div>
                  <div class="form-group">
                     <label for="password">Password</label>
                     <input type="password" class="form-control" name="password" id="password" placeholder="Password">
                  </div>
                  <button type="submit" id="login" class="btn btn-primary">Login</button>
               </form>

JS

<script>
            $(function()
            {
                $('#login').click(function(e){

                    let self = $(this);

                    e.preventDefault(); // prevent default submit behavior

                    $(this).prop('disabled',true); 

                    var data = $('#login-form').serialize(); // get form data
                    console.log(data)

                     // sending ajax reqeust to login.php file, it will proccess login reqeust and give response.
                    $.ajax({
                        url: './login.php',
                        type: "POST",
                        data: data,
                    }).done(function(res) {
                        res = JSON.parse(res);
                        if (res['status']) // if login successful redirect user to secure.php page.
                        {
                            location.href = "secure.php"; // redirect user to secure.php location/page.
                        } else {

                            var errorMessage = '';
                            // if there is any errors convert array of errors into html string, 
                            //here we are wrapping errors into a paragraph tag.
                            console.log(res.msg);
                            $.each(res['msg'],function(index,message) {
                                errorMessage += '<p>' + message+ '</p>';
                            });
                            // place the errors inside the div#error-msg.
                            $("#error-msg").html(errorMessage);
                            $("#error-msg").show(); // show it on the browser, default state, hide
                            // remove disable attribute to the login button, 
                            //to prevent multiple form submisstions 
                            //we have added this attributon on login from submit
                            self.prop('disabled',false); 
                        }
                    }).fail(function() {
                        alert("error");
                    }).always(function(){
                        self.prop('disabled',false); 
                    });
                });
            });
        </script>

Вы можете обратиться к скрап-коду в Как для выполнения запроса XHR формы scrapy с использованием Scrapy Python

Я хотел бы знать, какие изменения необходимо добавить сюда

Я сомневаюсь, что здесь чего-то не хватает

 def login(self, response):
        print ("\n Login is here! \n")
        form_data=self.fetch_form_data(response) 
        # form_data=ast.literal_eval(json.dumps(self.fetch_form_data(response)))        
        return FormRequest.from_response(response,
        formdata=form_data,
        callback=self.check_login_response)
...