How to Turn the Web into Data with Python and Scrapy

A guide to web scraping powered by Python and Scrapy

Feature Image

Since its inception, websites are used to share information. Whether it is a Wikipedia article, YouTube channel, Instagram account, or a Twitter handle. They all are packed with interesting data that is available for everyone with access to the internet and a web browser.

But, what if we want to get any specific data programmatically?

There are two ways to do that:-

  1. Using official API
  2. Web Scraping

The concept of API (Application Programming Interface) was introduced to exchange data between different systems in a standard way. But, most of the time, website owners don’t provide any API. In that case, we are only left with the possibility to extract the data using web scraping.

Basically, every web page is returned from the server in an HTML format. Meaning that our actual data is nicely packed inside HTML elements. It makes the whole process of retrieving specific data very easy and straightforward.

This tutorial will be an ultimate guide for you to learn web scraping using Python programming language. At first, I’ll walk you through some basic examples to make you familiar with web scraping. Later on, we’ll use that knowledge to extract data of football matches from Livescore.cz .

Without any further ado, let’s follow along with me.


Getting Started

To get us started you will need to start a new Python3 project with and install Scrapy (a web scraping and web crawling library for Python). I’m using pipenv for this tutorial, but you can use pip and venv, or conda.

pipenv install scrapy

At this point, you have Scrapy, but you still need to create a new web scraping project, and for that scrapy provides us with a command line that does the work for us.

Let’s now create a new project named web_scraper by using the scrapy cli.

If you are using pipenv like me, use:

pipenv run scrapy startproject web_scraper .

otherwise, from your virtual environment using

scrapy startproject web_scraper .

This will create a basic project in the current directory with the following structure:

scrapy.cfg
web_scraper/
    __init__.py
    items.py
    middlewares.py
    pipelines.py
    settings.py
    spiders/
        __init__.py

Building our first Spider with XPath queries

We will start our web scraping tutorial with a very simple example. At first, we’ll locate the logo of the Live Code Stream website inside HTML. And as we know it is just a text and not an image, so we’ll simply extract this text.

The code

To get started we need to create a new spider for this project. We can do that by either creating a new file or using the CLI.

Since we know already the code we need we will create a new Python file on this path /web_scraper/spiders/live_code_stream.py

Here are the contents of this file.

import scrapy

class LiveCodeStreamSpider(scrapy.Spider):
    name = "lcs"

    start_urls = [
        "https://livecodestream.dev/"
    ]

    def parse(self, response):
        yield {
            'logo': response.xpath("/html/body/header/nav/a[1]/text()").get()
        }

Code explanation:

  1. First of all, we imported the Scrapy library. It is because we need its functionality to create a Python web spider. This spider will then be used to crawl the specified website and extract useful information from it.

  2. We created a class and named it LiveCodeStreamSpider. Basically, it inherits from scrapy.Spider that’s why we passed it as a parameter.

  3. Now, an important step is to define a unique name for your spider using a variable called name. Remember that you are not allowed to use the name of an existing spider. Similarly, you can not use this name to create new spiders. It must be unique throughout this project.

  4. After that, we passed the website URL using the start_urls list.

  5. Finally, create a method called parse() that will locate the logo inside HTML code and extract its text. In Scrapy, there are two methods to find HTML elements inside source code. These are mentioned below.

    • CSS
    • XPath

    You can even use some external libraries like BeautifulSoup and lxml . But, for this example, we’ve used XPath.

    A quick way to determine the XPath of any HTML element is to open it inside the Chrome DevTools. Now, simply right-click on the HTML code of that element, hover the mouse cursor over “Copy” inside the popup menu that just appeared. Finally, click the “Copy XPath” menu item.

    Have a look at the below screenshot to understand it better.

    Find XPath using Chrome Dev Tools

    Find XPath using Chrome Dev Tools

    By the way, I used /text() after the actual XPath of the element to only retrieve the text from that element instead of the full element code.

Note: You’re not allowed to use any other name for the variable, list, or function as mentioned above. These names are pre-defined in Scrapy library. So, you must use them as it is. Otherwise, the program will not work as intended.

Run the Spider:

As we are already inside the web_scraper folder in command prompt. Let’s execute our spider and fill the result inside a new file lcs.json using the below code. Yes, the result we get will be well-structured using JSON format.

pipenv run scrapy crawl lcs -o lcs.json
scrapy crawl lcs -o lcs.json

Results:

When the above code executes, we’ll see a new file lcs.json in our project folder.

Here are the contents of this file.

[
{"logo": "Live Code Stream"}
]

Another Spider with CSS query selectors

Most of us love sports, and when it comes to Football, it is my personal favorite.

Football tournaments are organized frequently throughout the world. There are several websites that provide a live feed of match results while they are being played. But, most of these websites don’t offer any official API.

In turn, it creates an opportunity for us to use our web scraping skills and extract meaningful information by directly scraping their website.

For example, let’s have a look at Livescore.cz website.

On their home page, they have nicely displayed tournaments and their matches that will be played today (the date when you visit the website).

We can retrieve information like:

  • Tournament Name
  • Match Time
  • Team 1 Name (e.g. Country, Football Club, etc.)
  • Team 1 Goals
  • Team 2 Name (e.g. Country, Football Club, etc.)
  • Team 2 Goals
  • etc.

In our code example, we will be extracting tournament names that have matches today.

The code

Let’s create a new spider in our project to retrieve the tournament names. I’ll name this file as livescore_t.py

Here is the code that you need to enter inside /web_scraper/web_scraper/spiders/livescore_t.py

import scrapy

class LiveScoreT(scrapy.Spider):
    name = "LiveScoreT"

    start_urls = [
        "https://livescore.cz/"
    ]

    def parse(self, response):
        for ls in response.css('#soccer_livescore .tournament'):
            yield {
                'tournament': ls.css('.nation a::text').get()
            }

Code explanation:

  1. As usual, import Scrapy.

  2. Create a class that inherits the properties and functionality of scrapy.Spider.

  3. Give a unique name to our spider. Here, I used LiveScoreT as we will only be extracting the tournament names.

  4. The next step is to provide the URL of Livescore.cz.

  5. At last, the parse() function loop through all the matched elements that contains the tournament name and join it together using yield. Finally, we receive all the tournament names that have matches today.

    A point to be noted is that this time I used CSS selector instead of XPath.

Run the newly created spider:

It’s time to see our spider in action. Run the below command to let the spider crawl the home page of Livescore.cz website. The web scraping result will then be added inside a new file called ls_t.json in JSON format.

pipenv run scrapy crawl LiveScoreT -o ls_t.json

By now you know the drill.

Results:

This is what our web spider has extracted on 18 November 2020 from Livescore.cz . Remember that the output may change every day.

[
{"tournament": "International - World Cup Qualification CONMEBOL"},
{"tournament": "Brazil - Serie A"},
{"tournament": "International - UEFA Nations League A Grp. 3"},
{"tournament": "International - UEFA Nations League A Grp. 4"},
{"tournament": "International - UEFA Nations League C Grp. 1"},
{"tournament": "International - UEFA Nations League D Grp. 1"},
{"tournament": "International - UEFA Nations League D Grp. 2"},
{"tournament": "..."}
]

A more advanced use case

In this section, instead of just retrieving the tournament name. We will go the next mile and get complete details of tournaments and their matches.

Create a new file inside /web_scraper/web_scraper/spiders/ and name it as livescore.py. Now, enter the below code in it.

import scrapy

class LiveScore(scrapy.Spider):
    name = "LiveScore"

    start_urls = [
        "https://www.livescore.cz/yesterday.php"
    ]

    def parse(self, response):
        table_tr = response.css('tr')
        
        tournaments = []

        for tr in table_tr:
          if tr.css('.tournament'):
            tournaments.append({
                    'name': tr.css('.nation a::text').get(),
                    'matches': []
                })
          elif tr.css('.match'):
            team_score = tr.css('.col-score strong::text').get()

            if team_score is not None:
                team_1_score = team_score.split(':')[0]
                team_2_score = team_score.split(':')[1]
            else:
                team_1_score = None
                team_2_score = None

            tournaments[-1]['matches'].append({
                'time': tr.css('.match .col-time time::attr(datetime)').get(),
                'state': tr.css('.match .col-state span::text').get(),
                'team_1_name': tr.css('.col-home a::text').get(),
                'team_1_score': team_1_score,
                'team_2_name': tr.css('.col-guest a::text').get(),
                'team_2_score': team_2_score
            })
        
        for t in tournaments:
            yield {
                'tournament': t
            }

Code explanation:

The code structure of this file is the same as our previous examples. Here, we just updated the parse() method with new functionality.

Basically, we extracted all the HTML <tr></tr> elements from the page. Then, we loop through them to find out whether it is a tournament or a match. If it is a tournament, we extracted its name. In the case of a match, we extracted its “time”, “state”, and “name and score of both teams”.

Run the example:

Type the following command inside the console and execute it.

pipenv run scrapy crawl LiveScore -o ls.json

Results:

Here is a sample of what it’s retrieved:

[{
    "tournament": {
        "name": "International - World Cup Qualification CONMEBOL",
        "matches": [{
            "time": "2020-11-18T00:00:00+01:00",
            "state": null,
            "team_1_name": "Uruguay",
            "team_1_score": "0",
            "team_2_name": "Brazil",
            "team_2_score": "2"
        }, {
            "time": "2020-11-18T00:00:00+01:00",
            "state": null,
            "team_1_name": "Paraguay",
            "team_1_score": "2",
            "team_2_name": "Bolivia",
            "team_2_score": "2"
        }, {
            "time": "2020-11-18T01:30:00+01:00",
            "state": null,
            "team_1_name": "Peru",
            "team_1_score": "0",
            "team_2_name": "Argentina",
            "team_2_score": "2"
        }]
    }
}]

Now with this data, we can do anything we want, like use it to train our own neural network to predict future games :p.


Conclusion

Data Analysts often use web scraping because it helps them in collecting data to predict the future. Similarly, businesses use it to extract emails from web pages as it is an effective way of lead generation. We can even use it to monitor the prices of products.

In other words, web scraping has many use cases and Python is completely capable to do that.

So, what are you waiting for? Try scraping your favorite websites now.

Thanks for reading!

Join the Free Newsletter

A free, weekly e-mail with the best new articles, courses, and special bonuses.

We won't send you spam. Unsubscribe at any time.