MCPcopy Create free account
hub / github.com/ScrapeGraphAI/scrapecraft / search_urls

Method search_urls

backend/app/services/scrapegraph.py:79–163  ·  view source on GitHub ↗

Search for URLs using ScrapeGraphAI SearchScraper.

(self, query: str, max_results: int = 5)

Source from the content-addressed store, hash-verified

77
78
79 async def search_urls(self, query: str, max_results: int = 5) -> List[Dict[str, str]]:
80 """Search for URLs using ScrapeGraphAI SearchScraper."""
81 try:
82 async with AsyncClient(api_key=self.api_key) as client:
83 # Define schema for structured output
84 from pydantic import BaseModel, Field
85 from typing import List
86
87 class WebsiteInfo(BaseModel):
88 name: str = Field(description="Name of the website")
89 url: str = Field(description="Full URL of the website")
90 description: str = Field(description="Brief description of what the website offers")
91
92 class SearchResults(BaseModel):
93 websites: List[WebsiteInfo] = Field(description="List of relevant websites")
94
95 # Use searchscraper to find information about the topic
96 search_prompt = f"Find the top {max_results} most relevant websites and their URLs for: {query}. Return a list of websites with their URLs and descriptions."
97
98 try:
99 # Use searchscraper to find URLs - don't use schema for now
100 result = await client.searchscraper(
101 user_prompt=search_prompt
102 )
103
104 # Log the result to understand its structure
105 logger.info(f"SearchScraper raw result type: {type(result)}")
106 logger.info(f"SearchScraper raw result: {result}")
107
108 urls = []
109
110 # Handle the response - it's a dict when using searchscraper
111 if isinstance(result, dict):
112 # Extract from the result field
113 if 'result' in result and isinstance(result['result'], dict):
114 websites_data = result['result'].get('websites', [])
115
116 for website in websites_data:
117 if isinstance(website, dict) and 'url' in website:
118 urls.append({
119 'url': website['url'],
120 'description': website.get('description', website.get('name', f'Found via search for: {query}'))
121 })
122
123 # Also add reference URLs if they're different
124 if 'reference_urls' in result and result['reference_urls']:
125 for ref_url in result['reference_urls']:
126 if not any(u['url'] == ref_url for u in urls):
127 urls.append({
128 'url': ref_url,
129 'description': f'Reference source for: {query}'
130 })
131
132 logger.info(f"Final URLs extracted: {urls}")
133 return urls
134
135 except AttributeError as e:
136 # If searchscraper doesn't exist, try alternative approach

Callers 4

search_urlsFunction · 0.45
collect_urlsMethod · 0.45
_handle_add_urlsMethod · 0.45
_handle_url_searchMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected