Skip to main content

Overview

The web scraping module provides multiple functions for extracting content from URLs with different levels of processing. All functions are located in prediction_market_agent.tools.web_scrape.

Functions

web_scrape (Markdown)

Extracts web content and converts it to clean markdown format with automatic caching and retry logic. Location: prediction_market_agent.tools.web_scrape.markdown
str
required
The URL to scrape
int
default:"10"
Request timeout in seconds
str | None
Markdown-formatted text content, or None if the request fails or content is non-HTML
Features:
  • Cached for 1 day using @db_cache decorator
  • Automatic retry with 3 attempts and 1-second delays
  • Removes scripts, styles, images, and non-content elements
  • User-agent spoofing to avoid bot detection
  • Returns None for non-HTML content
The function uses tenacity for retry logic and markdownify for HTML-to-markdown conversion:
Elements removed during scraping:
  • <script>, <style>, <noscript> tags
  • <link>, <head> sections
  • <image> and <img> tags

web_scrape (Basic Summary)

Scrapes a URL and automatically summarizes content exceeding 10,000 characters using LLM. Location: prediction_market_agent.tools.web_scrape.basic_summary
str
required
The objective that defines what content to extract and summarize
str
required
The URL of the website to scrape
str
Extracted text content, summarized if longer than 10,000 characters
Summary Process:
  • Uses LangChain’s load_summarize_chain with map-reduce strategy
  • Splits text into 10,000 character chunks with 500 character overlap
  • Employs OpenAI’s GPT model (configurable via DEFAULT_OPENAI_MODEL)
  • Temperature set to 0 for consistent results
The summary function uses the DEFAULT_OPENAI_MODEL from prediction_market_agent.utils and requires OPENAI_API_KEY to be set in the environment.

web_scrape_structured

Scrapes content while preserving HTML structure and hierarchy. Location: prediction_market_agent.tools.web_scrape.structured_summary
str
required
The URL to scrape (automatically prefixes with https:// if protocol is missing)
Whether to remove anchor tags from the output
str
Structured text content with preserved hierarchy
Output Format: Unlike plain text scrapers, this preserves hierarchical structure ideal for tables and nested content:

web_scrape_structured_and_summarized

Combines structured scraping with LLM-based summarization. Location: prediction_market_agent.tools.web_scrape.structured_summary
str
required
The objective defining what information to extract
str
required
The URL to scrape
Whether to remove anchor tags
str
Summarized structured content focused on the objective

WebScrapingTool Class

Function calling tool for microchain agents and LLM function calling. Location: prediction_market_agent.tools.web_scrape.basic_summary

Schema

Usage


Helper Functions

fetch_html

Location: prediction_market_agent.tools.web_scrape.markdown Internal helper with automatic retry logic.
str
required
URL to fetch
int
required
Request timeout in seconds
Response
HTTP response object from requests library

clean_soup

Location: prediction_market_agent.tools.web_scrape.structured_summary Cleans BeautifulSoup Tag objects for structured extraction.
Tag
required
BeautifulSoup Tag object to clean
Whether to remove anchor elements
Tag
Cleaned Tag object
Cleaning operations:
  • Removes all attributes except href
  • Removes noscript, script, style tags
  • Optionally removes anchor tags
  • Removes HTML comments
  • Removes empty elements

Error Handling

All scraping functions can raise:
  • requests.RequestException - Network or HTTP errors
  • requests.Timeout - Request timeout exceeded
  • requests.HTTPError - HTTP error responses (404, 403, etc.)

Exception Handler

Use the tool exception handler for graceful error handling:

Configuration

Environment Variables

str
required
Required for summarization features in basic_summary and structured_summary modules

Database Cache

The markdown scraper uses @db_cache which requires:
str
Database URL for caching (optional, uses in-memory cache if not set)

Best Practices

Choose the Right Tool

  • Use markdown scraper for general content
  • Use basic_summary for long articles
  • Use structured for tables and hierarchical data

Handle Errors Gracefully

Always wrap scraping calls with error handlers or use tool_exception_handler

Respect Timeouts

Set appropriate timeouts based on expected page load times (default: 10s)

Cache Results

Use the markdown scraper’s built-in caching or implement your own for custom scrapers

Dependencies

See Also