![]() |
VOOZH | about |
Scrapy is a web scraping library that is used to scrape, parse and collect web data. For all these functions we are having a pipelines.py file which is used to handle scraped data through various components (known as class) which are executed sequentially.
In this article, we will be learning through the methods defined for this pipeline's file and will show different examples of it.
Let's, first, create a scrapy project. For that make sure that Python and PIP are installed in the system. Then run the given commands below one by one to create a scrapy project similar to the one which we will be using in this article.
Step 1: Let's first create a virtual environment in a folder named GFGScrapy and activate that virtual environment there.
# To create a folder named GFGScrapy mkdir GFGScrapy cd GFGScrapy # making virtual env there. virtualenv cd scripts # activating it activate cd..
Hence, after running all these commands, we will get the output as shown:
Step 2: Now it's time to create a scrapy project. For that Make sure that scrapy is installed in the system or not. If not installed, install it using the given command below.
pip install scrapy
Now to create a scrapy project use the, given command below and also create a spider.
# projEct name is scrapytutorial scrapy startproject scrapytutorial cd scrapytutorial scrapy genspider spider_to_crawl https://quotes.toscrape.com/
Then the output of the project directory looks like the one given in the image. (Please refer this if you want to know more about a scrapy project and get familiar with it).
Let's have a look at our spider_to_crawl.py file present inside our spiders folder. This is the file where we are writing the URL where our spider has to crawl and also a method named parse() which is used to describe what should be done with the data scraped by the spider.
This file is automatically generated by "scrapy genspider" command used above. The file is named after the spider's name. Below given is the default file generated.
Item pipeline is a pipeline method that is written inside pipelines.py file and is used to perform the below-given operations on the scraped data sequentially. The various operations we can perform on the scraped items are listed below:
We will be performing some of these operations in the examples below.
Operations are performed sequentially since we are using settings.py file to describe the order in which the operations should be done. i.e. we can mention which operation to be performed first and which to be performed next. This is usually done when we are performing several operations on the items.
Let's first see the inner structure of a default pipeline file. Below is the default class mentioned in that file.
For performing different operations on items we have to declare a separated component( classes in the file) which consists of various methods, used for performing operations. The pipelines file in default has a class named after the project name. We can also create our own classes to write what operations they have to perform. If any pipelines file consists of more than one class then we should mention their execution order explicitly. The structure of components are defined below:
Each component (class) must have one default function named process_item(). This is the default method which is always called inside the class or component of the pipelines file.
Syntax: process_item( self, item, spider )
Parameters:
- self : This is reference to the self object calling the method.
- item : These are the items list scraped by the spider
- spider : mentions the spider used to scrape.
The return type of this method is the modified or unmodified item object or an error will be raised if any fault is found in item.
This method is also used to call other method in this class which can be used to modify or store data.
Additional methods: These methods are used along with the above-mentionedself-object method to gain extra control over the items.
| Method | Description |
|---|---|
| open_spider(self,spider) |
Spider object which is opened and a reference to self object are the parameters. ( These are default cases of python language). Returns nothing except the fact that it is used to either make some changes or open a file or close a file. |
| close_spider(self,spider) |
Spider object which is closed and a reference to self-object. It also either is used to modify the file or open or close it. |
| from_crawler(cls, crawler) |
Crawler object that is specified. This method is used to give pipeline accessibility to all the core components of the scrapy settings so that pipelines can enhance their functionality, |
Apart from all these methods, we can also create our own method to perform more operations like if we want to store some data then we can have the component that initializes the database and create tables in it, Another component may be there which will add the data to the database.
Before we move ahead and refer to examples, an important point to note is that we should have to register all the components (classes) of the pipelines.py file in the settings.py of the folder structure. This is done to maintain an ordering of the components to be executed and hence produce accurate results.
One more thing to note is that we will require a description of what our item will contain in items.py file. Hence our items.py file contains the below-given code:
We will require this file to be imported in our spider_to_crawl.py file. Hence in this way we can create items to be passed to pipeline. We will mainly be using the Wisdom quotes web page where we can get several quotes based on their authors and respective tags and then we will modify and use items pipelines on the scraped data throughout the example.
To convert the data in JSON format we will be using JSON library of python along with its dumps() method.
The idea is that we will get the scraped data in pipelines.py file and then we will open a file named result.json (if not already present then it will be created automatically) and write all the JSON data in it.
Hence the code in our pipelines.py looks like this:
Our spider_to_crawl.py:
Output:
👁 ImageExplanation:
After using the command "scrapy crawl spider_to_crawl", The below-given steps are going to take place.
This is the JSON file which got created:
👁 ImageNow we are going to present an items pipeline that will scrape the content of the web and store it on the database table defined by us. For simplicity, we will be using the SQLite3 database.
So we will use the idea of how to implement SQLite3 in python to create a pipeline that will receive data from spider scraping and will insert that data to the table in the database created.
spider_to_crawl.py:
We are mentioning the pipeline methods below which are to be written in the pipelines.py File so that the database will be created:
pipelines.py file
Output:
👁 Image
Explanation:
After using the command "scrapy crawl spider_to_crawl", the below-given steps are going to take place