Python crawler tutorial -31-creating a scrapy Crawler Framework Project

Source: Internet
Author: User

This article describes the steps to create a Scrapy crawler framework project in a Anaconda environment, and introduces a more detailed

Python crawler tutorial -31-creating a scrapy Crawler Framework Project
    • First of all, this article is in the Anaconda environment, so if not installed Anaconda please first download the installation
    • anaconda:https://www.anaconda.com/download/
Creation of the Scrapy Crawler Framework Project
    • 0. Open "cmd"
    • 1. Enter the Anaconda environment you want to use
      • 1. The environment name can be found under "Project:" under "Settings" in "Pycharm"
      • 2. Use the command: Activate the environment name , for example:

        Activate learn

      • 3. Enter the directory where you want to store the Scrapy item "Note"
      • 4. New project: scrapy startproject XXX project name , for example:

        Scrapy Startproject New_project

    • 5. Operation:
    • 6. Open the directory in File Explorer and you will find that several files have been generated
    • 7. Use Pycharm to open the directory where the project is located.

Here we create the project and analyze the role of the automatically generated files

Development of Scrapy Crawler Framework Project
  • 0. Open the project using Pycharm:
  • The general process of project development:
    • 1. Identify the target/product to be crawled: write item.py
    • 2. Create a Python file crawler in spider catalog Download:
      • Address spider/xxspider.py is responsible for decomposition, extraction of downloaded data
    • 3. Storage content: pipelines.py
  • pipeline.py file
    • Corresponding pipelines file
    • After the crawler extracts the data into the item, the data stored in the item needs to be further processed, such as cleaning, pest, storage, etc.
    • Pipeline need to handle Process_item function
    • Process_item
      • The spider's extracted item is passed in as a parameter, and the spider is passed in.
      • This method must implement the
      • Must return an item object, the discarded item will not be pipeline
  • _ init _: Constructor
    • Perform some necessary parameter initialization.
  • Open_spider (spider):
    • The Spider object is called when it is turned on.
  • Close_spider (spider):
    • Called when the Spider object is closed
  • Spider Directory
    • corresponding to the file under the folder spider
    • _ init _: Initialize the crawler name, start _urls list
    • Start_requests: Generate requests object to scrapy download and return response
    • Parse: According to the returned response parse out the corresponding Item,item automatically enter pipeline: if necessary, the resolution Url,url automatically handed to the requests module, has been circulating
    • Start_requests: This method can be called once, read the start _urls content and start the loop process
    • Name: Set crawler name
    • Start_urls: Set the URL to start the first crawl
    • Allow_domains:spider List of domain names allowed to crawl
    • Start_request (self): called only once
    • Parse: Detection code
    • LOG: Logging
  • Next article link: Python crawler tutorial -32-scrapy crawler Framework Project settings.py Introduction
  • Bye
    • This note does not allow any person or organization to reprint

Python crawler tutorial -31-creating a scrapy Crawler Framework Project

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.