Spaces:
Running
Running
Rohan P H commited on
Commit ·
acac548
1
Parent(s): 62fe812
fix: replace arun_many with asyncio.gather+semaphore for true parallel crawling
Browse files- src/crawler.py +13 -14
src/crawler.py
CHANGED
|
@@ -7,7 +7,7 @@ from urllib.parse import urljoin, urlparse
|
|
| 7 |
import httpx
|
| 8 |
import polars as pl
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
-
from crawl4ai import AsyncWebCrawler, BrowserConfig, CacheMode, CrawlerRunConfig
|
| 11 |
|
| 12 |
from src.config import Config
|
| 13 |
from src.utils import content_hash, normalize_url
|
|
@@ -149,14 +149,7 @@ class Crawler:
|
|
| 149 |
remove_overlay_elements=True,
|
| 150 |
remove_consent_popups=True,
|
| 151 |
)
|
| 152 |
-
|
| 153 |
-
semaphore_count=4,
|
| 154 |
-
rate_limiter=RateLimiter(
|
| 155 |
-
base_delay=(1.0, 2.5),
|
| 156 |
-
max_delay=30.0,
|
| 157 |
-
max_retries=2,
|
| 158 |
-
),
|
| 159 |
-
)
|
| 160 |
|
| 161 |
async with httpx.AsyncClient(
|
| 162 |
timeout=30.0,
|
|
@@ -204,12 +197,18 @@ class Crawler:
|
|
| 204 |
next_layer.append(link)
|
| 205 |
|
| 206 |
if html_urls:
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
for result in html_results:
|
|
|
|
|
|
|
| 213 |
page = self._parse_arun_result(result, current_depth)
|
| 214 |
if page:
|
| 215 |
links = page.pop("_links", [])
|
|
|
|
| 7 |
import httpx
|
| 8 |
import polars as pl
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
+
from crawl4ai import AsyncWebCrawler, BrowserConfig, CacheMode, CrawlerRunConfig
|
| 11 |
|
| 12 |
from src.config import Config
|
| 13 |
from src.utils import content_hash, normalize_url
|
|
|
|
| 149 |
remove_overlay_elements=True,
|
| 150 |
remove_consent_popups=True,
|
| 151 |
)
|
| 152 |
+
html_semaphore = asyncio.Semaphore(4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
async with httpx.AsyncClient(
|
| 155 |
timeout=30.0,
|
|
|
|
| 197 |
next_layer.append(link)
|
| 198 |
|
| 199 |
if html_urls:
|
| 200 |
+
async def _fetch_one(c, url, cfg, sem):
|
| 201 |
+
async with sem:
|
| 202 |
+
return await c.arun(url=url, config=cfg)
|
| 203 |
+
|
| 204 |
+
html_coros = [
|
| 205 |
+
_fetch_one(crawler, u, crawl_config, html_semaphore)
|
| 206 |
+
for u in html_urls
|
| 207 |
+
]
|
| 208 |
+
html_results = await asyncio.gather(*html_coros, return_exceptions=True)
|
| 209 |
for result in html_results:
|
| 210 |
+
if isinstance(result, Exception):
|
| 211 |
+
continue
|
| 212 |
page = self._parse_arun_result(result, current_depth)
|
| 213 |
if page:
|
| 214 |
links = page.pop("_links", [])
|