Cómo crear tu propio bot web básico (ejemplo en C#)

Un bot web es un programa que automatiza tareas HTTP (scraping, monitoring, API, posting). Ejemplo mínimo en C# (.NET 6+):

Código básico

using System;
using System.Net.Http;
using System.Threading.Tasks;

class MiBot {
    static readonly HttpClient client = new HttpClient();
    static async Task Main() {
        // 1. URL objetivo
        var url = "https://httpbin.org/get"; // prueba
        
        // 2. Petición GET
        var response = await client.GetAsync(url);
        response.EnsureSuccessStatusCode();
        
        // 3. Leer respuesta
        string html = await response.Content.ReadAsStringAsync();
        Console.WriteLine($"Status: {response.StatusCode}");
        Console.WriteLine($"Longitud: {html.Length} chars");
        
        // 4. Procesa 'html' (HtmlAgilityPack, Regex, JSON, etc.)
    }
}

Pasos para crear el tuyo

  1. Instala .NET SDK.
  2. dotnet new console -n MiBot → cd MiBot.
  3. Edita Program.cs con tu lógica.
  4. dotnet run para probar.

Buenas prácticas

  • Respeta robots.txt y Rate-Limit (pausa entre peticiones: await Task.Delay(1000)).
  • Usa User-Agent identificativo: client.DefaultRequestHeaders.UserAgent.ParseAdd("MiBot/1.0 +https://misitio.com/bot").
  • Maneja errores (reintentos exponenciales, Polly).
  • Para JS renderizado: Puppeteer/Playwright/Selenium (headless browser).
  • Despliega como servicio (systemd, Docker, Azure Functions, AWS Lambda).
Legal/Ético: Algunos sitios prohíben bots (ToS). Lee robots.txt y términos. Exceso de peticiones = ban IP / acciones legales. Usa APIs oficiales si existen.