Funciones avanzadas de webscraping en C

This article was translated from English: Does it need improvement?
Translated
View the article in English

Función HttpIdentity

Algunos sistemas de sitios web requieren que el usuario inicie sesión para ver el contenido; en este caso, podemos usar un HttpIdentity. Aquí se explica cómo configurarlo:

// Create a new instance of HttpIdentity
HttpIdentity id = new HttpIdentity();

// Set the network username and password for authentication
id.NetworkUsername = "username";
id.NetworkPassword = "pwd";

// Add the identity to the collection of identities
Identities.Add(id);
// Create a new instance of HttpIdentity
HttpIdentity id = new HttpIdentity();

// Set the network username and password for authentication
id.NetworkUsername = "username";
id.NetworkPassword = "pwd";

// Add the identity to the collection of identities
Identities.Add(id);
' Create a new instance of HttpIdentity
Dim id As New HttpIdentity()

' Set the network username and password for authentication
id.NetworkUsername = "username"
id.NetworkPassword = "pwd"

' Add the identity to the collection of identities
Identities.Add(id)
$vbLabelText   $csharpLabel

Una de las características más impresionantes y poderosas en IronWebScraper es la capacidad de utilizar miles de credenciales de usuario únicas y/o motores de navegador para simular o extraer sitios web usando múltiples sesiones de inicio de sesión.

public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Define an array of proxies
    var proxies = "IP-Proxy1:8080,IP-Proxy2:8081".Split(',');

    // Iterate over common Chrome desktop user agents
    foreach (var UA in IronWebScraper.CommonUserAgents.ChromeDesktopUserAgents)
    {
        // Iterate over the proxies
        foreach (var proxy in proxies)
        {
            // Add a new HTTP identity with specific user agent and proxy
            Identities.Add(new HttpIdentity()
            {
                UserAgent = UA,
                UseCookies = true,
                Proxy = proxy
            });
        }
    }

    // Make an initial request to the website with a parse method
    this.Request("http://www.Website.com", Parse);
}
public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Define an array of proxies
    var proxies = "IP-Proxy1:8080,IP-Proxy2:8081".Split(',');

    // Iterate over common Chrome desktop user agents
    foreach (var UA in IronWebScraper.CommonUserAgents.ChromeDesktopUserAgents)
    {
        // Iterate over the proxies
        foreach (var proxy in proxies)
        {
            // Add a new HTTP identity with specific user agent and proxy
            Identities.Add(new HttpIdentity()
            {
                UserAgent = UA,
                UseCookies = true,
                Proxy = proxy
            });
        }
    }

    // Make an initial request to the website with a parse method
    this.Request("http://www.Website.com", Parse);
}
Public Overrides Sub Init()
	' Set the license key for IronWebScraper
	License.LicenseKey = "LicenseKey"

	' Set the logging level to capture all logs
	Me.LoggingLevel = WebScraper.LogLevel.All

	' Assign the working directory for the output files
	Me.WorkingDirectory = AppSetting.GetAppRoot() & "\ShoppingSiteSample\Output\"

	' Define an array of proxies
	Dim proxies = "IP-Proxy1:8080,IP-Proxy2:8081".Split(","c)

	' Iterate over common Chrome desktop user agents
	For Each UA In IronWebScraper.CommonUserAgents.ChromeDesktopUserAgents
		' Iterate over the proxies
		For Each proxy In proxies
			' Add a new HTTP identity with specific user agent and proxy
			Identities.Add(New HttpIdentity() With {
				.UserAgent = UA,
				.UseCookies = True,
				.Proxy = proxy
			})
		Next proxy
	Next UA

	' Make an initial request to the website with a parse method
	Me.Request("http://www.Website.com", Parse)
End Sub
$vbLabelText   $csharpLabel

Tienes múltiples propiedades para darte diferentes comportamientos, evitando así que los sitios web te bloqueen.

Algunas de estas propiedades incluyen:

  • NetworkDomain: El dominio de red a utilizar para la autenticación del usuario. Compatible con Windows, NTLM, Kerberos, Linux, BSD y Mac OS X. Debe utilizarse con NetworkUsername y NetworkPassword.
  • NetworkUsername: El nombre de usuario de red/http que se utilizará para la autenticación del usuario. Compatible con HTTP, redes de Windows, NTLM, Kerberos, redes de Linux, redes de BSD y Mac OS.
  • NetworkPassword: La contraseña de red/http que se utilizará para la autenticación del usuario. Compatible con HTTP, redes de Windows, NTLM, Kerberos, redes de Linux, redes de BSD y Mac OS.
  • Proxy: Para configurar los ajustes de proxy.
  • UserAgent: Para configurar un motor de navegador (por ejemplo, Chrome de escritorio, Chrome móvil, Chrome tablet, IE y Firefox, etc.).
  • HttpRequestHeaders: Para valores de cabecera personalizados que se utilizarán con esta identidad, acepta un objeto de diccionario Dictionary<string, string>.
  • UseCookies: Habilitar/deshabilitar el uso de cookies.

IronWebScraper ejecuta el scraper usando identidades aleatorias. Si necesitamos especificar el uso de una identidad específica para analizar una página, podemos hacerlo:

public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Create a new instance of HttpIdentity
    HttpIdentity identity = new HttpIdentity();

    // Set the network username and password for authentication
    identity.NetworkUsername = "username";
    identity.NetworkPassword = "pwd";

    // Add the identity to the collection of identities
    Identities.Add(identity);

    // Make a request to the website with the specified identity
    this.Request("http://www.Website.com", Parse, identity);
}
public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Create a new instance of HttpIdentity
    HttpIdentity identity = new HttpIdentity();

    // Set the network username and password for authentication
    identity.NetworkUsername = "username";
    identity.NetworkPassword = "pwd";

    // Add the identity to the collection of identities
    Identities.Add(identity);

    // Make a request to the website with the specified identity
    this.Request("http://www.Website.com", Parse, identity);
}
Public Overrides Sub Init()
	' Set the license key for IronWebScraper
	License.LicenseKey = "LicenseKey"

	' Set the logging level to capture all logs
	Me.LoggingLevel = WebScraper.LogLevel.All

	' Assign the working directory for the output files
	Me.WorkingDirectory = AppSetting.GetAppRoot() & "\ShoppingSiteSample\Output\"

	' Create a new instance of HttpIdentity
	Dim identity As New HttpIdentity()

	' Set the network username and password for authentication
	identity.NetworkUsername = "username"
	identity.NetworkPassword = "pwd"

	' Add the identity to the collection of identities
	Identities.Add(identity)

	' Make a request to the website with the specified identity
	Me.Request("http://www.Website.com", Parse, identity)
End Sub
$vbLabelText   $csharpLabel

Habilitar la función de caché web

Esta función almacena en caché las páginas solicitadas. Se utiliza a menudo en las fases de desarrollo y prueba, permitiendo a los desarrolladores almacenar en caché las páginas requeridas para reutilizarlas después de actualizar el código. Esto te permite ejecutar tu código en páginas almacenadas en caché después de reiniciar tu extractor web sin necesidad de conectarte al sitio web en vivo cada vez (repetición de acción).

Puedes usarlo en el método Init():

// Enable web cache without an expiration time
EnableWebCache();

// OR enable web cache with a specified expiration time
EnableWebCache(new TimeSpan(1, 30, 30));
// Enable web cache without an expiration time
EnableWebCache();

// OR enable web cache with a specified expiration time
EnableWebCache(new TimeSpan(1, 30, 30));
' Enable web cache without an expiration time
EnableWebCache()

' OR enable web cache with a specified expiration time
EnableWebCache(New TimeSpan(1, 30, 30))
$vbLabelText   $csharpLabel

Guardará tus datos almacenados en caché en la carpeta WebCache bajo la carpeta del directorio de trabajo.

public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Enable web cache with a specific expiration time of 1 hour, 30 minutes, and 30 seconds
    EnableWebCache(new TimeSpan(1, 30, 30));

    // Make an initial request to the website with a parse method
    this.Request("http://www.Website.com", Parse);
}
public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Enable web cache with a specific expiration time of 1 hour, 30 minutes, and 30 seconds
    EnableWebCache(new TimeSpan(1, 30, 30));

    // Make an initial request to the website with a parse method
    this.Request("http://www.Website.com", Parse);
}
Public Overrides Sub Init()
	' Set the license key for IronWebScraper
	License.LicenseKey = "LicenseKey"

	' Set the logging level to capture all logs
	Me.LoggingLevel = WebScraper.LogLevel.All

	' Assign the working directory for the output files
	Me.WorkingDirectory = AppSetting.GetAppRoot() & "\ShoppingSiteSample\Output\"

	' Enable web cache with a specific expiration time of 1 hour, 30 minutes, and 30 seconds
	EnableWebCache(New TimeSpan(1, 30, 30))

	' Make an initial request to the website with a parse method
	Me.Request("http://www.Website.com", Parse)
End Sub
$vbLabelText   $csharpLabel

IronWebScraper también tiene funciones para permitir que tu motor continúe extrayendo después de reiniciar el código estableciendo el nombre del proceso de inicio del motor usando Start(CrawlID).

static void Main(string[] args)
{
    // Create an object from the Scraper class
    EngineScraper scrape = new EngineScraper();

    // Start the scraping process with the specified crawl ID
    scrape.Start("enginestate");
}
static void Main(string[] args)
{
    // Create an object from the Scraper class
    EngineScraper scrape = new EngineScraper();

    // Start the scraping process with the specified crawl ID
    scrape.Start("enginestate");
}
Shared Sub Main(ByVal args() As String)
	' Create an object from the Scraper class
	Dim scrape As New EngineScraper()

	' Start the scraping process with the specified crawl ID
	scrape.Start("enginestate")
End Sub
$vbLabelText   $csharpLabel

La solicitud y respuesta de ejecución se guardarán en la carpeta SavedState dentro del directorio de trabajo.

Estrangulamiento

Podemos controlar el número mínimo y máximo de conexiones y la velocidad de conexión por dominio.

public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Set the total number of allowed open HTTP requests (threads)
    this.MaxHttpConnectionLimit = 80;

    // Set minimum polite delay (pause) between requests to a given domain or IP address
    this.RateLimitPerHost = TimeSpan.FromMilliseconds(50);

    // Set the allowed number of concurrent HTTP requests (threads) per hostname or IP address
    this.OpenConnectionLimitPerHost = 25;

    // Do not obey the robots.txt files
    this.ObeyRobotsDotTxt = false;

    // Makes the WebScraper intelligently throttle requests not only by hostname, but also by host servers' IP addresses
    this.ThrottleMode = Throttle.ByDomainHostName;

    // Make an initial request to the website with a parse method
    this.Request("https://www.Website.com", Parse);
}
public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Set the total number of allowed open HTTP requests (threads)
    this.MaxHttpConnectionLimit = 80;

    // Set minimum polite delay (pause) between requests to a given domain or IP address
    this.RateLimitPerHost = TimeSpan.FromMilliseconds(50);

    // Set the allowed number of concurrent HTTP requests (threads) per hostname or IP address
    this.OpenConnectionLimitPerHost = 25;

    // Do not obey the robots.txt files
    this.ObeyRobotsDotTxt = false;

    // Makes the WebScraper intelligently throttle requests not only by hostname, but also by host servers' IP addresses
    this.ThrottleMode = Throttle.ByDomainHostName;

    // Make an initial request to the website with a parse method
    this.Request("https://www.Website.com", Parse);
}
Public Overrides Sub Init()
	' Set the license key for IronWebScraper
	License.LicenseKey = "LicenseKey"

	' Set the logging level to capture all logs
	Me.LoggingLevel = WebScraper.LogLevel.All

	' Assign the working directory for the output files
	Me.WorkingDirectory = AppSetting.GetAppRoot() & "\ShoppingSiteSample\Output\"

	' Set the total number of allowed open HTTP requests (threads)
	Me.MaxHttpConnectionLimit = 80

	' Set minimum polite delay (pause) between requests to a given domain or IP address
	Me.RateLimitPerHost = TimeSpan.FromMilliseconds(50)

	' Set the allowed number of concurrent HTTP requests (threads) per hostname or IP address
	Me.OpenConnectionLimitPerHost = 25

	' Do not obey the robots.txt files
	Me.ObeyRobotsDotTxt = False

	' Makes the WebScraper intelligently throttle requests not only by hostname, but also by host servers' IP addresses
	Me.ThrottleMode = Throttle.ByDomainHostName

	' Make an initial request to the website with a parse method
	Me.Request("https://www.Website.com", Parse)
End Sub
$vbLabelText   $csharpLabel

Propiedades de estrangulamiento

  • MaxHttpConnectionLimit Número total de solicitudes HTTP abiertas permitidas (hilos)
  • RateLimitPerHost Retraso o pausa educado mínimo (en milisegundos) entre solicitudes a un dominio o dirección IP dado
  • OpenConnectionLimitPerHost Número permitido de solicitudes HTTP concurrentes (hilos) por nombre de host
  • ThrottleMode Hace que el WebScraper limite inteligentemente las solicitudes no solo por nombre de host, sino también por direcciones IP de los servidores de host. Esto es educado en caso de que múltiples dominios extraídos estén alojados en la misma máquina.

Comience a usar IronWebscraper

Comience a usar IronWebScraper en su proyecto hoy con una prueba gratuita.

Primer Paso:
green arrow pointer

Preguntas Frecuentes

¿Cómo puedo autenticar usuarios en sitios web que requieren inicio de sesión en C#?

Puede utilizar la función HttpIdentity en IronWebScraper para autenticar usuarios configurando propiedades como NetworkDomain, NetworkUsername y NetworkPassword.

¿Cuál es el beneficio de usar caché web durante el desarrollo?

La función de caché web le permite almacenar en caché las páginas solicitadas para reutilizarlas, lo que ayuda a ahorrar tiempo y recursos al evitar conexiones repetidas a sitios web en vivo, especialmente útil durante las fases de desarrollo y prueba.

¿Cómo puedo gestionar múltiples sesiones de inicio de sesión en web scraping?

IronWebScraper permite el uso de miles de credenciales de usuario únicas y motores de navegador para simular múltiples sesiones de inicio de sesión, lo que ayuda a evitar que los sitios web detecten y bloqueen el scraper.

¿Cuáles son las opciones avanzadas de limitación en web scraping?

IronWebScraper ofrece una configuración ThrottleMode que gestiona inteligentemente la limitación de solicitudes según los nombres de host y las direcciones IP, asegurando una interacción respetuosa con los entornos de alojamiento compartido.

¿Cómo puedo usar un proxy con IronWebScraper?

Para usar un proxy, defina un array de proxies y asócielos con instancias de HttpIdentity en IronWebScraper, permitiendo que las solicitudes se dirijan a través de diferentes direcciones IP para el anonimato y el control de acceso.

¿Cómo maneja IronWebScraper los retrasos en las solicitudes para evitar la sobrecarga del servidor?

La configuración RateLimitPerHost en IronWebScraper especifica el retraso mínimo entre solicitudes a un dominio o dirección IP específicos, ayudando a prevenir la sobrecarga del servidor al espaciar las solicitudes.

¿Se puede reanudar el web scraping después de una interrupción?

Sí, IronWebScraper puede reanudar el scraping después de una interrupción utilizando el método Start(CrawlID), que guarda el estado de ejecución y reanuda desde el último punto guardado.

¿Cómo controlo el número de conexiones HTTP concurrentes en un web scraper?

En IronWebScraper, puede configurar la propiedad MaxHttpConnectionLimit para controlar el número total de solicitudes HTTP abiertas permitidas, ayudando a gestionar la carga y los recursos del servidor.

¿Qué opciones están disponibles para registrar actividades de web scraping?

IronWebScraper le permite establecer el nivel de registro utilizando la propiedad LoggingLevel, lo que permite un registro exhaustivo para un análisis detallado y solución de problemas durante las operaciones de scraping.

Darrius Serrant
Ingeniero de Software Full Stack (WebOps)

Darrius Serrant tiene una licenciatura en Ciencias de la Computación de la Universidad de Miami y trabaja como Ingeniero de Marketing WebOps Full Stack en Iron Software. Atraído por la programación desde joven, vio la computación como algo misterioso y accesible, convirtiéndolo en el ...

Leer más
¿Listo para empezar?
Nuget Descargas 125,527 | Version: 2025.11 recién lanzado