Shifter verwenden mit Cheerio
Kombinieren Sie Shifters Residential- und ISP-Proxys mit Cheerio für schnelles, schlankes Node-Scraping. Cheerio übernimmt das jQuery-artige HTML-Parsing, Shifter übernimmt die Residential-IPs -- kein Headless-Browser erforderlich.
Schnellstart
Installieren
npm install cheerio axios https-proxy-agent Grundlegende Nutzung
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
import { load } from "cheerio";
const proxyUrl =
"customer-USERNAME-country-us-sid-123ABC:PASSWORD@p.shifter.io:443";
const { data: html } = await axios.get("https://example.com", {
httpsAgent: new HttpsProxyAgent(proxyUrl),
proxy: false,
});
const $ = load(html);
console.log($("h1").text());
$("article.post").each((_, el) => {
console.log($(el).find("h2").text(), "->", $(el).find("a").attr("href"));
}); Funktionen
Beispiele
Sticky Session + mehrseitiger Crawl
Pinnen Sie eine Residential-IP für die Dauer eines mehrseitigen Crawls. Fügen Sie `country-uk` für Geo-Targeting und `ttl-300` hinzu, um das Sticky-Fenster auf 5 Minuten zu verlängern.
import axios, { type AxiosInstance } from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
import { load } from "cheerio";
import { randomBytes } from "node:crypto";
function makeClient(country: string): AxiosInstance {
const sid = randomBytes(4).toString("hex");
const proxyUrl =
`customer-USERNAME-country-${country}-sid-${sid}-ttl-300:` +
`PASSWORD@p.shifter.io:443`;
return axios.create({
httpsAgent: new HttpsProxyAgent(proxyUrl),
proxy: false,
timeout: 30_000,
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
"Accept-Language": "en-US,en;q=0.9",
},
});
}
const client = makeClient("uk");
let url: string | null = "https://example.co.uk/products";
const products: { title: string; price: string }[] = [];
while (url) {
const { data: html } = await client.get(url);
const $ = load(html);
$(".product-card").each((_, el) => {
products.push({
title: $(el).find("h2").text().trim(),
price: $(el).find(".price").text().trim(),
});
});
const next = $("a.next-page").attr("href");
url = next ? new URL(next, url).toString() : null;
}
console.log(`Scraped ${products.length} products`); Paralleles Scraping (Rotation pro Anfrage)
Lassen Sie die sid für die Rotation pro Anfrage weg. Jeder parallele Abruf erhält eine andere Residential-IP -- ideal, um eine Liste von URLs abzurufen, ohne IP-basierte Rate-Limits auszulösen.
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
import { load } from "cheerio";
function rotatingClient() {
// No sid -> Shifter rotates the residential IP per request.
const proxyUrl =
"customer-USERNAME-country-us:PASSWORD@p.shifter.io:443";
return axios.create({
httpsAgent: new HttpsProxyAgent(proxyUrl),
proxy: false,
timeout: 30_000,
});
}
async function scrape(url: string) {
const client = rotatingClient();
const { data: html } = await client.get(url);
const $ = load(html);
return {
url,
title: $("h1").first().text().trim(),
headings: $("h2").map((_, el) => $(el).text().trim()).get(),
};
}
const urls = [
"https://example.com/category/laptops",
"https://example.com/category/phones",
"https://example.com/category/tablets",
"https://example.com/category/wearables",
];
const results = await Promise.all(urls.map(scrape));
console.log(results); got-scraping (integrierte Browser-Fingerprints)
got-scraping bündelt realistische Header-Generierung auf Basis von got. Kombinieren Sie es mit Shifter für einen kompakten, browsertypischen HTTP-Scraper, der dennoch 100x schneller als Puppeteer ist.
// npm install got-scraping cheerio
import { gotScraping } from "got-scraping";
import { load } from "cheerio";
const proxyUrl =
"customer-USERNAME-country-de-city-berlin-sid-456DEF:PASSWORD@p.shifter.io:443";
const html = await gotScraping({
url: "https://example.de/products",
proxyUrl,
headerGeneratorOptions: {
browsers: [{ name: "chrome", minVersion: 120 }],
devices: ["desktop"],
locales: ["de-DE", "en"],
operatingSystems: ["macos", "linux"],
},
}).text();
const $ = load(html);
$(".product").each((_, el) => {
console.log({
title: $(el).find("h2").text().trim(),
price: $(el).find(".price").text().trim(),
});
}); Crawlee (Produktions-Crawler mit Shifter)
Crawlee (von Apify) verwaltet Queues, Wiederholungsversuche, Persistenz und Proxy-Rotation von Haus aus. Binden Sie Shifter als ProxyConfiguration ein und lassen Sie Crawlee den Rest orchestrieren.
// npm install crawlee cheerio
import { CheerioCrawler, ProxyConfiguration } from "crawlee";
const proxyConfiguration = new ProxyConfiguration({
newUrlFunction: () => {
// New residential IP per session. Crawlee rotates sessions
// automatically on bans, so stale IPs get cycled out.
const sid = Math.random().toString(36).slice(2, 10);
return `customer-USERNAME-country-fr-sid-${sid}:PASSWORD@p.shifter.io:443`;
},
});
const crawler = new CheerioCrawler({
proxyConfiguration,
maxConcurrency: 8,
async requestHandler({ request, $, enqueueLinks, log }) {
log.info(`${request.url} -> ${$("h1").text().trim()}`);
$(".article").each((_, el) => {
log.info(` ${$(el).find("h2").text().trim()}`);
});
await enqueueLinks({ selector: "a.next-page", strategy: "same-domain" });
},
});
await crawler.run([
"https://example.fr/blog",
"https://example.fr/news",
]); Häufig gefragt FAQ-Fragen
Häufige Fragen zur Verwendung von Shifter mit Cheerio.
Nein. Cheerio ist ein Parser -- er stellt keine HTTP-Anfragen. Der Proxy wird auf dem jeweiligen HTTP-Client konfiguriert, den Sie mit Cheerio kombinieren (axios, got, undici, fetch). Sobald das HTML über Shifter abgerufen wurde, übergeben Sie es wie gewohnt an Cheerios load()-Funktion.
Shifter verwenden mit Cheerio
Kombinieren Sie Shifters 205M+ Residential- und ISP-Proxys mit Cheerio für schnelles, schlankes Node-Scraping. Rotation pro Anfrage, Sticky Sessions und vollständige Crawlee-Unterstützung.