How to Scrape Paginated Results
Data spread across multiple pages requires iterating through each page systematically. Pagination patterns vary widely — some use page numbers in the URL, others use query parameters, cursors, or infinite scroll — each requiring a different approach.
Step-by-Step Guide
Identify the pagination pattern
Inspect the URL as you move between pages. Look for page=N, offset=N, or cursor tokens in the URL or in API responses embedded in the page.
Fetch the first page and extract data
Send the first page URL to AlterLab and parse the returned HTML to extract both your target data and the next-page URL or pagination controls.
Build a pagination loop
Extract the next page URL from the current page's pagination controls. Continue fetching and extracting until no next-page link is found.
Handle rate limits gracefully
Add a short delay between requests. AlterLab manages proxy rotation, but respecting the target site's request cadence improves reliability.
Code Example
import requests
from bs4 import BeautifulSoup
def scrape_all_pages(start_url: str, api_key: str, next_selector: str):
url = start_url
results = []
while url:
response = requests.post(
"https://alterlab.io/api/v1/scrape",
headers={"X-API-Key": api_key, "Content-Type": "application/json"},
json={"url": url},
)
html = response.json().get("html", "")
soup = BeautifulSoup(html, "html.parser")
# Extract your data here
results.extend(soup.select(".result-item"))
# Find next page link
next_link = soup.select_one(next_selector)
url = next_link["href"] if next_link and next_link.get("href") else None
return resultsReplace YOUR_API_KEY with your key from the dashboard. No credit card required.
Ready to try it?
Run this tutorial on live websites with AlterLab's API. Start free — no credit card required.
Frequently Asked Questions
Responsible Use
AlterLab is designed for extracting publicly available data. Always review the terms of service for any website you access, respect robots.txt directives, and ensure your use case complies with applicable laws in your jurisdiction.
More tutorials
Browse all how-to guides for web scraping — from beginner extractions to advanced multi-page pipelines.
Your first scrape.
Sixty seconds.
$1 free balance. No credit card. No SDK.
Just a POST request.
No credit card required · Up to 5,000 free scrapes · Balance never expire