Proxy-Konfiguration in Puppeteer: Authentifizierungs-, Rotations- und Bypass-Techniken
Erfahren Sie, wie Sie Proxys in Puppeteer konfigurieren, um die Automatisierung zu verbessern, die Authentifizierung zu verwalten und Blockierungen effektiv zu vermeiden.

Proxys sind unerlässlich für Puppenspieler Automatisierung. Sie verbergen Ihre IP, umgehen geografische Beschränkungen und helfen, Sperren zu vermeiden. Hier erfahren Sie mehr über die Verwendung von Proxys in Puppenspieler:
- Proxys einrichten: Fügen Sie Ihrem Browser Proxys hinzu und authentifizieren Sie sie problemlos.
- Proxys rotieren: Verteilen Sie Anfragen auf mehrere IPs, um eine Erkennung zu vermeiden.
- Behandeln Sie Fehler: Wiederholen Sie Anfragen, verwalten Sie Blöcke und wechseln Sie bei Bedarf den Proxy.
- Geschwindigkeit optimieren: Blockieren Sie unnötige Ressourcen und wechseln Sie Benutzeragenten für schnelleres, heimlicheres Scraping.
Schneller Vergleich der Proxy-Typen
| Proxy-Typ | Schnelligkeit | Anonymität | Kosten | Geeignet für |
|---|---|---|---|---|
| Datacenter | Schnell | Niedrig | Kostengünstig | Großvolumiges Schaben |
| Wohnbereich | Moderat | Hoch | Teuer | Sensible oder geospezifische Aufgaben |
| Mobil | langsam | Sehr hoch | Sehr teuer | Anzeigenüberprüfung, CAPTCHAs |
Puppenspieler Proxy-Integration mit Helle Daten - Vollständiges Tutorial 2024
Grundlegende Proxy-Einrichtung
Die Einrichtung eines Proxys ist ein wichtiger Schritt zur Optimierung Ihrer Puppeteer-Automatisierung. So können Sie loslegen.
Hinzufügen eines Proxy-Servers
Sie können einen Proxyserver während des Browserstarts in Puppeteer wie folgt konfigurieren:
<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
<span class="hljs-attr">args</span>: [<span class="hljs-string">'--proxy-server=http://157.230.255.230:8118'</span>]
});
<span class="hljs-keyword">const</span> page = <span class="hljs-keyword">await</span> browser.<span class="hljs-title function_">newPage</span>();
Es ist auch wichtig, Fehler bei der Navigation mit einem Proxy zu behandeln:
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">goto</span>(<span class="hljs-string">'https://books.toscrape.com/'</span>, {
<span class="hljs-attr">timeout</span>: <span class="hljs-number">70000</span>,
<span class="hljs-attr">waitUntil</span>: <span class="hljs-string">'networkidle2'</span>
});
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(err);
}
Als Nächstes möchten Sie Ihre Verbindung durch die Einrichtung einer Proxy-Authentifizierung sichern.
Schritte zur Proxy-Authentifizierung
Wenn Ihr Proxy eine Authentifizierung erfordert, können Sie die folgende Methode verwenden:
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">authenticate</span>({
<span class="hljs-attr">username</span>: <span class="hljs-string">'your_proxy_username'</span>,
<span class="hljs-attr">password</span>: <span class="hljs-string">'your_proxy_password'</span>
});
Hier ist ein kurzer Vergleich verschiedener Authentifizierungsmethoden:
| Authentifizierungsmethode | Bester Anwendungsfall | Implementierungskomplexität |
|---|---|---|
| page.authenticate() | Standard-Proxy-Authentifizierung | Niedrig |
| Proxy-Chain-Paket | Fortgeschrittenes Routing | Medium |
| Proxy-Authorization-Header | Nur-HTTP-Sites | Hoch |
Nachdem Sie Ihren Proxy und die Authentifizierung eingerichtet haben, ist es Zeit, sie zu testen.
Testen der Proxy-Einstellungen
Stellen Sie sicher, dass Ihr Proxy funktioniert, indem Sie die folgenden Schritte ausführen:
- Testen Sie die Verbindung mit einer Beispiel-URL.
- Bestätigen Sie, dass die IP des Proxys verwendet wird.
- Überwachen Sie die Leistung des Setups.
Hier ist ein Beispielskript zum Testen Ihres Proxys:
<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">testProxy</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
<span class="hljs-attr">args</span>: [<span class="hljs-string">'--proxy-server=http://157.230.255.230:8118'</span>]
});
<span class="hljs-keyword">const</span> page = <span class="hljs-keyword">await</span> browser.<span class="hljs-title function_">newPage</span>();
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">goto</span>(<span class="hljs-string">'https://books.toscrape.com/'</span>, {
<span class="hljs-attr">timeout</span>: <span class="hljs-number">70000</span>,
<span class="hljs-attr">waitUntil</span>: <span class="hljs-string">'networkidle2'</span>
});
<span class="hljs-keyword">const</span> content = <span class="hljs-keyword">await</span> page.<span class="hljs-title function_">content</span>();
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Proxy connection successful'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Proxy connection failed:'</span>, err);
}
<span class="hljs-keyword">await</span> browser.<span class="hljs-title function_">close</span>();
}
Durch Tests wird sichergestellt, dass Ihre Proxy-Konfiguration für die Verwendung in Ihren Automatisierungsaufgaben bereit ist.
Proxy-Rotationsmethoden
Die Verwaltung der Proxy-Rotation ist wichtig, um IP-Sperren zu verhindern und Ratenbegrenzungen beim Scraping oder Automatisieren von Online-Aufgaben zu vermeiden.
Erstellen einer Proxy-Liste
Erstellen Sie zunächst eine Liste mit Proxys, die effizient rotiert werden können. Hier ist ein Beispiel für die Einrichtung und Verwendung eines Proxy-Pools:
<span class="hljs-keyword">const</span> proxyList = [
{
<span class="hljs-attr">host</span>: <span class="hljs-string">'157.230.255.230'</span>,
<span class="hljs-attr">port</span>: <span class="hljs-string">'8118'</span>,
<span class="hljs-attr">username</span>: <span class="hljs-string">'user1'</span>,
<span class="hljs-attr">password</span>: <span class="hljs-string">'pass1'</span>
},
<span class="hljs-comment">// Add more proxy configurations</span>
];
<span class="hljs-keyword">function</span> <span class="hljs-title function_">getRandomProxy</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">return</span> proxyList[<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">random</span>() * proxyList.<span class="hljs-property">length</span>)];
}
Beim Einrichten Ihrer Proxyliste ist es wichtig, die verschiedenen Proxytypen und ihre Vor- und Nachteile zu verstehen:
| Proxy-Typ | Reaktionszeit | Erfolgsrate | Kostenbereich |
|---|---|---|---|
| Datacenter | 100-300ms | 85-95 % | 0.50–2 $/IP |
| Wohnbereich | 500-1000ms | 90-98 % | 2–8 $/IP |
| Mobil | 800-1500ms | 95-99 % | 5–15 $/IP |
Jeder Typ hat seine Stärken und Schwächen. Treffen Sie Ihre Wahl also auf der Grundlage Ihrer spezifischen Anforderungen, beispielsweise Geschwindigkeit, Zuverlässigkeit oder Budget.
Wechseln von Proxys zwischen Anfragen
Um Proxys automatisch zu rotieren, können Sie das folgende Setup verwenden:
<span class="hljs-keyword">const</span> <span class="hljs-title class_">ProxyChain</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'proxy-chain'</span>);
<span class="hljs-keyword">const</span> server = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ProxyChain</span>.<span class="hljs-title class_">Server</span>({
<span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
<span class="hljs-attr">prepareRequestFunction</span>: <span class="hljs-function">(<span class="hljs-params">{ request }</span>) =></span> {
<span class="hljs-keyword">const</span> proxy = <span class="hljs-title function_">getRandomProxy</span>();
<span class="hljs-keyword">return</span> {
<span class="hljs-attr">upstreamProxyUrl</span>: <span class="hljs-string">`http://<span class="hljs-subst">${proxy.username}</span>:<span class="hljs-subst">${proxy.password}</span>@<span class="hljs-subst">${proxy.host}</span>:<span class="hljs-subst">${proxy.port}</span>`</span>
};
}
});
<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
<span class="hljs-attr">args</span>: [<span class="hljs-string">'--proxy-server=http://localhost:8080'</span>]
});
Dieser Ansatz stellt sicher, dass jede Anfrage über einen anderen Proxy geleitet wird, ohne den Browser neu zu starten, was den Prozess effizienter macht.
Richtlinien zur Proxy-Rotation
Hier sind einige wichtige Vorgehensweisen zur Verbesserung Ihrer Proxy-Rotationsstrategie:
1. Timing-Strategie
Führen Sie zufällige Verzögerungen zwischen den Anfragen ein, um ein natürliches Browsing-Verhalten zu simulieren:
<span class="hljs-keyword">function</span> <span class="hljs-title function_">getRotationDelay</span>(<span class="hljs-params"></span>) {
<span class="hljs-comment">// Random delay between 2-5 seconds</span>
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">random</span>() * <span class="hljs-number">3000</span>) + <span class="hljs-number">2000</span>;
}
<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">rotateProxy</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">await</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function"><span class="hljs-params">resolve</span> =></span> <span class="hljs-built_in">setTimeout</span>(resolve, <span class="hljs-title function_">getRotationDelay</span>()));
<span class="hljs-keyword">const</span> proxy = <span class="hljs-title function_">getRandomProxy</span>();
<span class="hljs-comment">// Apply new proxy configuration</span>
}
2. Fehlerbehandlung
Wenn ein Proxy ausfällt, beheben Sie den Fehler ordnungsgemäß, indem Sie zu einem neuen wechseln und die Anforderung wiederholen:
<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">handleProxyError</span>(<span class="hljs-params">error, page</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Proxy error: <span class="hljs-subst">${error.message}</span>`</span>);
<span class="hljs-keyword">await</span> <span class="hljs-title function_">rotateProxy</span>();
<span class="hljs-keyword">return</span> page.<span class="hljs-title function_">reload</span>({ <span class="hljs-attr">waitUntil</span>: <span class="hljs-string">'networkidle2'</span> });
}
3. Sitzungsverwaltung
Verwenden Sie für Aufgaben, die Sitzungskonsistenz erfordern, denselben Proxy für verwandte Anforderungen:
<span class="hljs-keyword">const</span> sessionMap = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>();
<span class="hljs-keyword">function</span> <span class="hljs-title function_">getProxyForSession</span>(<span class="hljs-params">sessionId</span>) {
<span class="hljs-keyword">if</span> (!sessionMap.<span class="hljs-title function_">has</span>(sessionId)) {
sessionMap.<span class="hljs-title function_">set</span>(sessionId, <span class="hljs-title function_">getRandomProxy</span>());
}
<span class="hljs-keyword">return</span> sessionMap.<span class="hljs-title function_">get</span>(sessionId);
}
Mit diesen Strategien können Sie Proxys effizient verwalten und sich auf anspruchsvollere Herausforderungen wie die Handhabung von Blöcken, CAPTCHAs und anderen Hindernissen vorbereiten.
sbb-itb-23997f1
Erweiterte Proxy-Techniken
Privat- oder Rechenzentrums-Proxys
Die Auswahl des richtigen Proxys ist entscheidend für die Arbeit mit Puppeteer-Automatisierung. Hier ist ein Vergleich, der Ihnen die Entscheidung erleichtert:
| Merkmal | Wohnimmobilien | Proxies für Rechenzentren |
|---|---|---|
| Quelle | Von ISPs für Privathaushalte bereitgestellt | Aus Rechenzentren bezogen |
| Schnelligkeit | Langsamer aufgrund des ISP-Routings | Schneller mit dedizierter Infrastruktur |
| Erkennungsrisiko | Niedriger - simuliert echten Benutzerverkehr | Höher - leichter zu erkennen |
| Kosten | Teurer | Kostengünstig |
| Geografische Abdeckung | Breit gefächert und standortübergreifend | Oft begrenzt |
| Beste Anwendungsfälle | Aufgaben mit hoher Anonymität, sensibles Scraping, Anzeigenüberprüfung | Umfangreiches Data Mining, Stapelverarbeitung |
Residential Proxies sind eine gute Wahl für Aufgaben, die hohe Anonymität erfordern, da sie der tatsächlichen Benutzeraktivität sehr nahe kommen. [1]..
Umgang mit Blöcken und CAPTCHAs
Um Website-Blockaden und CAPTCHA-Herausforderungen zu bewältigen, nutzen Sie Proxy-Rotation und Wiederholungsmechanismen. Hier ist ein Beispiel, wie Sie diese Probleme lösen können:
<span class="hljs-keyword">const</span> proxyManager = {
<span class="hljs-attr">rotationDelay</span>: <span class="hljs-number">2000</span>,
<span class="hljs-keyword">async</span> <span class="hljs-title function_">handleBlock</span>(<span class="hljs-params">page</span>) {
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">waitForTimeout</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">rotationDelay</span>);
<span class="hljs-keyword">const</span> newProxy = <span class="hljs-keyword">await</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getNextProxy</span>();
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">authenticate</span>(newProxy);
<span class="hljs-keyword">return</span> page.<span class="hljs-title function_">reload</span>({ <span class="hljs-attr">waitUntil</span>: <span class="hljs-string">'networkidle0'</span> });
},
<span class="hljs-keyword">async</span> <span class="hljs-title function_">detectBlock</span>(<span class="hljs-params">response</span>) {
<span class="hljs-keyword">return</span> response.<span class="hljs-title function_">status</span>() === <span class="hljs-number">403</span> || response.<span class="hljs-title function_">status</span>() === <span class="hljs-number">429</span>;
}
};
Wenn eine Blockierung erkannt wird, kann ein erneuter Versuch mit einem neuen Proxy dafür sorgen, dass Ihre Automatisierung reibungslos läuft:
page.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-title function_">async</span> (response) => {
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">await</span> proxyManager.<span class="hljs-title function_">detectBlock</span>(response)) {
<span class="hljs-keyword">await</span> proxyManager.<span class="hljs-title function_">handleBlock</span>(page);
}
});
Diese Strategien tragen dazu bei, die Kontinuität des Arbeitsablaufs auch bei Herausforderungen aufrechtzuerhalten.
Verwalten von Proxy-Fehlern
Für die effektive Behandlung von Proxy-Fehlern sind Timeout-Anpassungen, Validierungsprüfungen und Backup-Proxys erforderlich. Beispiel:
<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">handleProxyError</span>(<span class="hljs-params">error, page</span>) {
<span class="hljs-keyword">if</span> (error.<span class="hljs-property">message</span>.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'net::ERR_PROXY_CONNECTION_FAILED'</span>)) {
<span class="hljs-keyword">const</span> newProxy = <span class="hljs-keyword">await</span> <span class="hljs-title function_">getBackupProxy</span>();
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">authenticate</span>(newProxy);
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">setExtraHTTPHeaders</span>({
<span class="hljs-string">'User-Agent'</span>: <span class="hljs-title function_">generateRandomUserAgent</span>()
});
<span class="hljs-keyword">return</span> page.<span class="hljs-title function_">reload</span>({ <span class="hljs-attr">waitUntil</span>: <span class="hljs-string">'networkidle2'</span> });
}
}
Befolgen Sie diese Richtlinien für ein besseres Fehlermanagement:
- Legen Sie Verbindungstimeouts fest, um längere Wartezeiten zu vermeiden.
- Überwachen Sie kontinuierlich die Proxy-Leistung.
- Halten Sie einen Pool von Backup-Proxys bereit.
Auch die richtige Konfiguration Ihres Browsers trägt zur Gewährleistung einer reibungslosen Leistung bei:
<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
<span class="hljs-attr">args</span>: [<span class="hljs-string">'--proxy-server=http://proxy-server:port'</span>],
<span class="hljs-attr">timeout</span>: <span class="hljs-number">60000</span>,
<span class="hljs-attr">waitUntil</span>: <span class="hljs-string">'networkidle0'</span>
});
Überprüfen Sie abschließend immer Ihre Proxys, um sicherzustellen, dass sie funktionsfähig sind:
<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">validateProxy</span>(<span class="hljs-params">proxyConfig</span>) {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> <span class="hljs-title function_">fetch</span>(<span class="hljs-string">'https://api.ipify.org?format=json'</span>, {
<span class="hljs-attr">proxy</span>: <span class="hljs-string">`http://<span class="hljs-subst">${proxyConfig.host}</span>:<span class="hljs-subst">${proxyConfig.port}</span>`</span>
});
<span class="hljs-keyword">return</span> response.<span class="hljs-property">ok</span>;
} <span class="hljs-keyword">catch</span> (error) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`Proxy validation failed: <span class="hljs-subst">${error.message}</span>`</span>);
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
}
Tipps zur Proxy-Leistung
Möchten Sie Puppeteer optimal nutzen? Diese Tipps helfen Ihnen, Ihr Proxy-Setup zu optimieren und die Gesamtleistung zu verbessern.
Geschwindigkeitstests und -optimierung
Verkürzen Sie die Ladezeiten, indem Sie unnötige Anfragen wie Bilder, Schriftarten und Stylesheets blockieren:
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">setRequestInterception</span>(<span class="hljs-literal">true</span>);
page.<span class="hljs-title function_">on</span>(<span class="hljs-string">'request'</span>, <span class="hljs-function">(<span class="hljs-params">req</span>) =></span> {
<span class="hljs-keyword">if</span> (req.<span class="hljs-title function_">resourceType</span>() === <span class="hljs-string">'stylesheet'</span> || req.<span class="hljs-title function_">resourceType</span>() === <span class="hljs-string">'font'</span> || req.<span class="hljs-title function_">resourceType</span>() === <span class="hljs-string">'image'</span>) {
req.<span class="hljs-title function_">abort</span>();
} <span class="hljs-keyword">else</span> {
req.<span class="hljs-title function_">continue</span>();
}
});
Paar page.goto und page.waitForSelector für eine schnellere Navigation:
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">goto</span>(url, {
<span class="hljs-attr">timeout</span>: <span class="hljs-number">30000</span>,
<span class="hljs-attr">waitUntil</span>: <span class="hljs-string">'domcontentloaded'</span>
});
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">waitForSelector</span>(<span class="hljs-string">'.main-content'</span>);
„Denken Sie bei der Optimierung von Puppeteer daran, dass es nur eine begrenzte Anzahl von Möglichkeiten gibt, die Start-/Herunterfahrleistung von Puppeteer selbst zu beschleunigen. Die größten Vorteile erzielen Sie wahrscheinlich durch eine schnellere Darstellung Ihrer Zielseiten.“
– Jon Yongfook, Gründer, Bannerbär [3].
Proxy-Lastverteilung
Verteilen Sie den Datenverkehr mithilfe von Puppeteer Cluster auf mehrere Proxys:
<span class="hljs-keyword">const</span> proxyCluster = {
<span class="hljs-keyword">async</span> <span class="hljs-title function_">distributeLoad</span>(<span class="hljs-params">urls, proxyList</span>) {
<span class="hljs-keyword">const</span> cluster = <span class="hljs-keyword">await</span> <span class="hljs-title class_">Cluster</span>.<span class="hljs-title function_">launch</span>({
<span class="hljs-attr">concurrency</span>: <span class="hljs-title class_">Cluster</span>.<span class="hljs-property">CONCURRENCY_CONTEXT</span>,
<span class="hljs-attr">maxConcurrency</span>: <span class="hljs-number">5</span>
});
<span class="hljs-keyword">await</span> cluster.<span class="hljs-title function_">task</span>(<span class="hljs-title function_">async</span> ({ page, <span class="hljs-attr">data</span>: url }) => {
<span class="hljs-keyword">const</span> proxy = proxyList.<span class="hljs-title function_">getNext</span>();
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">authenticate</span>(proxy);
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">goto</span>(url);
});
}
};
Dieser Ansatz gleicht den Datenverkehr über die Proxys aus und ergänzt frühere Rotationsmethoden, um die Überlastung einzelner Proxys zu vermeiden.
Häufige Probleme und Lösungen
Hier sind einige häufige Herausforderungen und wie man sie effektiv bewältigt:
Netzwerk-Latenz Verwenden Sie den folgenden Code, um Timeouts zu verwalten und unnötige Verzögerungen zu vermeiden:
<span class="hljs-keyword">const</span> <span class="hljs-title function_">performRequest</span> = <span class="hljs-keyword">async</span> (<span class="hljs-params">page, url</span>) => { <span class="hljs-keyword">try</span> { <span class="hljs-keyword">await</span> page.<span class="hljs-title function_">goto</span>(url, { <span class="hljs-attr">timeout</span>: <span class="hljs-number">30000</span>, <span class="hljs-attr">waitUntil</span>: <span class="hljs-string">'networkidle2'</span> }); } <span class="hljs-keyword">catch</span> (error) { <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'TimeoutError'</span>) { <span class="hljs-keyword">await</span> page.<span class="hljs-title function_">evaluate</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">window</span>.<span class="hljs-title function_">stop</span>()); <span class="hljs-keyword">return</span> <span class="hljs-title function_">handleTimeout</span>(); } <span class="hljs-keyword">throw</span> error; } };Rate Limiting Erkennen und reagieren Sie mit diesem Snippet auf Ratenbegrenzungen:
<span class="hljs-keyword">const</span> <span class="hljs-title function_">checkRateLimit</span> = <span class="hljs-keyword">async</span> (<span class="hljs-params">response</span>) => { <span class="hljs-keyword">if</span> (response.<span class="hljs-title function_">status</span>() === <span class="hljs-number">429</span> || response.<span class="hljs-title function_">headers</span>()[<span class="hljs-string">'retry-after'</span>]) { <span class="hljs-keyword">await</span> <span class="hljs-title function_">handleRateLimit</span>(response.<span class="hljs-title function_">headers</span>()[<span class="hljs-string">'retry-after'</span>]); } };Authentifizierungsfehler Behandeln Sie Proxy-Authentifizierungsfehler, indem Sie es mit neuen Anmeldeinformationen erneut versuchen:
<span class="hljs-keyword">const</span> <span class="hljs-title function_">handleAuthError</span> = <span class="hljs-keyword">async</span> (<span class="hljs-params">response, page, proxy</span>) => { <span class="hljs-keyword">if</span> (response.<span class="hljs-title function_">status</span>() === <span class="hljs-number">407</span>) { <span class="hljs-keyword">await</span> <span class="hljs-title function_">retryWithNewCredentials</span>(page, proxy); <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; };
„Durch die Optimierung Ihres Puppeteer-Skripts können Sie einen reibungslosen und effizienten Betrieb mit genauen und konsistenten Ergebnissen sicherstellen.“
- ScrapeOps [2].
Behalten Sie die Antwortzeiten und Erfolgsraten im Auge, um sicherzustellen, dass Ihre Proxys effizient und unentdeckt bleiben.
Fazit
Wichtige Konfigurationsmethoden
Das Einrichten von Puppeteer mit Proxys umfasst einige wichtige Schritte. Für private Proxys verwenden Sie die authenticate Funktion zum Bereitstellen von Anmeldeinformationen:
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">authenticate</span>({
<span class="hljs-attr">username</span>: <span class="hljs-string">'proxyUser'</span>,
<span class="hljs-attr">password</span>: <span class="hljs-string">'proxyPass'</span>
});
Um sicherzustellen, dass die Seite vollständig geladen wird, verwenden Sie die waitUntil Wahlmöglichkeit (z. 'networkidle2') und legen Sie ein Timeout fest (z. B. 30 Sekunden):
<span class="hljs-keyword">await</span> page.<span class="hljs-title function_">goto</span>(url, {
<span class="hljs-attr">waitUntil</span>: <span class="hljs-string">'networkidle2'</span>,
<span class="hljs-attr">timeout</span>: <span class="hljs-number">30000</span>
});
Diese Konfigurationen bilden die Grundlage für ein zuverlässiges Proxy-Setup.
Praxisbeispiele
Um einen sicheren und effizienten Proxy-Betrieb aufrechtzuerhalten, befolgen Sie diese Strategien:
Verbindungsmanagement
- Passen Sie Timeouts basierend auf der Proxy-Geschwindigkeit an.
- Behandeln Sie Authentifizierungsfehler effektiv.
- Überwachen Sie Leistungsmetriken, um Engpässe zu identifizieren.
Sicherheitsoptimierung
- Wechseln Sie Benutzeragenten zusammen mit Proxys, um eine Erkennung zu vermeiden.
- Fügen Sie für zusätzlichen Schutz eine VPN-Ebene hinzu.
- Fangen Sie unnötige Anfragen ab und filtern Sie sie, um Bandbreite zu sparen.
Hier ist eine kurze Zusammenfassung wichtiger Überlegungen zur Proxy-Implementierung:
| Aspekt | Umsetzung | Vorteile |
|---|---|---|
| Authentifizierung | Verwenden Sie die Authentifizierungsfunktion | Sichert den Zugriff auf private Proxys |
| Lastmanagement | Anforderungsabfang aktivieren | Reduziert die Bandbreite und beschleunigt das Laden |
| Fehlerbehandlung | Implementieren Sie try-catch-Blöcke | Bewältigt Verbindungsprobleme elegant |
| Identitätsschutz | Rotieren Sie Proxys und Benutzeragenten | Verbessert die Erfolgsquote und vermeidet Blockaden |
Ähnliche Artikel
- Funktionsweise der Headless-Browser-Erkennung und Umgehung dieser
- Installieren und Konfigurieren von Puppeteer: Lösen häufiger Abhängigkeits- und Chromium-Probleme
- Abfangen und Ändern von Netzwerkanforderungen in Puppeteer: Ein praktischer Leitfaden
- User-Agent-Verwaltung in Puppeteer für die Geräteemulation



