In both amateur and professional use, there is often a need to aggregate data from several sources. This lets us, for example, compare the price of a product in different online stores, look up a specific item on the internet, or supplement the information on one page with information from another. You surely use apps and portals that gather information this way. You may also have heard the terms "crawler" and "scraper", or "crawling" and "scraping". Some people treat them as the same thing, but they shouldn't: these terms mean different operations, although they belong to the same field.

Today we will deal with exactly that, that is, we will explain what crawling is and what scraping is. The difference is quite big, even though both serve one purpose, namely automating the browsing of information from other corners of the internet. Still, they correspond to different levels of searching.

What do crawlers do?

Crawling is used to reach pages, for example in order to index them. In short, with a crawler we can move through pages, discovering the connections between them and the links leading to them. This is where we most often talk about robots (such as Google's) that find a page, check which other sites it points to through its links, and then follow them further. The web pages themselves are "read" only for the information that is key to determining the nature of the site. Most often that is the metadata, meaning what appears between the <head></head> tags, but not only that. As I mentioned, a crawler is mainly interested in links leading to other pages, and in simulating searches for elements the way a human would do it.

I have already mentioned that search engines use this mechanism. But where can you use it in your own applications? Imagine you are building a price comparison site and have to find products in many stores. You have a list of stores (for example, electronics retailers) and use a crawler to discover the link to a specific item. You often also have to implement paging through pages in order to reach a specific listing. Very often crawling is a prelude to the next stage, which we will discuss in a moment.

What do scrapers do?

Scraping means finding and downloading specific information from a page. The technical term is data extraction. At this point we no longer move between pages but focus on a single site and go through the HTML code looking for the data we care about. Sometimes this is quite simple (if the site's creators mark specific places well with IDs or classes), and sometimes it takes more work, preparing for different versions of subpages, dynamically loaded elements and so on.

Continuing the example from the previous paragraph: once we find the product links, we can "scrape" the price, technical parameters, description, photo and so on from them. This way we can move already structured data into our price comparison site, where further processing takes place. Of course, we need a separate scraper (and crawler) for each store, because each has its web page built differently, with different element positions, a different naming convention, and sometimes information spread across other views.

Sometimes a crawler and a scraper actually become one application. This happens most often when we don't need much information and can take it directly from the product list. For example, I once wrote an e-book price comparison tool for my own needs. My input was the books I was interested in, but I didn't want to download any parameters other than the price. As we know well, online stores are usually built so that the item list already shows the basic data of each item, including its price. So the crawler (which could find the list containing a given e-book or e-books) became a scraper at the same time (the list immediately showed the e-book's price and the information needed to make sure it was the item being sought).

What are crawling and/or scraping used for, and when?

First of all, these techniques are used in situations where no other way of integrating is possible. There is no denying that where an API is available, things get much easier, because it is a more convenient route to downloading data automatically, with values already labeled. Unfortunately, sometimes the situation forces us to resort to other methods, closer to human behavior, and this is where crawling and scraping come in handy.

Price comparison runs through this whole text, and it is arguably the most popular use of such tools. We often want to look up specific information to make better purchasing decisions. For one or two items this can be done by hand, but with several dozen products it becomes hard for a human to process. What is more, manual work takes much more time, and prices, for example, may change between the start and the end of the search. That is why robots are used, which get this data faster.

Scraping is useful for more than numerical values, though. You can pull out far more information this way, such as comments under articles, the content collected on a site, or audiovisual material. In addition, sometimes all we need is the crawler itself, which gives us a database of links or simply searches predefined pages for the presence of certain information.

Finally, sometimes an API is too hard to use or cannot be used for some reason (for example, because of an additional verification step). In such cases, scraping pages can be an alternative that gets at least part of the data you need. It is also worth mentioning that while the main goal of scraping is downloading information, the process itself can also be used to analyze the structure of a page and extract data from that, which lets you carry out research, for example, or better prepare more universal solutions.

Risk factors

There is no rose without thorns, and scraping and crawling have a great many thorns to watch out for. First of all, this technique is very sensitive to changes on the page. In theory, an API change also forces the applications that use it to adapt, but it rarely happens that the changes make the existing API client useless, and the format of the transmitted information rarely changes from XML to JSON, for example. When a web page's structure changes, however, scrapers most often have to be rewritten from scratch or almost from scratch, because they were prepared for a specific order of information, nesting and so on.

In addition, scraping and crawling most often require sending many HTTP requests, more than with an API. As a result, not only is the data transfer larger, but the sites the robot tries to read from may identify a given crawler as a spying robot and block it temporarily or permanently. This doesn't happen often, but once it does, getting the information becomes impossible. The remedies are limiting the speed of data collection or using many different IP addresses, but these methods slow down the search for information and may simply not be enough.

It is also worth remembering that these techniques, and writing scrapers, are simply hard and time-consuming. You need to analyze the HTML code, design a scenario for how the robot will "walk" around the page, and then work out how to discover where particular information is located. As I mentioned, if the page's creators describe it well, things get much easier, but that is not always the case. Besides, simulating certain mechanisms (such as search or pagination) is often hard in itself. And let's remember that such an application has to be prepared for each site separately.

Scrapers and crawlers are also "uncertain". When the structure of information on a page is very bad, sometimes you simply cannot extract the data in a satisfactory way, or be sure that a given value should be interpreted one way and not another. Unfortunately, such cases happen, and they require either a change of approach, more work put into writing the script, or the use of machine learning or artificial intelligence.

Finally, it is worth realizing that with the spread of modern JS frameworks, pages sometimes don't load all their code at once. As a result, a robot visiting a site may see only the basic structure, which tells it nothing, because the rest of the data is loaded in the background. This creates additional difficulties and sometimes makes writing the tool impossible altogether. In that case you need a different approach.

What are crawlers and scrapers written in?

These tools rely on downloading the HTML code of a page and analyzing it. Most often this involves sending a single GET request, getting its content, transforming it into a DOM tree, and then moving through the successive levels much like looking for files in a folder on your computer. Information is most often identified by:

  • HTML IDs or classes,
  • the order of nodes,
  • the style assigned to particular tags (sometimes this is the only method left),
  • the nature of the information (for example, its length).

As you can see, this requires writing a so-called DOM parser or using ready-made libraries, which are available in practically every significant programming language. Python is very often named as the best language for writing crawlers and scrapers. Among other things, it offers the Beautiful Soup library, for which you can find plenty of tutorials. Python is a language you can write quickly in, and it comes with many solutions available "out of the box", which is not insignificant given that sooner or later a crawler or scraper will have to be rewritten anyway. Fans of PHP, Java or JavaScript will, of course, also find suitable modules in those languages to make the whole process more pleasant. Crawlers and scrapers are often not very large programs, but they can be quite complicated depending on the nature of the site and the amount of data collected.

There is also a second approach, which can come in handy when a page contains many dynamic elements and, for example, the user (and so also a robot, which is much faster than a real person, even too fast) has to wait for elements to load. In that case, writing a scraper the classic way may not work, because we won't get the HTML we are interested in right away to work on. Instead, you can choose solutions known from writing automated acceptance tests, the most popular of which is Selenium, available both as a browser extension and as an API for various programming languages. It is not the only library of this kind, but it can still be used successfully to simulate a normal user browsing a page, waiting for information to appear, and collecting it.

Summary

Crawlers and scrapers are small applications (or rather scripts) that fetch information or its location, and they are usually written with specific websites in mind. As APIs become more widely available on various sites and their creators more open, developers more often turn to web services or dedicated libraries, but sometimes this is not possible and you have to read the page the way a human and a browser do. It is worth remembering that, because they depend so heavily on how information is stored on a web page, robots are very sensitive to any changes. So you can't assume they are unchangeable and will serve for the whole duration of a project. Fortunately, they often "work" long enough, but it is worth making sure such scrapers can be replaced easily and not making the whole system depend on them.

Best regards and thank you, Jakub Rojek.