Search and extract information from multiple web sources using AI:
Copy
from scrapegraph_py.models import TimeRangeresponse = client.searchscraper( user_prompt="What are the key features and pricing of ChatGPT Plus?", time_range=TimeRange.PAST_WEEK # Optional: Filter results by time range)
The Pydantic object that describes the structure and format of the response (AI extraction mode only)
location_geo_code
string
No
Optional geo code for location-based search (e.g., “us”)
time_range
TimeRange
No
Optional time range filter for search results. Options: TimeRange.PAST_HOUR, TimeRange.PAST_24_HOURS, TimeRange.PAST_WEEK, TimeRange.PAST_MONTH, TimeRange.PAST_YEAR
Basic Schema Example
Define a simple schema for structured search results:
Copy
from pydantic import BaseModel, Fieldfrom typing import Listclass ProductInfo(BaseModel): name: str = Field(description="Product name") description: str = Field(description="Product description") price: str = Field(description="Product price") features: List[str] = Field(description="List of key features") availability: str = Field(description="Availability information")from scrapegraph_py.models import TimeRangeresponse = client.searchscraper( user_prompt="Find information about iPhone 15 Pro", output_schema=ProductInfo, location_geo_code="us", # Optional: Geo code for location-based search time_range=TimeRange.PAST_MONTH # Optional: Filter results by time range)print(f"Product: {response.name}")print(f"Price: {response.price}")print("\nFeatures:")for feature in response.features: print(f"- {feature}")
Advanced Schema Example
Define a complex schema for comprehensive market research:
Use markdown mode for cost-effective content gathering:
Copy
from scrapegraph_py import Clientclient = Client(api_key="your-api-key")from scrapegraph_py.models import TimeRange# Enable markdown mode for cost-effective content gatheringresponse = client.searchscraper( user_prompt="Latest developments in artificial intelligence", num_results=3, extraction_mode=False, # Enable markdown mode (2 credits per page vs 10 credits) location_geo_code="us", # Optional: Geo code for location-based search time_range=TimeRange.PAST_WEEK # Optional: Filter results by time range)# Access the raw markdown contentmarkdown_content = response['markdown_content']reference_urls = response['reference_urls']print(f"Markdown content length: {len(markdown_content)} characters")print(f"Reference URLs: {len(reference_urls)}")# Process the markdown contentprint("Content preview:", markdown_content[:500] + "...")# Save to file for analysiswith open('ai_research_content.md', 'w', encoding='utf-8') as f: f.write(markdown_content)print("Content saved to ai_research_content.md")
Markdown Mode Benefits:
Cost-effective: Only 2 credits per page (vs 10 credits for AI extraction)
Full content: Get complete page content in markdown format
Faster: No AI processing overhead
Perfect for: Content analysis, bulk data collection, building datasets
Time Range Filter Example
Filter search results by date range to get only recent information:
Copy
from scrapegraph_py import Clientfrom scrapegraph_py.models import TimeRangeclient = Client(api_key="your-api-key")# Search for recent news from the past weekresponse = client.searchscraper( user_prompt="Latest news about AI developments", num_results=5, time_range=TimeRange.PAST_WEEK # Options: PAST_HOUR, PAST_24_HOURS, PAST_WEEK, PAST_MONTH, PAST_YEAR)print("Recent AI news:", response['result'])print("Reference URLs:", response['reference_urls'])
Time Range Options:
TimeRange.PAST_HOUR - Results from the past hour
TimeRange.PAST_24_HOURS - Results from the past 24 hours
TimeRange.PAST_WEEK - Results from the past week
TimeRange.PAST_MONTH - Results from the past month