Python Scrapy tutorial for beginners — 03 — How to go to the next page
On our last lesson, extracting all the data with Scrapy, we managed to get all the books URL and then extracted the data from each one. We were limited to the books on the main page, as we didn’t know how to go to the next page using Scrapy.
Until now.
In this post you will learn how to:
- Navigate to the ‘next page’
- Solve routing problems
- Extract all the data of every book available
Video version of this lesson
Our game-plan
Initially we just listed all the book URLs and then, one by one, we extracted the data.
As we had 20 books, we just listed 20 book URLs, and then parsed those 20 URLs, yielding the result.
We just need to add another step.
Now, we’ll list 20 book URLs, parse them, and then, if there is a ‘Next’ page, we’ll navigate to it to repeat the process, listing and yielding the new 20 book URLs, until there a re no more pages.
In our Beautiful Soup tutorial we used the same strategy:
And that’s what we are going to start using right now.
Checking if there is a ‘next page’ available
Let’s start from the code we used in our second lesson, extract all the data
Since this is currently working, we just need to check if there is a ‘Next’ button after the for loop is finished. Right-click on the next button:
The next page URL is inside an a tag, within a li tag. You know how to extract it, so create a next_page_url we can navigate to. Beware, it is a partial URL, so you need to add the base URL. As we did it before, you can do it yourself. Give it a try.
This is how I did it:
Run the code with scrapy crawl spider -o next_page.json and check the result.
What’s going on? There is only 20 elements in the file! Let’s check the logging to see what’s going on.
We managed to get the first 20 books, but then, suddenly, we can’t get more books…
The books.toscrape.com is a website made by Scraping Hub to train people on web scraping, and they have little traps you need to notice. Compare the successful URLs (blue underline) with the failed ones (red underline). There is a /catalogue missing on each routing. They didn’t add it to make you fail.
Let’s solve that problem.
Solving the ‘book’ routing problem
As /catalogue is missing from some URLs, let’s have a check: If the routing doesn’t have it, let’s prefix it to the partial URL. As simple as that.
Try it on your own before continuing. You can check my code here:
Let’s run the code again! It should work, right? scrapy crawl spider -o next_page.json
Now we have more books! But only 40. We managed to get the first 20, then the next 20. Then, something happened. We didn’t get the third page from the second one. Let’s go to the second page and see what’s going on with the next button and compare it with the first one (and its link to the second one)
We have the same problem we had with the books: Some links have /catalogue, some others don’t.
Solving the ‘next’ routing problem
As we have the same problem, we have the same solution. One you can solve easily. Why don’t you try? Again, you just need to check the link and prefix /catalogue in case that sub-string isn’t there.
If you couldn’t solve it, this is my solution:
You can see the pattern: We get the partial URL, we check if /catalogue is missing and if it does, we add it. Then, we add the base_url and we have our absolute URL.
Run the spider again: scrapy crawl spider -o next_page.json.
Now we have our 1000 books. Every single one. 🙂
This is the final code:
Conclusion
You hit a milestone today. Now you are able to extract every single element from a website.
You have learnt that you need to get all the elements on the first page, scrap them individually, and how to go to the next page to repeat this process. Let me show the diagram once again:
And not only that. This example was a tricky one as we had to check if the partial URL had /catalogue to add it.
Normally, paginating websites with Scrapy is easier as the ‘next’ button contains the full URL, so this example was even harder than normal and yet you managed to get it!
But… what if I tell you that this can be even easier than what we did?
Instead of grabbing your pitchfork and heading to my home, go to the fourth lesson where you will learn how to scrape every single item in an even easier way using crawlers.
Originally published at https://letslearnabout.net on September 12, 2019.