๐ท๏ธ 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: "ํฌ๋กค๋ง ์ ์ด: AutoThrottle, ์ฒดํฌํฌ์ธํธ, ์บ์, ์คํธ๋ฆฌ๋ฐ" description: "ํฌ๋กค๋ง ์๋์ ๋์ ์์ฒญ์ ์ ์ดํ์ธ์. ์๋ต ์บ์, ์ผ์ ์ค์ง์ ์ฌ๊ฐ, ํญ๋ชฉ ์คํธ๋ฆฌ๋ฐ, ์ฒดํฌํฌ์ธํธ์ ์ ์ฅ๋๋ ์ํ๋ฅผ ์์๋ด ๋๋ค."
๊ณ ๊ธ ๊ธฐ๋ฅ {#advanced-usages}
์๊ฐ {#introduction}
!!! success "์์ ์ ํ์ธ"
1. [์์ํ๊ธฐ](getting-started.md)๋ฅผ ์ฝ๊ณ ๊ฐ๋จํ ์คํ์ด๋๋ฅผ ๋ง๋ค๊ณ ์คํํ๋ ๋ฐฉ๋ฒ์ ์์๋์ธ์.
์ด ํ์ด์ง๋ ๋์ ์์ฒญ ์ ์ด, ์ผ์ ์ค์ง์ ์ฌ๊ฐ, ์คํธ๋ฆฌ๋ฐ, ์๋ช ์ฃผ๊ธฐ ํ , ํต๊ณ, ๋ก๊น ๋ฑ ์คํ์ด๋์ ๊ณ ๊ธ ๊ธฐ๋ฅ์ ์ค๋ช ํฉ๋๋ค.
๋์ ์์ฒญ ์ ์ด {#concurrency-control}
๋ค์ ํด๋์ค ์์ฑ์ผ๋ก ํฌ๋กค๋ง ๋ถํ๋ฅผ ์กฐ์ ํฉ๋๋ค.
| ์์ฑ | ๊ธฐ๋ณธ๊ฐ | ์ค๋ช |
|---|---|---|
concurrent_requests |
4 |
๋์์ ์ฒ๋ฆฌํ ์ต๋ ์์ฒญ ์ |
concurrent_requests_per_domain |
0 |
๋๋ฉ์ธ๋ณ ์ต๋ ๋์ ์์ฒญ ์์ ๋๋ค. 0์ ๋๋ฉ์ธ๋ณ ์ ํ์ด ์๋ค๋ ๋ป์ ๋๋ค |
download_delay |
0.0 |
๊ฐ ์์ฒญ ์ ๋๊ธฐ ์๊ฐ(์ด) |
robots_txt_obey |
False |
robots.txt์ Disallow, Crawl-delay, Request-rate ๊ท์น์ ์งํต๋๋ค |
class PoliteSpider(Spider):
name = "polite"
start_urls = ["https://example.com"]
# Be gentle with the server
concurrent_requests = 4
concurrent_requests_per_domain = 2
download_delay = 1.0 # Wait 1 second between requests
async def parse(self, response: Response):
yield {"title": response.css("title::text").get("")}
concurrent_requests_per_domain์ ์ค์ ํ๋ฉด ์ ์ฒด ์ ํ ์ธ์ ๋๋ฉ์ธ๋ณ ์ ํ์ด ์๊น๋๋ค. ์ฌ๋ฌ ๋๋ฉ์ธ์ ๋์์ ํฌ๋กค๋งํ ๋ ์ ์ฒด ์์ฒญ ์๋ ๋๊ฒ ์ ์งํ๋ฉด์ ๊ฐ ์ฌ์ดํธ์ ๋ถํ๋ฅผ ์ค์ผ ์ ์์ต๋๋ค.
!!! tip
`download_delay`๋ ๋๋ฉ์ธ๊ณผ ๊ด๊ณ์์ด ๋ชจ๋ ์์ฒญ ์ ์ ๊ณ ์ ๋ ์๊ฐ์ ๊ธฐ๋ค๋ฆฝ๋๋ค. ๊ฐ๋จํ ์์ฒญ ๋น๋ ์ ํ์ ์ฌ์ฉํ์ธ์.
AutoThrottle {#autothrottle}
๊ณ ์ ๋ download_delay๋ ์ง์ ์์ํด์ ์ ํด์ผ ํฉ๋๋ค. ๋๋ฌด ์งง์ผ๋ฉด ์ฐจ๋จ๋๊ณ ๋๋ฌด ๊ธธ๋ฉด ํฌ๋กค๋ง์ด ์ค๋ ๊ฑธ๋ฆฝ๋๋ค. AutoThrottle์ ์ค์ ์๋ต ์๋๋ฅผ ๋ณด๊ณ ๊ฐ ๋๋ฉ์ธ์ ์ง์ฐ์ ์กฐ์ ํฉ๋๋ค. ๋น ๋ฅธ ์๋ฒ์์๋ ์๋๋ฅผ ๋์ด๊ณ , ๋๋ฆฌ๊ฑฐ๋ ์์ฒญ์ ์ฐจ๋จํ๋ ์๋ฒ์์๋ ์๋๋ฅผ ์ค์
๋๋ค.
| ์์ฑ | ๊ธฐ๋ณธ๊ฐ | ์ค๋ช |
|---|---|---|
autothrottle_enabled |
False |
์ ์ํ ์ง์ฐ์ ์ผญ๋๋ค |
autothrottle_start_delay |
5.0 |
๋๋ฉ์ธ์ ์ฒซ ์์ฒญ์ ์ฌ์ฉํ ์ง์ฐ |
autothrottle_max_delay |
60.0 |
ํ์ฉ๋๋ ์ต๋ ์ง์ฐ |
autothrottle_target_concurrency |
None |
๋ชฉํ๋ก ํ๋ ๋๋ฉ์ธ๋ณ ๋์ ์์ฒญ ์์ ๋๋ค. ์๋ ์ค๋ช ์ ์ฐธ๊ณ ํ์ธ์ |
autothrottle_block_backoff |
True |
์ฐจ๋จ๋ ๋๋ง๋ค ํด๋น ๋๋ฉ์ธ์ ์ง์ฐ์ ๋ ๋ฐฐ๋ก ๋๋ฆฝ๋๋ค |
class AdaptiveSpider(Spider):
name = "adaptive"
start_urls = ["https://example.com"]
concurrent_requests = 8
concurrent_requests_per_domain = 1
autothrottle_enabled = True
autothrottle_start_delay = 2.0
autothrottle_max_delay = 30.0
async def parse(self, response: Response):
yield {"title": response.css("title::text").get("")}
์๋ต์ ๋ฐ์ ๋๋ง๋ค ์ง์ฐ์ ์๋ฒ์ ์๋ต ์๊ฐ์ ๊ฐ๊น์์ง๋๋ค. ์ฌ์ดํธ๊ฐ 0.5์ด ์์ ์๋ตํ๋ฉด ์ง์ฐ๋ ์ฝ 0.5์ด๊ฐ ๋๋ฉฐ, ๋๋ต ์์ฒญ ํ๋์ฉ ์ฒ๋ฆฌํ๋ ์ํ๊ฐ ๋ฉ๋๋ค. ๋๋ฉ์ธ๋ณ ๋ชฉํ ์์ฒญ ์๋ ์ค์ ๋ autothrottle_target_concurrency, ๋ค์์ผ๋ก concurrent_requests_per_domain, ๋ ๋ค ์์ผ๋ฉด 1์ ์ฌ์ฉํฉ๋๋ค. ์ด๋ฏธ ์ค์ ํ ๋๋ฉ์ธ๋ณ ์ ํ์ด ๋ชฉํ๊ฐ ๋๊ณ ์ง์ฐ์ ๊ทธ ๊ฐ์ผ๋ก ๋๋๋ฏ๋ก, ๋ณดํต ๋ณ๋์ ๋ชฉํ๋ฅผ ์ง์ ํ ํ์๊ฐ ์์ต๋๋ค. ์๋ต ์๊ฐ์ด ๊ธ๊ฒฉํ ๋์ด๋๋ฉด ํ๊ท ์ ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๋ฐ๋ก ๋ฐ์ํฉ๋๋ค. ๋ฐ๋ผ์ ์๋ฒ๊ฐ ๋๋ ค์ง๋ฉด ์ฆ์ ์๋๋ฅผ ์ค์ด๊ณ , ๋ค์ ๋นจ๋ผ์ง ๋๋ ์์ํ ์๋๋ฅผ ๋์
๋๋ค.
์ฐจ๋จ๋ ๋ ์๋ ์ค์ด๊ธฐ {#backing-off-when-blocked}
์๋ต ์๊ฐ๋ง์ผ๋ก๋ ์์ฒญ ๋น๋ ์ ํ์ ์ ์ ์์ต๋๋ค. 429, 403, CAPTCHA ํ์ด์ง๋ ์ค์ ์ฝํ
์ธ ๋ณด๋ค ๋ ๋นจ๋ฆฌ ๋์์ค๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. ์๊ฐ๋ง ๋ณด๋ฉด ์๋๋ฅผ ๋์ฌ๋ ๋๋ค๊ณ ํ๋จํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ์ํ ์ฝ๋๊ฐ 2xx๊ฐ ์๋๊ฑฐ๋ is_blocked()๊ฐ ์ฐจ๋จ์ผ๋ก ํ๋จํ ์๋ต์ ๋๋ฉ์ธ์ ์ง์ฐ์ ๋ ๋ฐฐ๋ก ๋๋ฆฝ๋๋ค.
0.5s -> 1s -> 2s -> 4s -> 8s ... (up to autothrottle_max_delay)
์ฌ์ดํธ๊ฐ ๊ณ์ ๊ฑฐ๋ถํ๋ฉด ์คํ์ด๋๋ ๊ณ์ ๋๋ ค์ง๋๋ค. ์ ์ ์๋ต์ด ๋์์ค๋ฉด ๋ค์ ํ๊ท ์ ์ ์ฉํด ์๋ฒ ์๋์ ๋ง๊ฒ ์ง์ฐ์ ๋ฎ์ถฅ๋๋ค. ๋ค์ ์์ํ์ง ์์๋ ์๋์ผ๋ก ํ๋ณตํฉ๋๋ค.
์ฌ์ดํธ๊ฐ 429 ๋๋ 503 ์๋ต์ Retry-After ํค๋๋ก ๋๊ธฐ ์๊ฐ์ ์๋ ค์ฃผ๋ฉด, ์ง์ฐ์ ๋ ๋ฐฐ๋ก ๋๋ฆฌ๋ ๋์ ๊ทธ ๊ฐ์ ์ฌ์ฉํฉ๋๋ค. ์ซ์ ํ์(Retry-After: 120)๊ณผ HTTP ๋ ์ง ํ์์ ๋ชจ๋ ์ดํดํฉ๋๋ค.
์ด ๊ธฐ๋ฅ์ ๋๋ ค๋ฉด autothrottle_block_backoff = False๋ฅผ ์ค์ ํ์ธ์. ์๋ต ์๊ฐ๋ง์ผ๋ก ์ง์ฐ์ ์กฐ์ ํ๋ฉฐ, ์ฐจ๋จ์ ์๋๊ฐ ๋ ๋นจ๋ผ์ง๋ ๊ฒ๋ง ๋ง์ต๋๋ค.
๋๋ฉ์ธ๋ณ ์ต์ข ์ง์ฐ์ ํต๊ณ์์ ํ์ธํ ์ ์์ต๋๋ค.
result = AdaptiveSpider().start()
print(result.stats.autothrottle_delays) # {'example.com': 0.62}
!!! note "์ฐธ๊ณ "
* `download_delay`์ robots.txt ์ง์ฐ์ ์ต์๊ฐ์ผ๋ก ์ฌ์ฉํ์ง๋ง, ์ด ๊ฐ๋ `autothrottle_max_delay`์ ์ ํ์ ๋ฐ์ต๋๋ค. ์ง์ผ์ผ ํ๋ ์ค์ ๋๋ robots.txt ์ง์ฐ ์ด์์ผ๋ก ์ต๋๊ฐ์ ์ ํ์ธ์.<br/>
* `concurrent_requests_per_domain`์ ๋๋ฉ์ธ์ ๋์ ์์ฒญ ์๋ฅผ ์ ํํ๋ฉด์ AutoThrottle์ ๋ชฉํ๊ฐ ๋ฉ๋๋ค. ๋ณดํต ๋ชฉํ๋ฅผ ๋ฐ๋ก ์ค์ ํ ํ์๋ ์์ต๋๋ค. ์ด ๊ฐ์ ์ค์ ํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ์์ฒญ์ด ๋๋ฉ์ธ ์ง์ฐ์ ๊ธฐ๋ค๋ฆฌ๋ ๋์์๋ ์ ์ฒด ์ ํ์ ํ ์๋ฆฌ๋ฅผ ์ฐจ์งํ๊ธฐ ๋๋ฌธ์
๋๋ค. ๋๋ฉ์ธ๋ณ ์ ํ์ด ์์ผ๋ฉด ๋๊ธฐ ์ค์ธ ์์ฒญ๋ค์ด ๋ค๋ฅธ ๋๋ฉ์ธ๊ณผ ๊ณต์ ํ๋ ์๋ฆฌ๋ฅผ ์ฌ์ฉํฉ๋๋ค.<br/>
* ์ธก์ ์๊ฐ์๋ ๋ด๋ถ ์ฌ์๋๋ฅผ ํฌํจํ ์ ์ฒด ๊ฐ์ ธ์ค๊ธฐ ์๊ฐ์ด ๋ค์ด๊ฐ๋๋ค. ๋ธ๋ผ์ฐ์ ์ธ์
์์๋ ํ์ด์ง ๋ ๋๋ง ์๊ฐ๋ ํฌํจํฉ๋๋ค.<br/>
* `autothrottle_max_delay`๋ `Retry-After`๋ฅผ ํฌํจํ ๋ชจ๋ ์ง์ฐ์ ์ ํํฉ๋๋ค. ์ฌ์ดํธ๊ฐ ๋ ์ค๋ ๊ธฐ๋ค๋ฆฌ๋ผ๊ณ ํ๋ฉด `autothrottle_max_delay`๋ฅผ ๋๋ฆฌ์ธ์.<br/>
* ๊ณ์ฐ๋ ์ง์ฐ์ ์ฒดํฌํฌ์ธํธ์ ์ ์ฅํ์ง ์์ต๋๋ค. ์ผ์ ์ค์ง ํ ์ฌ๊ฐํ๋ฉด ๊ฐ ๋๋ฉ์ธ์ ๋ค์ `autothrottle_start_delay`๋ถํฐ ์์ํฉ๋๋ค.
uvloop ์ฌ์ฉํ๊ธฐ {#using-uvloop}
start()์ use_uvloop ์ธ์๋ก ๋ ๋น ๋ฅธ uvloop/winloop ์ด๋ฒคํธ ๋ฃจํ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ํด๋น ํจํค์ง๊ฐ ์ค์น๋์ด ์์ด์ผ ํฉ๋๋ค.
result = MySpider().start(use_uvloop=True)
์
์ถ๋ ฅ์ด ๋ง์ ํฌ๋กค๋ง์ ์ฒ๋ฆฌ๋์ ๋์ผ ์ ์์ต๋๋ค. uvloop(Linux/macOS) ๋๋ winloop(Windows)๋ฅผ ๋ณ๋๋ก ์ค์นํ์ธ์.
์ผ์ ์ค์ง์ ์ฌ๊ฐ {#pause-resume}
์คํ์ด๋๋ ์ฒดํฌํฌ์ธํธ๋ก ์์ ํ๊ฒ ์ผ์ ์ค์งํ๊ณ ์ฌ๊ฐํ ์ ์์ต๋๋ค. ์ฌ์ฉํ๋ ค๋ฉด ์์ฑ์์ crawldir ๋๋ ํฐ๋ฆฌ๋ฅผ ์ ๋ฌํ์ธ์.
spider = MySpider(crawldir="crawl_data/my_spider")
result = spider.start()
if result.paused:
print("Crawl was paused. Run again to resume.")
else:
print("Crawl completed!")
๋์ ๋ฐฉ์ {#how-it-works}
- ์ผ์ ์ค์ง: ํฌ๋กค๋ง ์ค
Ctrl+C๋ฅผ ๋๋ฅด์ธ์. ์คํ ์ค์ธ ์์ฒญ์ด ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ ๋ค, ๋๊ธฐ ์์ฒญ๊ณผ ์ด๋ฏธ ํ์ธํ ์์ฒญ ์ง๋ฌธ ์งํฉ์ ์ฒดํฌํฌ์ธํธ๋ก ์ ์ฅํ๊ณ ์ข ๋ฃํฉ๋๋ค. - ๊ฐ์ ์ค์ง:
Ctrl+C๋ฅผ ๋ค์ ๋๋ฅด๋ฉด ์คํ ์ค์ธ ์์ ์ ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๋ฐ๋ก ๋ฉ์ถฅ๋๋ค. - ์ฌ๊ฐ: ๊ฐ์
crawldir๋ก ๋ค์ ์คํํ์ธ์. ์ฒดํฌํฌ์ธํธ๋ฅผ ์ฐพ์ ํ์ ์ง๋ฌธ ์งํฉ์ ๋ณต์ํ๊ณ ,start_requests()๋ฅผ ๊ฑด๋๋ฐ์ด ์ค๋จํ ๊ณณ๋ถํฐ ๊ณ์ํฉ๋๋ค. - ์ ๋ฆฌ: ์ผ์ ์ค์ง๊ฐ ์๋๋ผ ํฌ๋กค๋ง์ด ์ ์์ ์ผ๋ก ๋๋๋ฉด ์ฒดํฌํฌ์ธํธ ํ์ผ์ ์๋์ผ๋ก ์ญ์ ํฉ๋๋ค.
ํฌ๋กค๋ง ์ค์๋ ์ฒดํฌํฌ์ธํธ๋ฅผ ์ฃผ๊ธฐ์ ์ผ๋ก ์ ์ฅํฉ๋๋ค. ๊ธฐ๋ณธ ๊ฐ๊ฒฉ์ 5๋ถ์ ๋๋ค.
๊ฐ๊ฒฉ์ ๋ค์๊ณผ ๊ฐ์ด ๋ฐ๊ฟ ์ ์์ต๋๋ค.
# Save checkpoint every 2 minutes
spider = MySpider(crawldir="crawl_data/my_spider", interval=120.0)
์ฒดํฌํฌ์ธํธ๋ฅผ ์ธ ๋๋ ์์ ํ์ผ์ ์ฌ์ฉํ๊ณ , ์ฐ๊ธฐ์ ์ฑ๊ณตํ ๊ฒฝ์ฐ์๋ง ์ด์ ์ฒดํฌํฌ์ธํธ๋ฅผ ๊ต์ฒดํฉ๋๋ค.
!!! warning "์ฒดํฌํฌ์ธํธ ๋ฐ์ดํฐ"
`crawldir`๋ฅผ ์ฌ์ฉํ๋ฉด `meta`์ ์์ฒญ ์ธ์๋ฅผ ํฌํจํ ๋๊ธฐ ์์ฒญ์ ๊ฐ์ Python์ `pickle` ๋ชจ๋๋ก ์ ์ฅํ ์ ์์ด์ผ ํฉ๋๋ค. ์ ์ฅํ ์ ์๋ ๊ฐ์ด ์์ผ๋ฉด ์ฒดํฌํฌ์ธํธ ์ ์ฅ์ ์คํจํ๊ณ ํฌ๋กค๋ง์ด ์ค์ง๋ฉ๋๋ค. `Selector`๋ ์ฝ๋ฐฑ์์ ์ฌ์ฉํ ์ ์์ง๋ง ์ด ๋ฐฉ์์ผ๋ก ์ ์ฅํ ์๋ ์์ต๋๋ค.
`Selector` ๊ฐ์ฒด ๋์ ๋ฌธ์์ด์ด๋ ์ซ์์ฒ๋ผ ์ถ์ถํ ๊ฐ์ ์ ์ฅํ์ธ์. ๋ฆฌ์คํธ์ ๋์
๋๋ฆฌ์๋ ์ ์ฅํ ์ ์๋ ๊ฐ๋ง ๋ด์์ผ ํฉ๋๋ค.
```python
yield response.follow("/details", meta={"title": str(response.css("h1::text").get(""))})
```
!!! tip
`start()`๋ฅผ ์ฌ์ฉํ ๋ `Ctrl+C`๋ฅผ ๋๋ฅด๋ฉด ์ฒดํฌํฌ์ธํธ๋ฅผ ์ผ์ง ์์๋ ์งํ ์ค์ธ ์์
์ ๋ง์น ๋ค ์ข
๋ฃํฉ๋๋ค. ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๋ค์ ๋๋ฅด๋ฉด ์ฆ์ ๊ฐ์ ๋ก ์ข
๋ฃํฉ๋๋ค.
์ฌ๊ฐ ์ค์ธ์ง ํ์ธํ๊ธฐ {#knowing-if-youre-resuming}
on_start() ํ
์ resuming ํ๋๊ทธ๋ฅผ ๋ฐ์ต๋๋ค.
async def on_start(self, resuming: bool = False):
if resuming:
self.logger.info("Resuming from checkpoint!")
else:
self.logger.info("Starting fresh crawl")
๊ฐ๋ฐ ๋ชจ๋ {#development-mode}
parse()๋ฅผ ์์ ํ ๋๋ง๋ค ์๋ฒ์ ์์ฒญํ๋ฉด ๋๋ฆฌ๊ณ ๋ถํ์ํ ๋ถํ๊ฐ ์๊น๋๋ค. ๊ฐ๋ฐ ๋ชจ๋๋ ์ฒซ ์คํ์์ ์๋ต์ ๋์คํฌ์ ์บ์ํ๊ณ , ๋ค์ ์คํ์์๋ ์ ์ฅ๋ ์๋ต์ ์ฌ์ฉํฉ๋๋ค. ๊ฐ์ ํ์ด์ง๋ฅผ ๋ค์ ๊ฐ์ ธ์ค์ง ์๊ณ ์ ํ์๋ฅผ ๋ฐ๊พธ๊ณ ์คํ์ด๋๋ฅผ ๋ฐ๋ณต ์คํํ ์ ์์ต๋๋ค.
์คํ์ด๋์ development_mode = True๋ฅผ ์ค์ ํ์ธ์.
class MySpider(Spider):
name = "my_spider"
start_urls = ["https://example.com"]
development_mode = True
async def parse(self, response: Response):
yield {"title": response.css("title::text").get("")}
์ฒซ ์คํ์ ์ ์์ ์ผ๋ก ํ์ด์ง๋ฅผ ๊ฐ์ ธ์ ์๋ต์ ์ ์ฅํฉ๋๋ค. ๋ค์ ์คํ๋ถํฐ ๊ฐ์ ์์ฒญ์ ํ์ด์ง๋ฅผ ๊ฐ์ ธ์ค์ง ์๊ณ ์บ์์์ ์ฒ๋ฆฌํฉ๋๋ค. robots_txt_obey=True์ด๋ฉด ์บ์๋ฅผ ์ฐพ๊ธฐ ์ ์ robots.txt๋ฅผ ํ์ธํฉ๋๋ค.
์บ์ ์์น {#cache-location}
๊ธฐ๋ณธ ์์น๋ ํ์ฌ ์์
๋๋ ํฐ๋ฆฌ ์๋์ .scrapling_cache/{spider.name}/์
๋๋ค. ์คํ์ด๋๋ฅผ ์คํํ ๋๋ ํฐ๋ฆฌ์ด๋ฉฐ, ์คํฌ๋ฆฝํธ ํ์ผ์ด ์๋ ์์น๊ฐ ์๋๋๋ค. development_cache_dir๋ก ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
class MySpider(Spider):
name = "my_spider"
start_urls = ["https://example.com"]
development_mode = True
development_cache_dir = "/tmp/my_spider_cache"
๋์ ๋ฐฉ์ {#how-it-works_1}
- ์บ์ ํค: ์์ฒญ ์ง๋ฌธ์ ์ฌ์ฉํฉ๋๋ค. ์ง๋ฌธ์ ์ํฅ์ ์ฃผ๋ ์์ฑ(
fp_include_kwargs,fp_include_headers,fp_keep_fragments)์ ๋ฐ๊พธ๋ฉด ์๋ก ๊ฐ์ ธ์ต๋๋ค. - ์ ์ฅ ํ์: ์๋ต๋ง๋ค
{fingerprint_hex}.jsonํ์ผ ํ๋๋ฅผ ๋ง๋ญ๋๋ค. ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก ๋ณด๊ดํ๋๋ก ๋ณธ๋ฌธ์ base64๋ก ์ธ์ฝ๋ฉํฉ๋๋ค. ์์ ํ์ผ์ ๋ง๋ ๋ค ์ด๋ฆ์ ๋ฐ๊พธ๋ ์์์ ์ฐ๊ธฐ๋ฅผ ์ฌ์ฉํฉ๋๋ค. - ์บ์ ์ฌ์ฉ: ์บ์์ ์์ผ๋ฉด ํ์ด์ง ๊ฐ์ ธ์ค๊ธฐ์ ํจ๊ป
download_delay, ์์ฒญ ๋น๋ ์ ํ,is_blocked()์ฌ์๋ ๊ณผ์ ์ ๊ฑด๋๋๋๋ค. ์บ์๋ ์๋ต์ ์ฝ๋ฐฑ์ผ๋ก ๋ฐ๋ก ์ ๋ฌํฉ๋๋ค. - ํต๊ณ: ์บ์ ์์ฒญ๋
requests_count,response_bytes, ์ํ ์ฝ๋๋ณ ํ์์ ํฌํจ๋๋ฏ๋ก ์ผ๋ฐ ํฌ๋กค๋ง๊ณผ ๊ฐ์ ํต๊ณ๋ฅผ ๋ณผ ์ ์์ต๋๋ค.cache_hits์cache_misses๋ก ์บ์ ์ฌ์ฉ ๊ฒฐ๊ณผ๋ ํ์ธํ ์ ์์ต๋๋ค.
์บ์ ์ง์ฐ๊ธฐ {#clearing-the-cache}
์บ์๋ ์๋์ผ๋ก ๋ง๋ฃ๋์ง ์์ต๋๋ค. ์๋ก ํฌ๋กค๋งํ๋ ค๋ฉด ์บ์ ๋๋ ํฐ๋ฆฌ๋ฅผ ์ญ์ ํ๊ฑฐ๋ ๊ด๋ฆฌ์์ clear()๋ฅผ ์ง์ ํธ์ถํ์ธ์.
!!! warning
๊ฐ๋ฐ ๋ชจ๋๋ ๊ฐ๋ฐ ์ ์ฉ์
๋๋ค. ์บ์ ์๋ต์ ๋ง๋ฃ๋์ง ์๊ณ , ์บ์ ์ฌ์ฉ ์ ์์ฒญ ๋น๋ ์ ํ๊ณผ ์ฐจ๋จ ์์ฒญ ์ฌ์๋๋ฅผ ๊ฑด๋๋๋๋ค. ์ด์ ํ๊ฒฝ์์๋ `development_mode = True`๋ฅผ ์ฌ์ฉํ์ง ๋ง์ธ์.
์คํธ๋ฆฌ๋ฐ {#streaming}
์ค๋ ์คํํ๋ ์คํ์ด๋๋ ์์งํ ํญ๋ชฉ์ ์ฆ์ ์ฌ์ฉํ๋ ์ฑ์์๋ start() ๋์ stream()์ ์ฌ์ฉํ์ธ์.
import anyio
async def main():
spider = MySpider()
async for item in spider.stream():
print(f"Got item: {item}")
# Access real-time stats
print(f"Items so far: {spider.stats.items_scraped}")
print(f"Requests made: {spider.stats.requests_count}")
anyio.run(main)
start()์์ ์ฃผ์ ์ฐจ์ด๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
stream()์ ๋น๋๊ธฐ ์ปจํ ์คํธ์์ ํธ์ถํด์ผ ํฉ๋๋ค.- ํญ๋ชฉ์ ๋ฆฌ์คํธ์ ๋ชจ์ผ์ง ์๊ณ ์์งํ ๋๋ง๋ค ํ๋์ฉ ๋ด๋ณด๋ ๋๋ค.
- ๋ฐ๋ณต ์ค์
spider.stats๋ก ์ค์๊ฐ ํต๊ณ๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
!!! abstract
`spider.stats`์์ ํ์ธํ ์ ์๋ ์ ์ฒด ํต๊ณ๋ [์๋](#results-statistics)์ ์ค๋ช
๋์ด ์์ต๋๋ค.
์ฒดํฌํฌ์ธํธ์ ํจ๊ป ์ฌ์ฉํ ์๋ ์์ต๋๋ค. ์ค์๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ฌ์ฃผ๋ฉด์ ์ผ์ ์ค์ง์ ์ฌ๊ฐ๋ฅผ ์ง์ํ๋ UI๋ฅผ ์ฝ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค.
import anyio
async def main():
spider = MySpider(crawldir="crawl_data/my_spider")
async for item in spider.stream():
print(f"Got item: {item}")
# Access real-time stats
print(f"Items so far: {spider.stats.items_scraped}")
print(f"Requests made: {spider.stats.requests_count}")
anyio.run(main)
์ ์ฝ๋์์ spider.pause()๋ก ์คํ์ด๋๋ฅผ ์ข
๋ฃํ ์๋ ์์ต๋๋ค. ์ฒดํฌํฌ์ธํธ๋ฅผ ์ผ์ง ์์๋ค๋ฉด ํฌ๋กค๋ง๋ง ์ข
๋ฃํฉ๋๋ค.
์๋ช ์ฃผ๊ธฐ ํ {#lifecycle-hooks}
์ฌ๋ฌ ๋จ๊ณ์์ ์ํ๋ ๋์์ ์ถ๊ฐํ ์ ์๋๋ก ์ฌ์ ์ ๊ฐ๋ฅํ ํ ์ ์ ๊ณตํฉ๋๋ค.
on_start {#on_start}
ํฌ๋กค๋ง ์ ์ ํธ์ถํฉ๋๋ค. ๋ฐ์ดํฐ ๋ก๋๋ ์์ ์ด๊ธฐํ ๊ฐ์ ์ค๋น ์์ ์ ์ฌ์ฉํ์ธ์.
async def on_start(self, resuming: bool = False):
self.logger.info("Spider starting up")
# Load seed URLs from a database, initialize counters, etc.
on_close {#on_close}
ํฌ๋กค๋ง์ด ์๋ฃ๋๊ฑฐ๋ ์ผ์ ์ค์ง๋ ๋ค ํธ์ถํฉ๋๋ค. ์์ ์ ๋ฆฌ์ ์ฌ์ฉํ์ธ์.
async def on_close(self):
self.logger.info("Spider shutting down")
# Close database connections, flush buffers, etc.
on_error {#on_error}
์์ฒญ์์ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ํธ์ถํฉ๋๋ค. ์ค๋ฅ ๊ธฐ๋ก์ด๋ ๋ณต๊ตฌ ์์ ์ ์ฌ์ฉํ์ธ์.
async def on_error(self, request: Request, error: Exception):
self.logger.error(f"Failed: {request.url} - {error}")
# Log to error tracker, save failed URL for later, etc.
on_scraped_item {#on_scraped_item}
์์งํ ๊ฐ ํญ๋ชฉ์ ๊ฒฐ๊ณผ์ ๋ฃ๊ธฐ ์ ์ ํธ์ถํฉ๋๋ค. ์ ์งํ๋ ค๋ฉด ์์ ์ฌ๋ถ์ ๊ด๊ณ์์ด ํญ๋ชฉ์ ๋ฐํํ๊ณ , ์ ์ธํ๋ ค๋ฉด None์ ๋ฐํํ์ธ์.
async def on_scraped_item(self, item: dict) -> dict | None:
# Drop items without a title
if not item.get("title"):
return None
# Modify items (e.g., add timestamps)
item["scraped_at"] = "2026-01-01"
return item
!!! tip
์ด ํ
์ผ๋ก ํญ๋ชฉ์ ์ง์ ๋ง๋ ์ฒ๋ฆฌ ํ์ดํ๋ผ์ธ์ ์ ๋ฌํ ๋ค ์คํ์ด๋ ๊ฒฐ๊ณผ์์ ์ ์ธํ ์๋ ์์ต๋๋ค.
start_requests {#start_requests}
start_urls ๋์ ์ํ๋ ๋ฐฉ์์ผ๋ก ์์ ์์ฒญ์ ๋ง๋ค๋ ค๋ฉด start_requests()๋ฅผ ์ฌ์ ์ํ์ธ์.
async def start_requests(self):
# POST request to log in first
yield Request(
"https://example.com/login",
method="POST",
data={"user": "admin", "pass": "secret"},
callback=self.after_login,
)
async def after_login(self, response: Response):
# Now crawl the authenticated pages
yield response.follow("/dashboard", callback=self.parse)
๊ฒฐ๊ณผ์ ํต๊ณ {#results-statistics}
start()๊ฐ ๋ฐํํ๋ CrawlResult์๋ ์์งํ ํญ๋ชฉ๊ณผ ์์ธํ ํต๊ณ๊ฐ ์์ต๋๋ค.
result = MySpider().start()
# Items
print(f"Total items: {len(result.items)}")
result.items.to_json("output.json", indent=True)
# Did the crawl complete?
print(f"Completed: {result.completed}")
print(f"Paused: {result.paused}")
# Statistics
stats = result.stats
print(f"Requests: {stats.requests_count}")
print(f"Failed: {stats.failed_requests_count}")
print(f"Blocked: {stats.blocked_requests_count}")
print(f"Offsite filtered: {stats.offsite_requests_count}")
print(f"Robots.txt disallowed: {stats.robots_disallowed_count}")
print(f"Cache hits: {stats.cache_hits}")
print(f"Cache misses: {stats.cache_misses}")
print(f"Items scraped: {stats.items_scraped}")
print(f"Items dropped: {stats.items_dropped}")
print(f"Response bytes: {stats.response_bytes}")
print(f"Duration: {stats.elapsed_seconds:.1f}s")
print(f"Speed: {stats.requests_per_second:.1f} req/s")
์์ธํ ํต๊ณ {#detailed-stats}
CrawlStats๋ ๋ค์๊ณผ ๊ฐ์ ์ธ๋ถ ์ ๋ณด๋ฅผ ๊ธฐ๋กํฉ๋๋ค.
stats = result.stats
# Status code distribution
print(stats.response_status_count)
# {'status_200': 150, 'status_404': 3, 'status_403': 1}
# Bytes downloaded per domain
print(stats.domains_response_bytes)
# {'example.com': 1234567, 'api.example.com': 45678}
# Requests per session
print(stats.sessions_requests_count)
# {'http': 120, 'stealth': 34}
# Proxies used during the crawl
print(stats.proxies)
# ['http://proxy1:8080', 'http://proxy2:8080']
# Log level counts
print(stats.log_levels_counter)
# {'debug': 200, 'info': 50, 'warning': 3, 'error': 1, 'critical': 0}
# Timing information
print(stats.start_time) # Unix timestamp when crawl started
print(stats.end_time) # Unix timestamp when crawl finished
print(stats.download_delay) # The download delay used (seconds)
# Concurrency settings used
print(stats.concurrent_requests) # Global concurrency limit
print(stats.concurrent_requests_per_domain) # Per-domain concurrency limit
# AutoThrottle
print(stats.autothrottle_enabled) # Whether the adaptive delay was on
print(stats.autothrottle_delays) # Final delay per domain, {'example.com': 0.62}
# Custom stats (set by your spider code)
print(stats.custom_stats)
# {'login_attempts': 3, 'pages_with_errors': 5}
# Export everything as a dict
print(stats.to_dict())
๋ก๊น {#logging}
์คํ์ด๋์ ๊ธฐ๋ณธ ๋ก๊ฑฐ๋ self.logger๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค. ์คํ์ด๋ ์ด๋ฆ์ด ๋ฏธ๋ฆฌ ์ค์ ๋์ด ์๊ณ ๋ค์ ํญ๋ชฉ์ ๋ฐ๊ฟ ์ ์์ต๋๋ค.
| ์์ฑ | ๊ธฐ๋ณธ๊ฐ | ์ค๋ช |
|---|---|---|
logging_level |
logging.DEBUG |
๊ธฐ๋กํ ์ต์ ๋ก๊ทธ ์์ค |
logging_format |
"[%(asctime)s]:({spider_name}) %(levelname)s: %(message)s" |
๋ก๊ทธ ๋ฉ์์ง ํ์ |
logging_date_format |
"%Y-%m-%d %H:%M:%S" |
๋ก๊ทธ ๋ฉ์์ง์ ๋ ์ง ํ์ |
log_file |
None |
์ฝ์ ์ถ๋ ฅ๊ณผ ํจ๊ป ์ฌ์ฉํ ๋ก๊ทธ ํ์ผ ๊ฒฝ๋ก |
import logging
class MySpider(Spider):
name = "my_spider"
start_urls = ["https://example.com"]
logging_level = logging.INFO
log_file = "logs/my_spider.log"
async def parse(self, response: Response):
self.logger.info(f"Processing {response.url}")
yield {"title": response.css("title::text").get("")}
๋ก๊ทธ ํ์ผ์ ๋๋ ํฐ๋ฆฌ๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ๋ง๋ญ๋๋ค. ์ฝ์๊ณผ ํ์ผ์ ๊ฐ์ ์ถ๋ ฅ ํ์์ ์ฌ์ฉํฉ๋๋ค.