Scrapy is an open source and framework for extracting information from the different website. It uses APIs and is written in Python. Scrapy is currently maintained by a web scraping company named as Scrapinghub Ltd.
It is a simple tutorial on how to write web crawler using Scrapy, parse Craigslist and store information in CSV format. The five main steps of this tutorial are mentioned below:
1. Create a new Scrapy project
2. Write a spider to crawl a website and extract data
3. Export the scraped data using the command line
4. Change spider to follow links
5. Use spider arguments
1. Create a project
The first step is to create a project. You would have to download and install Scrapy. In its search bar, you should enter the directory name where you want to store the data. Scrapy uses different spiders to extract information, and these spiders make initial requests to create directories. To put a spider to work, you need to visit the list of directories and insert a particular code there. Keep an eye on the files in your current directory and notice two new files: quotes-a.html and quotes-b.html.
2. Write a spider to crawl a website and extract data:
The best way to write a spider and extract data is creating different selectors in Scrapy's shell. You should always enclose the URLs in quotes; otherwise, Scrapy will change the nature or names of those URLs instantly. You should use double quotes around a URL to write a spider appropriately. You should use.extract_first() and avoid an index error.
3. Export the scraped data using the command line:
It is important to export the scraped data using the command line. If you don't export it, you will not get accurate results. The spider will generate different directories containing useful information. You should use the yield Python keywords to export this information in a better way. Importing data to JSON files is possible. The JSON files are useful for programmers. Tools like JQ help export scraped data without any problem.
4. Change spider to follow links:
In small projects, you can change spiders to follow links appropriately. But it is not necessary with large-sized data scraping projects. A placeholder file for Item Pipelines will be set up when you change spider. This file can be located in the tutorial/pipelines.py section. With Scrapy, you can build sophisticated spiders and change their location anytime. You can extract multiple sites at a time and carry out various data extraction projects.
5. Use spider arguments:
The parse_author callback is a spider argument that can be used to extract data from dynamic websites. You can also provide command line arguments to the spiders with a specific code. The spider arguments become spider attributes in no time and change the overall look of your data.
In this tutorial, we covered only the basics of Scrapy. There are a lot of features and options for this tool. You just need to download and activate Scrapy to know more about its specifications.