How to Do Pagination in Python
If you rely on web scraping for your business or career outlook, you’re likely familiar with the increasing need for speed and efficiency in web scraping tools. After all, your clients or business model requires immediate access to large quantities of data from multiple websites. Any delays, gaps, or inefficiencies in getting this data can quickly put you behind the competition.
Effective web scraping tools require resources that can quickly access all data on a website. But this also means that a good web scraping program must be able to navigate any potential obstacles to efficient data collection. One such obstacle that many web scraping programs struggle with is pagination.
If you are using a Python-based web scraping program, you should learn how to do pagination in Python and how Python web scraping tools minimize pagination-related delays in data collection. Web proxy providers like Rayobyte can provide the web scraping tools you need to mitigate these issues and effectively scrape data from paginated websites.
Pagination in Python: What Is It?
Pagination is the process of dividing a website into separate pages. Each page contains different, though related, sets of data on the advertised product or service. Sometimes pagination divides a longer article or data stream into separate pages, much like individual chapters in a novel.
In other cases, pagination separates data corresponding to products or services grouped under the broader heading. The latter type of pagination is widespread in eCommerce, especially for different but related products for sale.
Pagination in Python will assign each page a separate URL. In these cases, the website will generally be laid out so users can easily transition from one page to another in sequential order.
While pagination provides a convenient experience for regular users, it does pose a problem for web scrapers. If you are trying to scrape data from a paginated website, your scraper will need to go through each page one by one.
The scraper must recognize the paginated layout of each page containing meaningful data and have the capacity to navigate the specific type of pagination and its respective means of transition from one page to the next.
If a web scraper is not designed to navigate pagination in Python on a website, it may experience costly delays when trying to scrape data from each page. It might also run into issues getting from one page to the next.
If your web scraper cannot understand the type of pagination on the website it is scraping, it may get stuck on one page without any means of transitioning to the next one. These delays will turn into costly gaps in your web scraping returns.
Common types of pagination in Python
When learning how to do pagination in Python, it’s a good idea to learn about some of the most common types of pagination you’ll encounter. Understanding the different types of pagination can help you find the best web scraping programs for your needs.
Pagination types in Python differ by how they transition from one page to the next. An ideal Python-based web scraper can effectively navigate each major type of pagination and quickly scrape data.
Pagination in Python with a “next” button
One of the most common types of pagination uses a “next” button to transition between pages. On these websites, several pages will be collected in a logical order, and the user can move to the next page with a prominent “next” button after reading the previous one.
That page will have another “next” button that will take the user to the following page, all the way until the final page in the sequence. In most examples of this type of pagination, the “next” button will be at the bottom of the page.
In many cases, the website layout complements the “next” button with a “previous” button opposite it that will take the user to the previous page. Some websites with “next” button pagination will also have clickable page number links in between the “previous” and “next” buttons.
Pagination in Python with page numbers
Some paginated websites feature clickable page number links at the bottom of the page without the “previous” and “next” buttons. These layouts usually feature all page numbers linked at the bottom of the page.
One of the main benefits of this type of pagination is that it allows users to access any page from where they are instead of clicking through several other pages via the “next” or “previous” buttons. However, this type of pagination usually works best for websites with limited page numbers (e.g., less than ten) since all pages must be displayed at the bottom.
Pagination in Python with infinite scroll
One common feature of both “next button” and “page number” pagination in Python coding is a distinct URL for each page. When you click on the “next” button or the linked page number, the website’s URL will change in response.
However, some types of pagination in Python do not require URL changes. One common type uses an “infinite scroll” mechanism to load additional pages on a website.
Instead of separating site data into different pages with URLs, pagination in Python for a website with an infinite scroll mechanism will display a limited amount of data on your browser when you first open the page.
Then, as you scroll down to browse the data, the website will automatically load more data at the bottom of the page.
To access all the data on the page, you would continue scrolling until all data has loaded. Your URL will not change. The site will use an API to import more data, essentially creating a new page at the bottom of the previous one.
Pagination in Python with load more
A related type of pagination uses a “load more” button to introduce more data onto a page. Like the “infinite scroll” pagination, this type of pagination does not involve different URLs for different pages.
However, unlike infinite scroll pagination, “load more” pagination will have a specific button included at the bottom of the page (usually labeled as “load more” or something similar).
When you press this button, the site will load an additional page underneath the page you have already read. This provides a continuous top-down reading effect similar to infinite scroll pagination.
The key difference here is that the user will need to manually press the “load more” button at the bottom of the page to open the additional data.
What is pagination in SEO?
Pagination also creates specific issues for SEO-focused website development and web scrapers. If you need to scrape data from certain websites, it’s helpful to understand how SEO considerations may have affected that site’s pagination and how this may impact your web scraping operations.
For some SEO specialists, pagination may threaten to dilute search returns across multiple pages. If this happens, no single page will rank particularly highly on a Google search of a particular keyword.
There may also be fears that too much pagination may create duplicate content in a search return or prevent one page from having enough content to rank highly on specific internet searches.
However, these concerns can be mitigated with proper site design. Different content spread across multiple pages can prevent duplication in a search return (even if meta tags and meta descriptions are the same). Site developers can also limit pagination on their sites to keep diluted content to a minimum.
Too much content on a single page can often diminish the user’s experience and prevent most users from reading the entire page. By keeping pagination to a minimum, websites can prevent diluted search returns while maintaining the average site visitor’s interest.
If you are scraping data from websites with these kinds of SEO considerations, your web scraping program should take this into account. For example, if you are scraping a website with a “next button” type pagination, you can better organize web data by recognizing the distribution of data on each page.
Your scraper will recognize the anchor element of the “next button” on each page, and by flipping from one page to the next, the scraper may collect data of decreasing SEO value.
If a website places high-priority information on its main landing page to maximize SEO results, your scraper should organize landing page data before accessing the anchor element of the “next” button.
What are paginated reports?
Sometimes you may have to scrape data from online paginated reports, which are official reports or documents divided into separate pages. Unlike paginated eCommerce sites, paginated reports often display data in organized tables and charts, even if those tables and charts are divided across multiple pages.
A normal web scraper should be able to collect all data from a table format fairly easily. However, if that data is paginated, your scraper may encounter difficulties accessing and organizing data from one page to the next. If the paginated report is displayed in a PDF, a good web scraper should be able to recognize the organization of the data within the framework of the page divisions.
If the paginated report is designed using Python, the page differentiations will be found within the HTML code. Web scrapers should parse each page’s HTML code to recognize the paginated divisions while also parsing the HTML of the table display to maintain the coherent organization of the report’s data.
What is pagination in programming?
In the world of programming, pagination ensures that a web browser can quickly access and display all relevant data according to the specific page divisions you have prescribed. Remember, when a web browser accesses backend programming data for a website, it moves backward to try and translate that data into the accessible display settings.
When a browser accesses a paginated website, it will access all data at once. The pagination programming makes sure the browser divides the data according to the correct number of pages while still organizing specific data points onto specific pages. Pagination programming also tells the browser how to transition from one page to the next.
For example, you might program your backend pagination code to tell the browser to have a “next” button that allows the users to move from one page to the next. Alternatively, you might program your code to have infinite scroll pagination on the browser display.
Parsing the code for pagination in Python does not need to be a source of anxiety for anyone with some degree of familiarity with Python code. Many of the basic principles of Python code parsing apply to cases of paginated web pages. The only real consideration here is to make sure you can identify the type of pagination you’re working with (i.e., “next button,” infinite scroll, etc.) as well as the key anchor elements.
First, you should determine which Python library you want to use. If you are parsing Python pagination code for web scraping, the Beautiful Soup library is usually your best bet. While many Python libraries have code you can use for pagination in Python, Beautiful Soup stands out because it was specifically designed to parse Python data from websites. Many paginated websites you will encounter while web scraping use either HTML or XML code trees.
While this code is meant to create front-end displays for website users, it works according to a top-down structure that makes it machine-readable at the backend. Any web scraping package you use must be able to not only read all of the HTML or XML data from the backend of a website but also parse and extract relevant data efficiently.
The Beautiful Soup library offers packages that allow you to parse HTML and XML data by creating a parse tree. You can extract large quantities of data from individual web pages from this parse tree. This makes Beautiful Soup the best library to use for web scraping.
To get an idea of the kinds of paginated web pages you will likely be dealing with, look at examples from the internet today. For example, here is a link to Barnes and Noble’s list of best summer reads of 2023. Note that list of summer reading recommendations contains 96 book recommendations divided across five discrete pages. Also note that this particular paginated website uses the “next button” type of pagination, with additional links to individual pages.
At the bottom of the page, the website displays a right arrow button that will take the user from page one to page two. On page two, the user can click on the right “next” button or the left “previous” button (the first page has a left previous button for display purposes, but in this case, it is an inactive link).
Between the “previous” and “next” buttons at the bottom are enumerated links to individual pages. Unlike full “page number” type pagination, this website does not include every single page link between the “next” and previous” buttons. Instead, the first page contains links to the second and third pages, while the link to the fourth page is replaced with an ellipsis.
In practice, this gives site users easy options for navigating the different pages without being overwhelmed. The link to the fifth and final page also lets them know how many pages are included in the list.
Web scraping pagination
To parse the site data, look at the URL on each page. Here is the URL for page one of the five-page display:
The URL ends with “page=1.” If you click the “next” button to go on to page 2, you will see that the URL likewise changes from “page=1” to “page=2.”
Next, you can inspect the anchor code for the buttons at the bottom that take users to different pages. For each link, the href is given as the link to the page that button will take you to. However, note that while the URLs for the enumerated page number links remain the same across all five pages, the href URLs for the “next” and “previous buttons are relative to what page you are on.
So if you inspect the “next” button on page one, it will provide an href for the URL of the second page. However, inspecting the “next” button on the second page will give you an href with the URL of the third page. Also, note that the “next” button on each of the five pages is listed as a class in the actual HTML code.
In cases where the pagination uses a “next” and “previous” button layout, the web scraping program must work with the relative href framework for the relevant links on each page. This means that the anchor element access on the first page must be different from that on the second page, and so on.
Web scraping pagination in Python
Fortunately, web scraping pagination in Python makes this easier. First, import a specific parser from a library like Beautiful Soup. You might use a code like:
pip install requests beautifulsoup4 lxml
Once you have your parser, the next step is to access the site’s HTML code to see how your parser can access the data. Using the same Barnes and Noble reading list, take a look at the basic web scraping code you might use for the first page in and of itself:
from bs4 import BeautifulSoup
url = ‘ https://www.barnesandnoble.com/b/best-summer-reads-of-2023/_/N-2vji?Nrpp=20&page=1 ’
response = requests.get(url)
soup = BeautifulSoup(response.text, “lxml”)
footer_element = soup.select_one(‘li.current’)
# Other code to extract data
If your parser is only scraping this code in and of itself, its outcome will be the page-specific designator at the end of the URK. So, if your scraper parses the first page of the five-page Barnes and Noble book list, its outcome will be “page=1.”
Here, your scraper will send GET requests with the “page=1” designator in place to the specific URL. Because the CSS Selector is website-specific, the parser will only extract the “page=1+ data unless you program the parser to handle the site’s pagination.
You can do this by modifying your parser’s code to identify and utilize the “next” button. Using Beautiful Soup, you would create a code that looks like this:
next_page_element = soup.select_one(‘li.next > a’)
Remember that the href for the “next” button is relative since the URL changes depending on what page you’re on. To get around this, you can access the urllib.parse module to get urljoin for your parser. This will let you create a “while true” loop for your entire code, allowing your web scraper to use the “next” button on each of the five pages despite the relative href URL.
The resulting code will look something like this:
url = ‘https://www.barnesandnoble.com/b/best-summer-reads-of-2023/_/N-2vji?Nrpp=20&page=1’ while True:
response = requests.get(url)
soup = BeautifulSoup(response.text, “lxml”)
footer_element = soup.select_one(‘li.current’)
next_page_element = soup.select_one(‘li.next > a’)
next_page_url = next_page_element.get(‘href’)
url = urljoin(url, next_page_url)
Finding the Best Web Scraping Resources for All Python Web Scraping Pagination
Pagination is an essential element of any website looking to improve the overall user experience. Whether a website uses a “next” button to move between pages or else uses an infinite scroll or “load more” function, pagination helps convey large quantities of information in a way that doesn’t overwhelm a visitor. If you depend on prompt and efficient web scraping, you need a parser that can recognize the pagination code in a website and get all essential data without any delays or downtime.
Web scraping Python-based website code requires parsers that can handle the complexities of the task. While knowing how to do pagination in Python is important for web scraping, you must also have the best web scraping solutions available. Rayobyte’s leading web proxy and web scraping solutions use the best resources from Python to deliver the best web scraping results. If you depend on fast, reliable, and comprehensive web scraping, get in touch with Rayobyte for a free demo of the best web scraping and proxy solutions.
The information contained within this article, including information posted by official staff, guest-submitted material, message board postings, or other third-party material is presented solely for the purposes of education and furtherance of the knowledge of the reader. All trademarks used in this publication are hereby acknowledged as the property of their respective owners.
Start a risk-free, money-back guarantee trial today and see the Rayobyte
difference for yourself!