Scraping Websites in Python Using Olostep

Introduction

The Olostep Python SDK simplifies the process of web scraping, particularly for sites that utilize JavaScript to dynamically load content. With this tool, users can extract data from web pages in various formats, such as markdown, HTML, and plain text. This article outlines how to set up and use the Olostep Python SDK for effective web scraping.

Key Features

  • Supports scraping pages that load content with JavaScript.
  • Returns results in markdown, HTML, or plain text formats.
  • Manages browser interactions and proxies automatically.
  • Error handling capabilities to ensure smooth execution, even with malformed URLs.

Getting Started

To use the Olostep SDK, you need the following prerequisites:

  • Python versions 3.11 to 3.13.
  • The Olostep SDK, which can be installed using pip install olostep.
  • An Olostep API key obtained from the Olostep dashboard.

The first 500 credits for scraping are free, with additional costs incurred for each scrape performed. The SDK typically incurs one credit per scrape.

Setup Instructions

Setting up the Olostep client involves importing the necessary libraries and obtaining your API key. The key can be stored in an environment variable or entered directly if it is not available:

import os
from getpass import getpass
from olostep import Olostep

api_key = os.getenv("OLOSTEP_API_KEY") or getpass("Olostep API key: ")
client = Olostep(api_key=api_key)

Scraping Content

To scrape a webpage, specify the URL and desired formats. For instance, to scrape a page to markdown, use the following code:

page = client.scrapes.create(
    url_to_scrape="https://books.toscrape.com/",
    formats=["markdown"],
)

This command retrieves the content and displays the credits consumed during the operation.

Error Handling

When scraping multiple pages, unexpected errors may arise, such as invalid URLs. The SDK gracefully handles such scenarios, allowing the scraping of valid URLs to continue uninterrupted. Users can implement a custom error-handling mechanism:

import threading
from olostep import Olostep_BaseError

def custom_thread_excepthook(args):
    # Custom error handling logic
    pass

threading.excepthook = custom_thread_excepthook

def safe_scrape(url):
    try:
        return client.scrapes.create(url_to_scrape=url, formats=["markdown"])
    except (Olostep_BaseError, KeyError) as error:
        print(f"skipped {url}: OlostepClientError_RequestValidationFailed")
        return None

Conclusion

The Olostep Python SDK provides a streamlined approach to web scraping, especially for websites that rely on JavaScript for content rendering. By utilizing this tool, users can efficiently collect data while adhering to best practices for responsible scraping. Make sure to review each site’s terms of service and their robots.txt before beginning any scraping activities.

Source: www.freecodecamp.org

Related Articles

The surge of public records requests, particularly driven by artificial intelligence (AI), is straining America’s open records systems. Companies such...
Overview of FigureLabs FigureLabs is an AI-powered tool designed to assist researchers in creating clear and informative...
Introduction Setting up light and dark mode in a Next.js project using the shadcn/ui framework is largely automated. This guide walks...