๐ท๏ธ The BEST Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev
title: "ํ๋ก์ ์ํ๊ณผ ์ฐจ๋จ๋ ํฌ๋กค๋ง ์์ฒญ ์ฌ์๋" description: "Scrapling ์คํ์ด๋์์ ํ๋ก์์ ์ํ ๋ฐฉ์์ ์ฌ์ฉํ์ธ์. ์ฐจ๋จ๋ ์๋ต์ ๊ฐ์งํ๊ณ ์์ฒญ์ ์ฌ์๋ํ๋ฉฐ HTTP์์ ๋ธ๋ผ์ฐ์ ์ธ์ ์ผ๋ก ์ ํํฉ๋๋ค."
ํ๋ก์ ๊ด๋ฆฌ์ ์ฐจ๋จ ์ฒ๋ฆฌ {#proxy-management-and-handling-blocks}
์๊ฐ {#introduction}
!!! success "์์ ์ ํ์ธ"
1. [์์ํ๊ธฐ](getting-started.md)๋ฅผ ์ฝ๊ณ ๊ฐ๋จํ ์คํ์ด๋๋ฅผ ๋ง๋ค๊ณ ์คํํ๋ ๋ฐฉ๋ฒ์ ์์๋์ธ์.
2. [์ธ์
](sessions.md)์ ์ฝ๊ณ ์ธ์
์ค์ ๋ฐฉ๋ฒ์ ์ดํดํด์ผ ํฉ๋๋ค.
๋ง์ ๋ฐ์ดํฐ๋ฅผ ์์งํ ๋๋ ์์ฒญ ๋น๋ ์ ํ์ด๋ ์ฐจ๋จ์ ํผํ๊ธฐ ์ํด ์ฌ๋ฌ ํ๋ก์๋ฅผ ๋ฒ๊ฐ์ ์จ์ผ ํ ์ ์์ต๋๋ค. Scrapling์ ProxyRotator๋ก ๊ฐ๋จํ๊ฒ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ๋ชจ๋ ์ธ์
์ข
๋ฅ์ ํจ๊ป ์ฌ์ฉํ ์ ์๊ณ , ์คํ์ด๋์ ์ฐจ๋จ ์์ฒญ ์ฌ์๋ ๊ธฐ๋ฅ๊ณผ๋ ์ฐ๊ฒฐ๋ฉ๋๋ค.
ํ๋ก์๊ฐ ๋ฌด์์ธ์ง, ์ด๋ป๊ฒ ๊ณจ๋ผ์ผ ํ๋์ง ๋ชจ๋ฅธ๋ค๋ฉด ์ด ์๋ด๋ฅผ ์ฐธ๊ณ ํ์ธ์.
ProxyRotator {#proxyrotator}
ProxyRotator๋ ํ๋ก์ ๋ชฉ๋ก์ ๊ด๋ฆฌํ๊ณ ์๋์ผ๋ก ์ํํฉ๋๋ค. ์ธ์
์ proxy_rotator ์ธ์๋ก ์ ๋ฌํ์ธ์.
from scrapling.spiders import Spider, Response
from scrapling.fetchers import FetcherSession, ProxyRotator
class MySpider(Spider):
name = "my_spider"
start_urls = ["https://example.com"]
def configure_sessions(self, manager):
rotator = ProxyRotator([
"http://proxy1:8080",
"http://proxy2:8080",
"http://user:pass@proxy3:8080",
])
manager.add("default", FetcherSession(proxy_rotator=rotator))
async def parse(self, response: Response):
# Check which proxy was used
print(f"Proxy used: {response.meta.get('proxy')}")
yield {"title": response.css("title::text").get("")}
๊ฐ ์์ฒญ์ ์์์ ๋ฐ๋ผ ๋ค์ ํ๋ก์๋ฅผ ์๋์ผ๋ก ์ฌ์ฉํฉ๋๋ค. ์ฌ์ฉํ ํ๋ก์๋ response.meta["proxy"]์ ์ ์ฅ๋๋ฏ๋ก ์ด๋ค ํ๋ก์๋ก ํ์ด์ง๋ฅผ ๊ฐ์ ธ์๋์ง ํ์ธํ ์ ์์ต๋๋ค.
๋ธ๋ผ์ฐ์ ์ธ์ ์์๋ ๋ค์๊ณผ ๊ฐ์ด ์กฐ๊ธ ๋ค๋ฅด๊ฒ ์ค์ ํฉ๋๋ค.
from scrapling.fetchers import AsyncStealthySession, ProxyRotator
# String proxies work for all session types
rotator = ProxyRotator([
"http://proxy1:8080",
"http://proxy2:8080",
])
# Dict proxies (Playwright format) work for browser sessions
rotator = ProxyRotator([
{"server": "http://proxy1:8080", "username": "user", "password": "pass"},
{"server": "http://proxy2:8080"},
])
# Then inside the spider
def configure_sessions(self, manager):
rotator = ProxyRotator(["http://proxy1:8080", "http://proxy2:8080"])
manager.add("browser", AsyncStealthySession(proxy_rotator=rotator))
!!! info
1. ํ ์ธ์
์์ `proxy_rotator`๋ฅผ ๊ณ ์ `proxy` ๋๋ `proxies` ์ธ์์ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ธ์
์ ์ค์ ํ ๋ ํ ๋ฐฉ์์ ์ ํํ์ธ์. ๋์ค์ ๊ฐ๋ณ ์์ฒญ์์ ํ๋ก์๋ฅผ ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ์ ์๋์์ ์ค๋ช
ํฉ๋๋ค.
2. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์ธ์
์ ์ฌ๋ฌ ํญ์ ๊ด๋ฆฌํ๋ ํ๊ณผ ์๊ตฌ ๋ธ๋ผ์ฐ์ ์ปจํ
์คํธ๋ฅผ ์ฌ์ฉํฉ๋๋ค. ํ์ง๋ง ๋ธ๋ผ์ฐ์ ๋ ํญ๋ง๋ค ๋ค๋ฅธ ํ๋ก์๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ `ProxyRotator`๋ฅผ ์ฌ์ฉํ๋ฉด Fetcher๊ฐ ํ๋ก์๋ง๋ค ๋ณ๋์ ์ปจํ
์คํธ์ ํญ ํ๋๋ฅผ ๋ง๋ญ๋๋ค. ํญ์ ์์
์ด ๋๋๋ฉด ํญ๊ณผ ์ปจํ
์คํธ๋ฅผ ๋ชจ๋ ๋ซ์ต๋๋ค.
ํ๋ก์ ์ํ ๋ฐฉ์ ๋ฐ๊พธ๊ธฐ {#custom-rotation-strategies}
๊ธฐ๋ณธ์ ์ผ๋ก ProxyRotator๋ ํ๋ก์๋ฅผ ์์๋๋ก ์ฌ์ฉํ๊ณ , ๋ง์ง๋ง์ ๋๋ฌํ๋ฉด ์ฒ์๋ถํฐ ๋ค์ ์์ํฉ๋๋ค.
๋ค๋ฅธ ๋ฐฉ์์ด ํ์ํ๋ฉด ๋ค์ ์๊ทธ๋์ฒ๋ฅผ ๋ฐ๋ฅด๋ ํจ์๋ฅผ ์ ๋ฌํ์ธ์.
from scrapling.core._types import ProxyType
def my_strategy(proxies: list, current_index: int) -> tuple[ProxyType, int]:
...
์ด ํจ์๋ ํ๋ก์ ๋ชฉ๋ก๊ณผ ํ์ฌ ์ธ๋ฑ์ค๋ฅผ ๋ฐ๊ณ , ์ ํํ ํ๋ก์์ ๋ค์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค.
๋ค์์ ์ฌ์ฉ์ ์ ์ ์ํ ๋ฐฉ์์ ์์ ๋๋ค.
๋ฌด์์ ์ ํ {#random-rotation}
import random
from scrapling.fetchers import ProxyRotator
def random_strategy(proxies, current_index):
idx = random.randint(0, len(proxies) - 1)
return proxies[idx], idx
rotator = ProxyRotator(
["http://proxy1:8080", "http://proxy2:8080", "http://proxy3:8080"],
strategy=random_strategy,
)
๊ฐ์ค์น์ ๋ฐ๋ฅธ ์ ํ {#weighted-rotation}
import random
def weighted_strategy(proxies, current_index):
# First proxy gets 60% of traffic, others split the rest
weights = [60] + [40 // (len(proxies) - 1)] * (len(proxies) - 1)
proxy = random.choices(proxies, weights=weights, k=1)[0]
return proxy, current_index # Index doesn't matter for weighted
rotator = ProxyRotator(proxies, strategy=weighted_strategy)
๊ฐ๋ณ ์์ฒญ์ ํ๋ก์ ๋ฐ๊พธ๊ธฐ {#per-request-proxy-override}
๊ฐ๋ณ ์์ฒญ์ proxy= ํค์๋ ์ธ์๋ฅผ ์ ๋ฌํ๋ฉด ์๋ ์ํ ๋์ ํด๋น ํ๋ก์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
async def parse(self, response: Response):
# This request uses the rotator's next proxy
yield response.follow("/page1", callback=self.parse_page)
# This request uses a specific proxy, bypassing the rotator
yield response.follow(
"/special-page",
callback=self.parse_page,
proxy="http://special-proxy:8080",
)
ํน์ ํ์ด์ง์ ์ง์ ๋ ํ๋ก์๊ฐ ํ์ํ ๋ ์ ์ฉํฉ๋๋ค. ์๋ฅผ ๋ค์ด ์ง์ญ๋ณ ์ฝํ ์ธ ๋ฅผ ๊ฐ์ ธ์ฌ ๋ ํด๋น ์ง์ญ์ ํ๋ก์๋ฅผ ์ธ ์ ์์ต๋๋ค.
์ฐจ๋จ๋ ์์ฒญ ์ฒ๋ฆฌ {#blocked-request-handling}
์คํ์ด๋์๋ ์ฐจ๋จ ๊ฐ์ง์ ์ฌ์๋ ๊ธฐ๋ฅ์ด ์์ต๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก 401, 403, 407, 429, 444, 500, 502, 503, 504 HTTP ์ํ ์ฝ๋๋ฅผ ์ฐจ๋จ์ผ๋ก ๋ด
๋๋ค.
์ฌ์๋๋ ๋ค์ ์์๋ก ์งํํฉ๋๋ค.
- ์๋ต์ ๋ฐ์ผ๋ฉด
is_blocked(response)๋ฅผ ํธ์ถํฉ๋๋ค. - ์ฐจ๋จ๋ ๊ฒฝ์ฐ ์์ฒญ์ ๋ณต์ฌํ๊ณ
retry_blocked_request()๋ฅผ ํธ์ถํฉ๋๋ค. ์ฌ๊ธฐ์ ์ฌ์๋ํ ์์ฒญ์ ์์ ํ ์ ์์ต๋๋ค. - ์์ฒญ์
dont_filter=True์ ๋ฎ์ ์ฐ์ ์์๋ก ํ์ ๋ค์ ๋ฃ์ต๋๋ค. ์ค๋ณต ์ ๊ฑฐ๋ฅผ ๊ฑด๋๋ฐ๊ณ ๋ฐ๋ก ์ฌ์๋ํ์ง ์๋๋ก ํฉ๋๋ค. - ์ต๋
max_blocked_retries๋ฒ ๋ฐ๋ณตํฉ๋๋ค. ๊ธฐ๋ณธ๊ฐ์ 3์ ๋๋ค.
!!! tip
1. ์ฌ์๋ํ ๋ ๊ธฐ์กด `proxy`/`proxies` ์ธ์๋ฅผ ์์ฒญ์์ ์๋์ผ๋ก ์ง์๋๋ค. ๋ฐ๋ผ์ ์ํ ๊ธฐ๋ฅ์ด ์ ํ๋ก์๋ฅผ ์ ํํฉ๋๋ค.
2. `max_blocked_retries`๋ ์ธ์
๋ด๋ถ์ ์ฌ์๋์ ๋ณ๊ฐ์ด๋ฉฐ ํ์๋ฅผ ๊ณต์ ํ์ง ์์ต๋๋ค.
์ฐจ๋จ ๊ฐ์ง ๋ฐ๊พธ๊ธฐ {#custom-block-detection}
is_blocked()๋ฅผ ์ฌ์ ์ํด ํ์ํ ๊ฐ์ง ๋ฐฉ์์ ์ถ๊ฐํ์ธ์.
class MySpider(Spider):
name = "my_spider"
start_urls = ["https://example.com"]
async def is_blocked(self, response: Response) -> bool:
# Check status codes (default behavior)
if response.status in {403, 429, 503}:
return True
# Check response content
body = response.body.decode("utf-8", errors="ignore")
if "access denied" in body.lower() or "rate limit" in body.lower():
return True
return False
async def parse(self, response: Response):
yield {"title": response.css("title::text").get("")}
์ฌ์๋ ๋์ ๋ฐ๊พธ๊ธฐ {#customizing-retries}
retry_blocked_request()๋ฅผ ์ฌ์ ์ํ๋ฉด ์ฌ์๋ ์ ์ ์์ฒญ์ ๋ฐ๊ฟ ์ ์์ต๋๋ค. max_blocked_retries๋ ์ฐจ๋จ๋ ์์ฒญ์ ์ฌ์๋ ํ์์ด๋ฉฐ ๊ธฐ๋ณธ๊ฐ์ 3์
๋๋ค.
from scrapling.spiders import Spider, SessionManager, Request, Response
from scrapling.fetchers import FetcherSession, AsyncStealthySession
class MySpider(Spider):
name = "my_spider"
start_urls = ["https://example.com"]
max_blocked_retries = 5
def configure_sessions(self, manager: SessionManager) -> None:
manager.add('requests', FetcherSession(impersonate=['chrome', 'firefox', 'safari']))
manager.add('stealth', AsyncStealthySession(block_webrtc=True), lazy=True)
async def retry_blocked_request(self, request: Request, response: Response) -> Request:
request.sid = "stealth"
self.logger.info(f"Retrying blocked request: {request.url}")
return request
async def parse(self, response: Response):
yield {"title": response.css("title::text").get("")}
์ ์๋ ์ฐจ๋จ ๊ฐ์ง ๋ฐฉ์์ ๊ทธ๋๋ก ๋๊ณ HTTP ์์ฒญ์ ์ฌ์ฉํ๋ค๊ฐ ์ฐจ๋จ๋๋ฉด ์คํ ์ค ๋ธ๋ผ์ฐ์ ๋ก ์ ํํฉ๋๋ค.
๋ชจ๋ ํฉ์น๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
from scrapling.spiders import Spider, SessionManager, Request, Response
from scrapling.fetchers import FetcherSession, AsyncStealthySession, ProxyRotator
cheap_proxies = ProxyRotator([ "http://proxy1:8080", "http://proxy2:8080"])
# A format acceptable by the browser
expensive_proxies = ProxyRotator([
{"server": "http://residential_proxy1:8080", "username": "user", "password": "pass"},
{"server": "http://residential_proxy2:8080", "username": "user", "password": "pass"},
{"server": "http://mobile_proxy1:8080", "username": "user", "password": "pass"},
{"server": "http://mobile_proxy2:8080", "username": "user", "password": "pass"},
])
class MySpider(Spider):
name = "my_spider"
start_urls = ["https://example.com"]
max_blocked_retries = 5
def configure_sessions(self, manager: SessionManager) -> None:
manager.add('requests', FetcherSession(impersonate=['chrome', 'firefox', 'safari'], proxy_rotator=cheap_proxies))
manager.add('stealth', AsyncStealthySession(block_webrtc=True, proxy_rotator=expensive_proxies), lazy=True)
async def retry_blocked_request(self, request: Request, response: Response) -> Request:
request.sid = "stealth"
self.logger.info(f"Retrying blocked request: {request.url}")
return request
async def parse(self, response: Response):
yield {"title": response.css("title::text").get("")}
์ฒ์์๋ ๋ฐ์ดํฐ์ผํฐ ํ๋ก์์ฒ๋ผ ์ ๋ ดํ ํ๋ก์๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ฐจ๋จ๋๋ฉด ์ฃผ๊ฑฐ์ฉ์ด๋ ๋ชจ๋ฐ์ผ ํ๋ก์์ฒ๋ผ ํ์ง์ด ๋์ ํ๋ก์๋ก ์ฌ์๋ํฉ๋๋ค.