Learning Scrapy notes (5)-Scrapy logon website and scrapy logon website

Source: Internet
Author: User
Tags send cookies

Learning Scrapy notes (5)-Scrapy logon website and scrapy logon website

Abstract: This article introduces the process of using Scrapy to log on to a simple website, which does not involve Verification Code cracking.

Simple Logon

Most of the time, you will find that the website you want to crawl data has a logon mechanism. In most cases, you are required to enter the correct user name and password. Now we can simulate this situation. Open the webpage http: // 127.0.0.1: 9312/dynamic in the browser, open the debugger, and click the Elements tab to view the source code of the logon form.

# Start with a login requestdef start_requests (self): return [FormRequest ("http: // webpage: 9312/dynamic/login", formdata = {"user": "user ", "pass": "pass"} # The key name in the formdata dictionary is the name value in the input tag marked with a red line in the first graph)]

In this simple way, Scrapy will automatically process cookies for us. As long as we log on successfully, it will automatically send cookies like a browser.

Run the spider: scrapy crawl login

# This function returns a Request, in which the callback function has been specified as parse_welcome, so the response will be transmitted to the parse_welcome function def start_requests (self ): return [Request ("http: // web: 9312/dynamic/nonce", callback = self. parse_welcome)]

  • Add a parse_welcome function as follows:
# Receive the response containing all the data in the form (the username and password are empty at this time, but the value of the hidden field nonce is obtained), fill the username and password, and then log on to def parse_welcome (self, response): return FormRequest. from_response (response, formdata = {"user": "user", "pass": "pass "})

Run the spider: scrapy crawl noncelogin

Two requests (one GET and one POST) are sent during the login process)

 

It is worth noting that the form_response Function

(Http://doc.scrapy.org/en/1.0/topics/request-response.html#topics-request-response-ref-request-userlogin), this function is worth a careful study, if there are multiple forms on the login page, then you need to use parameters to specify the form to fill the user name and password, at the same time, by default, the function simulates the click behavior of the logon button. If the website uses javascript to control logon, the default function clicking behavior may fail, disable simulated clicks by setting dont_click to True.

The above only uses two steps to log on, but in the face of more complex login methods, you need to organize more steps.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.