技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
headless-web-scraping【数据与研究】Extract data from web pages using the scrapling Python library — select the appropriate fetcher tier (HTTP, stealth Chromium, or full browser automation) based on target site defenses, configure headless browsing, and extract structured data with CSS selectors. Use when WebFetch is insufficient for JS-rendered pages, anti-bot-protected sites, or stru…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Extraer datos de páginas web que resisten solicitudes HTTP simples — contenido renderizado por JS, sitios protegidos por Cloudflare y SPAs dinámicos — usando la arquitectura de fetcher de tres niveles de scrapling y la extracción de datos basada en CSS.
WebFetch simple o requests.get() retorna respuestas vacías o bloqueadasDeterminar qué fetcher de scrapling coincide con las defensas del sitio objetivo.
# Decision matrix:
# 1. Fetcher — static HTML, no JS, no anti-bot (fastest)
# 2. StealthyFetcher — Cloudflare/Turnstile, TLS fingerprint checks
# 3. DynamicFetcher — JS-rendered SPAs, click/scroll interactions
# Quick probe: try Fetcher first, escalate on failure
from scrapling import Fetcher
fetcher = Fetcher()
response = fetcher.get("https://example.com/target-page")
if response.status == 200 and response.get_all_text():
print("Fetcher tier sufficient")
else:
print("Escalate to StealthyFetcher or DynamicFetcher")
| Señal | Tier Recomendado |
|---|---|
| HTML estático, sin protección | Fetcher |
| 403/503, página de challenge de Cloudflare | StealthyFetcher |
| La página carga pero el área de contenido está vacía | DynamicFetcher |
| Necesita hacer click en botones o scroll | DynamicFetcher |
| altcha CAPTCHA presente | Ninguno (no se puede automatizar) |
Esperado: Uno de los tres tiers es identificado. Para la mayoría de los sitios modernos, StealthyFetcher es el punto de partida correcto.
En caso de fallo: Si los tres tiers retornan respuestas bloqueadas, verificar si el sitio usa altcha CAPTCHA (challenge de proof-of-work que no se puede sortear). Si es así, documentar la limitación y proveer instrucciones de extracción manual en su lugar.
Configurar el fetcher seleccionado con las opciones apropiadas.
from scrapling import Fetcher, StealthyFetcher, DynamicFetcher
# Tier 1: Fast HTTP with TLS fingerprint impersonation
fetcher = Fetcher()
fetcher.configure(
timeout=30,
retries=3,
follow_redirects=True
)
# Tier 2: Headless Chromium with anti-detection
fetcher = StealthyFetcher()
fetcher.configure(
headless=True,
timeout=60,
network_idle=True # wait for all network requests to settle
)
# Tier 3: Full browser automation
fetcher = DynamicFetcher()
fetcher.configure(
headless=True,
timeout=90,
network_idle=True,
wait_selector="div.results" # wait for specific element before extracting
)
Esperado: La instancia del fetcher está configurada y lista. Sin errores en la instanciación. Para StealthyFetcher y DynamicFetcher, un binario de Chromium está disponible (scrapling lo gestiona automáticamente en la primera ejecución).
En caso de fallo:
playwright o binario de navegador no encontrado -- ejecutar python -m playwright install chromiumconfigure() -- aumentar el valor de timeout o verificar conectividad de redpip install scraplingNavegar a la URL objetivo y extraer datos estructurados usando selectores CSS.
# Fetch the page
response = fetcher.get("https://example.com/target-page")
# Single element extraction
title = response.find("h1.page-title")
if title:
print(title.get_all_text())
# Multiple elements
items = response.find_all("div.result-item")
for item in items:
name = item.find("span.name")
price = item.find("span.price")
print(f"{name.get_all_text()}: {price.get_all_text()}")
# Get attribute values
links = response.find_all("a.product-link")
urls = [link.get("href") for link in links]
# Get raw HTML content of an element
detail_html = response.find("div.description").html_content
Referencia clave de la API:
| Método | Propósito |
|---|---|
response.find("selector") | Primer elemento que coincide |
response.find_all("selector") | Todos los elementos que coinciden |
element.get("attr") | Valor del atributo (href, src, data-*) |
element.get_all_text() | Todo el contenido de texto, recursivamente |
element.html_content | HTML interno crudo |
Esperado: Los datos extraídos coinciden con el contenido visible de la página. Los elementos son no-None y el contenido de texto es no-vacío para páginas pobladas.
En caso de fallo:
find() retorna None -- inspeccionar el HTML real (response.html_content) para verificar el selector; la página puede usar nombres de clase diferentes a los esperadosget_all_text() -- el contenido puede estar dentro de shadow DOM o un iframe; probar DynamicFetcher con un wait_selector.css_first() -- esto no es parte de la API de scrapling (confusión común con otras librerías)Implementar lógica de fallback para detección de CAPTCHA, respuestas vacías y requisitos de sesión.
import time
def scrape_with_fallback(url, selector):
"""Try each fetcher tier in order, with CAPTCHA detection."""
tiers = [
("Fetcher", Fetcher),
("StealthyFetcher", StealthyFetcher),
("DynamicFetcher", DynamicFetcher),
]
for tier_name, tier_class in tiers:
fetcher = tier_class()
fetcher.configure(headless=True, timeout=60)
try:
response = fetcher.get(url)
except Exception as error:
print(f"{tier_name} failed: {error}")
continue
# Detect CAPTCHA / challenge pages
page_text = response.get_all_text().lower()
if "altcha" in page_text or "proof of work" in page_text:
print(f"altcha CAPTCHA detected -- cannot automate")
return None
if response.status == 403 or response.status == 503:
print(f"{tier_name} blocked (HTTP {response.status}), escalating")
continue
result = response.find(selector)
if result and result.get_all_text().strip():
return result.get_all_text()
print(f"{tier_name} returned empty content, escalating")
print("All tiers exhausted. Manual extraction required.")
return None
Esperado: La función retorna texto extraído en caso de éxito, o None con un mensaje diagnóstico cuando todos los tiers fallan. Las páginas CAPTCHA son detectadas y reportadas en lugar de reintentar indefinidamente.
En caso de fallo:
Implementar delays y respetar las políticas del sitio antes de ejecutar a escala.
import time
import urllib.robotparser
def check_robots_txt(base_url, target_path):
"""Check if scraping is allowed by robots.txt."""
rp = urllib.robotparser.RobotFileParser()
rp.set_url(f"{base_url}/robots.txt")
rp.read()
return rp.can_fetch("*", f"{base_url}{target_path}")
def scrape_urls(urls, selector, delay=1.0):
"""Scrape multiple URLs with rate limiting."""
results = []
fetcher = StealthyFetcher()
fetcher.configure(headless=True, timeout=60)
for url in urls:
response = fetcher.get(url)
data = response.find(selector)
if data:
results.append(data.get_all_text())
time.sleep(delay) # respect the server
return results
Lista de verificación de scraping ético:
robots.txt antes de hacer scraping -- respetar las directivas DisallowEsperado: El scraping corre a una tasa controlada. robots.txt se verifica antes de operaciones masivas. No se disparan respuestas 429.
En caso de fallo:
robots.txt no permite la ruta -- respetar la directiva; no anularlaconfigure() (no kwargs deprecados del constructor).find() / .find_all() (no .css_first() u otros métodos de librería)robots.txt se verifica antes de operaciones masivas.css_first() en lugar de .find(): scrapling usa .find() y .find_all() para selección de elementos -- .css_first() pertenece a una librería diferente y lanzará AttributeErrorFetcher primero, luego escalar -- DynamicFetcher es 10-50x más lento debido al arranque del navegador completoconfigure(): scrapling v0.4.x deprecó pasar opciones al constructor; siempre usar el método configure()