Erweiterte Web Scraping-Funktionen in C# mit IronWebScraper
HttpIdentity-Funktion
Einige Webseitensysteme erfordern, dass der Benutzer angemeldet ist, um den Inhalt anzuzeigen; in diesem Fall können wir eine HttpIdentity verwenden. So können Sie es einrichten:
// 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)
Eine der beeindruckendsten und leistungsstärksten Funktionen in IronWebScraper ist die Fähigkeit, Tausende einzigartiger Benutzeranmeldeinformationen und/oder Browser-Engines zu verwenden, um unter Verwendung mehrerer Anmeldesitzungen Websites zu täuschen oder zu scrapen.
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
Sie haben mehrere Eigenschaften, die Ihnen unterschiedliche Verhaltensweisen bieten, um zu verhindern, dass Websites Sie blockieren.
Einige dieser Eigenschaften umfassen:
NetworkDomain: Die Netzwerkdomain, die für die Benutzerauthentifizierung verwendet werden soll. Unterstützt Windows-, NTLM-, Kerberos-, Linux-, BSD- und Mac OS X-Netzwerke. Muss mitNetworkUsernameundNetworkPasswordverwendet werden.NetworkUsername: Der Netzwerk-/HTTP-Benutzername, der für die Benutzerauthentifizierung verwendet werden soll. Unterstützt HTTP-, Windows-Netzwerke, NTLM-, Kerberos-, Linux-, BSD- und Mac OS-Netzwerke.NetworkPassword: Das Netzwerk-/HTTP-Passwort, das für die Benutzerauthentifizierung verwendet werden soll. Unterstützt HTTP-, Windows-Netzwerke, NTLM-, Kerberos-, Linux-, BSD- und Mac OS-Netzwerke.Proxy: Zum Einstellen der Proxy-Einstellungen.UserAgent: Zum Einstellen einer Browser-Engine (z. B. Chrome Desktop, Chrome Mobile, Chrome Tablet, IE und Firefox, etc.).HttpRequestHeaders: Für benutzerdefinierte Header-Werte, die mit dieser Identität verwendet werden sollen, wird ein WörterbuchobjektDictionary<string, string>akzeptiert.UseCookies: Aktivieren/Deaktivieren der Verwendung von Cookies.
IronWebScraper führt den Scraper unter Verwendung zufälliger Identitäten aus. Wenn wir die Verwendung einer bestimmten Identität zum Parsen einer Seite angeben müssen, können wir dies tun:
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
Aktivieren der Web-Cache-Funktion
- Diese Funktion cachet angeforderte Seiten. Sie wird häufig in den Entwicklungs- und Testphasen verwendet, um Entwicklern zu ermöglichen, erforderliche Seiten für die Wiederverwendung nach der Aktualisierung des Codes zwischenzuspeichern. Das ermöglicht es Ihnen, Ihren Code auf zwischengespeicherten Seiten auszuführen, nachdem Sie Ihren Webscraper neu gestartet haben, ohne jedes Mal die Verbindung zur Live-Website herstellen zu müssen (Aktions-Wiedergabe).
Sie können es in der Init()-Methode verwenden:
// 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))
Die zwischengespeicherten Daten werden im Ordner WebCache unter dem Arbeitsverzeichnis gespeichert.
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
IronWebScraper hat auch Funktionen, um Ihre Engine nach dem Neustart des Codes fortsetzen zu lassen, indem der Startprozessname der Engine mit Start(CrawlID) festgelegt wird.
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
Die Ausführungsanforderung und die Antwort werden im Ordner SavedState innerhalb des Arbeitsverzeichnisses gespeichert.
Drosselung
Wir können die minimalen und maximalen Verbindungszahlen und die Verbindungsgeschwindigkeit pro Domain steuern.
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
Drosselungseigenschaften
MaxHttpConnectionLimitGesamtanzahl erlaubter offener HTTP-Anfragen (Threads)RateLimitPerHostMinimale höfliche Verzögerung oder Pause (in Millisekunden) zwischen Anfragen an eine bestimmte Domain oder IP-AdresseOpenConnectionLimitPerHostErlaubte Anzahl gleichzeitiger HTTP-Anfragen (Threads) pro HostnameThrottleModeLässt den WebScraper intelligent Anfragen drosseln, nicht nur nach Hostname, sondern auch nach den IP-Adressen der Hostserver. Das ist höflich, falls mehrere gescrapte Domains auf derselben Maschine gehostet werden.
Legen Sie los mit IronWebscraper
Nutzen Sie IronWebScraper heute kostenlos in Ihrem Projekt.
Häufig gestellte Fragen
Wie kann ich Benutzer auf Websites authentifizieren, die einen Login erfordern, in C#?
Sie können die HttpIdentity-Funktion in IronWebScraper nutzen, um Benutzer zu authentifizieren, indem Sie Eigenschaften wie NetworkDomain, NetworkUsername und NetworkPassword einrichten.
Was ist der Vorteil der Verwendung von Web-Cache während der Entwicklung?
Die Web-Cache-Funktion ermöglicht das Zwischenspeichern von angeforderten Seiten zur Wiederverwendung, was hilft, Zeit und Ressourcen zu sparen, indem wiederholte Verbindungen zu Live-Websites vermieden werden, besonders nützlich während der Entwicklungs- und Testphasen.
Wie kann ich mehrere Login-Sitzungen im Web-Scraping verwalten?
IronWebScraper ermöglicht die Nutzung von Tausenden einzigartiger Benutzeranmeldedaten und Browser-Engines, um mehrere Login-Sitzungen zu simulieren, was hilft, zu verhindern, dass Websites den Scraper erkennen und blockieren.
Was sind die erweiterten Drosselungsoptionen im Web-Scraping?
IronWebScraper bietet eine ThrottleMode-Einstellung, die intelligent die Drosselung von Anfragen basierend auf Hostnamen und IP-Adressen verwaltet, um eine respektvolle Interaktion mit gemeinsam genutzten Hosting-Umgebungen zu gewährleisten.
Wie kann ich einen Proxy mit IronWebScraper verwenden?
Um einen Proxy zu verwenden, definieren Sie ein Array von Proxys und verknüpfen Sie sie mit HttpIdentity-Instanzen in IronWebScraper, damit Anfragen über verschiedene IP-Adressen für Anonymität und Zugriffskontrolle geleitet werden können.
Wie geht IronWebScraper mit Anfragedelays um, um eine Serverüberlastung zu verhindern?
Die RateLimitPerHost-Einstellung in IronWebScraper gibt die Mindestverzögerung zwischen Anfragen an eine bestimmte Domain oder IP-Adresse an, um eine Serverüberlastung zu verhindern, indem Anfragen zeitlich gestaffelt werden.
Kann Web-Scraping nach einer Unterbrechung fortgesetzt werden?
Ja, IronWebScraper kann das Scraping nach einer Unterbrechung fortsetzen, indem die Start(CrawlID)-Methode verwendet wird, die den Ausführungszustand speichert und vom letzten gespeicherten Punkt fortsetzt.
Wie kontrolliere ich die Anzahl gleichzeitiger HTTP-Verbindungen in einem Web-Scraper?
In IronWebScraper können Sie die MaxHttpConnectionLimit-Eigenschaft festlegen, um die Gesamtzahl zulässiger offener HTTP-Anfragen zu kontrollieren, was hilft, Serverlast und Ressourcen zu verwalten.
Welche Optionen sind für die Protokollierung von Web-Scraping-Aktivitäten verfügbar?
IronWebScraper ermöglicht es Ihnen, das Protokollierungsniveau mit der LoggingLevel-Eigenschaft festzulegen, um umfassende Protokollierung für detaillierte Analyse und Fehlersuche während der Scraping-Vorgänge zu aktivieren.

