프록시 관리
IP 주소 차단은 웹사이트 접근을 제한하는 가장 오래되고 효과적인 방법 중 하나입니다. 따라서 웹 스크래핑 라이브러리에서는 IP 차단을 우회할 수 있는 사용하기 쉽고 강력한 도구를 제공하는 것이 매우 중요합니다. IP 차단을 피하기 위한 가장 강력한 도구는 프록시 서버입니다.
Crawlee를 사용하면 자체 프록시 서버나 타사 제공업체의 프록시 서버를 활용할 수 있습니다.
차단 방지에 대한 자세한 내용은 차단 방지 가이드를 참조하세요.
빠른 시작
이미 프록시 URL을 보유하고 있다면, 몇 줄의 코드만으로 바로 사용을 시작할 수 있습니다.
import { ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: [
'http://proxy-1.com',
'http://proxy-2.com',
]
});
const proxyUrl = await proxyConfiguration.newUrl();
크롤러에서 프록시 URL을 사용하는 예제는 아래 크롤러 통합 섹션에서 확인할 수 있습니다.
프록시 설정
모든 프록시 관련 기능은 ProxyConfiguration
클래스에서 관리됩니다. 제공된 옵션을 기반으로 ProxyConfiguration
constructor
함수를 사용하여 인스턴스를 생성합니다. 사용 가능한 모든 생성자 옵션은 ProxyConfigurationOptions
를 참조하세요.
정적 프록시 목록
proxyUrls
옵션에 프록시 URL 목록을 제공할 수 있습니다. ProxyConfiguration
은 제공된 프록시들을 순차적으로 사용합니다.
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: [
'http://proxy-1.com',
'http://proxy-2.com',
null // null은 프록시를 사용하지 않음을 의미합니다
]
});
이는 프록시 목록을 사용하는 가장 간단한 방법입니다. Crawlee는 라운드 로빈 방식으로 프록시 목록을 순환합니다.
커스텀 프록시 함수
ProxyConfiguration
클래스에서는 프록시 URL을 선택하기 위한 사용자 정의 함수를 제공할 수 있습니다. 프록시 선택에 대한 자체 로직을 구현하고 싶을 때 유용합니다.
const proxyConfiguration = new ProxyConfiguration({
newUrlFunction: (sessionId, { request }) => {
if (request?.url.includes('crawlee.dev')) {
return null; // crawlee.dev의 경우 프록시를 사용하지 않음
}
return 'http://proxy-1.com'; // 다른 모든 URL에 대해 이 프록시 사용
}
});
newUrlFunction
은 sessionId
와 options
두 개의 매개변수를 받아 프록시 URL 문자열을 반환합니다.
sessionId
매개변수는 항상 제공되며 서로 다른 세션을 구분할 수 있게 해줍니다. 예를 들어, Crawlee가 크롤러가 차단되었음을 감지하면 자동으로 다른 ID를 가진 새로운 세션을 생성합니다.
options
매개변수는 Request
객체를 포함하며, 이는 실행될 요청을 나타냅니다. 이 객체가 항상 사용 가능한 것은 아닙니다. 예를 들어 newUrl
함수를 직접 사용할 때는 사용할 수 없습니다. 따라서 사용자 정의 함수는 request
객체의 존재 여부에 의존해서는 안 되며, 객체가 없을 때의 기본 동작을 제공해야 합니다.
계층화된 프록시
ProxyConfiguration
클래스에 프록시 계층 목록을 제공할 수도 있습니다. 이는 웹사이트의 차단 동작에 따라 자동으로 다른 프록시로 전환하고 싶을 때 유용합니다.
tieredProxyUrls
옵션을 사용하려면 크롤러 인스턴스에서 ProxyConfiguration
을 사용해야 합니다(아래 참조).
newUrl
호출을 통해 이 설정을 사용하면 예상된 결과를 얻을 수 없습니다.
const proxyConfiguration = new ProxyConfiguration({
tieredProxyUrls: [
[null], // 처음에는 프록시 없이 연결 시도
['http://okay-proxy.com'],
['http://slightly-better-proxy.com', 'http://slightly-better-proxy-2.com'],
['http://very-good-and-expensive-proxy.com'],
]
});
이 설정에서는 먼저 프록시 없이 시작한 다음, Crawlee가 대상 웹사이트에서 차단되고 있음을 감지하면 http://okay-proxy.com
으로 전환합니다. 이 프록시도 차단되면 slightly-better-proxy
URL 중 하나로 전환하고, 이것들도 차단되면 very-good-and-expensive-proxy.com
URL로 전환합니다.
Crawlee는 또한 주기적으로 하위 계층 프록시가 차단 해제되었는지 확인하고, 차단이 해제된 경우 다시 해당 프록시로 전환합니다.
크롤러 통합
ProxyConfiguration
은 HttpCrawler
, CheerioCrawler
, JSDOMCrawler
, PlaywrightCrawler
, PuppeteerCrawler
와 원활하게 통합됩니다.
- HttpCrawler
- CheerioCrawler
- JSDOMCrawler
- PlaywrightCrawler
- PuppeteerCrawler
import { HttpCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: ['http://proxy-1.com', 'http://proxy-2.com'],
});
const crawler = new HttpCrawler({
proxyConfiguration,
// ...
});
import { CheerioCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: ['http://proxy-1.com', 'http://proxy-2.com'],
});
const crawler = new CheerioCrawler({
proxyConfiguration,
// ...
});
import { JSDOMCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: ['http://proxy-1.com', 'http://proxy-2.com'],
});
const crawler = new JSDOMCrawler({
proxyConfiguration,
// ...
});
import { PlaywrightCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: ['http://proxy-1.com', 'http://proxy-2.com'],
});
const crawler = new PlaywrightCrawler({
proxyConfiguration,
// ...
});
import { PuppeteerCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: ['http://proxy-1.com', 'http://proxy-2.com'],
});
const crawler = new PuppeteerCrawler({
proxyConfiguration,
// ...
});
이제 크롤러는 모든 연결에 선택된 프록시를 사용합니다.
IP 로테이션과 세션 관리
proxyConfiguration.newUrl()
에 sessionId
매개변수를 전달할 수 있습니다. 이는 sessionId
-proxyUrl
쌍을 생성하는 데 사용되며, 동일한 sessionId
로 후속 newUrl()
호출을 하면 항상 동일한 proxyUrl
을 반환합니다. 이는 실제 사용자처럼 보이게 하려는 스크래핑에서 매우 유용합니다. 실제 세션 유지가 차단을 피하는 데 어떻게 도움이 되는지에 대한 자세한 내용은 세션 관리 가이드와 SessionPool
클래스를 참조하세요.
sessionId
가 제공되지 않으면 프록시 URL은 라운드 로빈 방식으로 순환됩니다.
- HttpCrawler
- CheerioCrawler
- JSDOMCrawler
- PlaywrightCrawler
- PuppeteerCrawler
- Standalone
import { HttpCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new HttpCrawler({
useSessionPool: true,
persistCookiesPerSession: true,
proxyConfiguration,
// ...
});
import { CheerioCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new CheerioCrawler({
useSessionPool: true,
persistCookiesPerSession: true,
proxyConfiguration,
// ...
});
import { JSDOMCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new JSDOMCrawler({
useSessionPool: true,
persistCookiesPerSession: true,
proxyConfiguration,
// ...
});
import { PlaywrightCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new PlaywrightCrawler({
useSessionPool: true,
persistCookiesPerSession: true,
proxyConfiguration,
// ...
});
import { PuppeteerCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new PuppeteerCrawler({
useSessionPool: true,
persistCookiesPerSession: true,
proxyConfiguration,
// ...
});
import { ProxyConfiguration, SessionPool } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const sessionPool = await SessionPool.open({
/* opts */
});
const session = await sessionPool.getSession();
const proxyUrl = await proxyConfiguration.newUrl(session.id);
크롤러에서 현재 프록시 검사하기
HttpCrawler
, CheerioCrawler
, JSDOMCrawler
, PlaywrightCrawler
, PuppeteerCrawler
는 requestHandler
에서 proxyInfo
객체를 통해 현재 사용 중인 프록시에 대한 정보에 접근할 수 있습니다. proxyInfo
객체를 사용하면 프록시 URL에 쉽게 접근할 수 있습니다.
- HttpCrawler
- CheerioCrawler
- JSDOMCrawler
- PlaywrightCrawler
- PuppeteerCrawler
import { HttpCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new HttpCrawler({
proxyConfiguration,
async requestHandler({ proxyInfo }) {
console.log(proxyInfo);
},
// ...
});
import { CheerioCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new CheerioCrawler({
proxyConfiguration,
async requestHandler({ proxyInfo }) {
console.log(proxyInfo);
},
// ...
});
import { JSDOMCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new JSDOMCrawler({
proxyConfiguration,
async requestHandler({ proxyInfo }) {
console.log(proxyInfo);
},
// ...
});
import { PlaywrightCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new PlaywrightCrawler({
proxyConfiguration,
async requestHandler({ proxyInfo }) {
console.log(proxyInfo);
},
// ...
});
import { PuppeteerCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
/* opts */
});
const crawler = new PuppeteerCrawler({
proxyConfiguration,
async requestHandler({ proxyInfo }) {
console.log(proxyInfo);
},
// ...
});