๐ท๏ธ 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: "์น ์คํฌ๋ํ์ HTTP ์์ฒญ๊ณผ ๋ธ๋ผ์ฐ์ ์ค ๋ฌด์์ ์ธ๊น์?" description: "์์ ์ ๋ง๋ Fetcher ๋๋ StealthyFetcher๋ฅผ ์ ํํ์ธ์. ์ง์ HTTP ์์ฒญ, JavaScript ์คํ, ์ธ์ ์ฌ์ฌ์ฉ์ด ํ์ํ ๊ฒฝ์ฐ๋ฅผ ์์๋ด ๋๋ค."
Fetcher ๊ธฐ์ด {#fetchers-basics}
์๊ฐ {#introduction}
Fetcher๋ ํ ์ค๋ก ์์ฒญ์ ๋ณด๋ด๊ฑฐ๋ ํ์ด์ง๋ฅผ ๊ฐ์ ธ์ค๊ณ Response ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ํด๋์ค์ ๋๋ค. ๋ค์ํ ๊ธฐ๋ฅ๋ ์ ๊ณตํฉ๋๋ค. v0.3๋ถํฐ ๋ชจ๋ Fetcher์๋ ์ธ์ ์ ์ ์งํ๋ ๋ณ๋ ํด๋์ค๊ฐ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๋ธ๋ผ์ฐ์ Fetcher๋ ์์ฒญ๋ง๋ค ์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ฌ๋ ๋์ ๋ชจ๋ ์์ฒญ์ด ๋๋ ๋๊น์ง ๊ฐ์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ด์ด๋ ์ ์์ต๋๋ค. ์์ ์ ๋ง๊ฒ ์ ํํ๋ฉด ๋ฉ๋๋ค.
v0.2 ์ด์ ์ Scrapling์ ํ์ฑ๋ง ์ ๊ณตํ์ต๋๋ค. ์น ์คํฌ๋ํ์ ํ์ํ ๋ชจ๋ ๊ธฐ๋ฅ์ ํ๊ณณ์์ ์ ๊ณตํ๊ธฐ ์ํด ์ด ๊ธฐ๋ฅ๋ค์ ์ถ๊ฐํ์ต๋๋ค.
Fetcher๋ ๋ค๋ฅธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋จ์ํ ๊ฐ์ผ ๋๊ตฌ๊ฐ ์๋๋๋ค. ๋ค๋ฅธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์์ฒญ๊ณผ ํ์ด์ง ๋ก๋ฉ์ ์ํ ์์ง์ผ๋ก ์ฌ์ฉํฉ๋๋ค. ๊ฐ Fetcher๋ ํด๋น ์์ง์ ์๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ฉด์ ์์ง์ ๊ธฐ๋ฅ์ ํ์ฉํ๊ณ ์น ์คํฌ๋ํ์ ๋ง๊ฒ ์ต์ ํํฉ๋๋ค.
Fetcher ๋น๊ต {#fetchers-overview}
Scrapling์ HTTP์ฉ Fetcher์ AsyncFetcher, ๋ธ๋ผ์ฐ์ ์ฉ StealthyFetcher, ๊ทธ๋ฆฌ๊ณ ๋ฐ๋ณต ์์ฒญ์ ์ฐ๋ ์ธ์
ํด๋์ค๋ฅผ ์ ๊ณตํฉ๋๋ค.
๋ค์ ํ๋ฅผ ๋ณด๋ฉด ์์ ์ ๋ง๋ Fetcher๋ฅผ ๋น ๋ฅด๊ฒ ์ ํํ ์ ์์ต๋๋ค.
| ๊ธฐ๋ฅ | Fetcher | StealthyFetcher |
|---|---|---|
| ์๋์ ์ธ ์๋ | ๐๐๐๐๐ | ๐๐๐ |
| ํ์ง ํํผ | โญโญ | โญโญโญโญโญ |
| ๋ด ์ฐจ๋จ ์ฐํ ์ต์ | โญโญ | โญโญโญโญโญ |
| JavaScript ๋ก๋ฉ | โ | โ |
| ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ | โญ | โญโญโญ |
| ์ ํฉํ ์์ | HTTP ์์ฒญ๋ง์ผ๋ก ๊ฐ๋ฅํ ๊ธฐ๋ณธ์ ์ธ ์คํฌ๋ํ | - ๋์ ์ผ๋ก ๋ก๋๋๋ ์ฌ์ดํธ - ๊ฐ๋จํ ์๋ํ - ๋จ์ํ ๋ณดํธ๋ถํฐ ๋ณต์กํ ๋ณดํธ๊น์ง ์ฐํ |
| ๋ธ๋ผ์ฐ์ | โ | Chromium๊ณผ Google Chrome |
| ์ฌ์ฉํ๋ ๋ธ๋ผ์ฐ์ API | โ | Playwright |
| ์ค์ ๋์ด๋ | ์ฌ์ | ์ฌ์ |
๋ค์ ํ์ด์ง๋ถํฐ ๊ฐ๊ฐ ์์ธํ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
๋ชจ๋ Fetcher์ ํ์ ์ค์ {#parser-configuration-in-all-fetchers}
๋ชจ๋ Fetcher๋ ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๊ฐ์ ธ์ต๋๋ค.
from scrapling.fetchers import Fetcher, AsyncFetcher, StealthyFetcher
๊ฐ์ฒด๋ฅผ ๋ง๋ค์ง ์๊ณ ๋ฐ๋ก ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ํ์์ ๊ธฐ๋ณธ ์ค์ ์ ์ฌ์ฉํฉ๋๋ค.
page = StealthyFetcher.fetch('https://example.com')
์๋ต์ ๋ฐํํ๊ธฐ ์ ์ ์ ์ฉํ ํ์(Selector ํด๋์ค)๋ฅผ ์ค์ ํ๋ ค๋ฉด ๋จผ์ ๋ค์์ ์คํํ์ธ์.
from scrapling.fetchers import Fetcher
Fetcher.configure(adaptive=True, keep_comments=False) # and the rest
๋๋ ๋ค์์ฒ๋ผ ์์ฑํฉ๋๋ค.
from scrapling.fetchers import Fetcher
Fetcher.adaptive=True
Fetcher.keep_comments=False
๊ทธ๋ฐ ๋ค์ ํ์์ฒ๋ผ ์ฝ๋๋ฅผ ์ด์ด์ ์์ฑํ๋ฉด ๋ฉ๋๋ค.
์ค์ ํ ์ ์๋ ์ธ์๋ adaptive, adaptive_domain, huge_tree, keep_comments, storage, storage_args์
๋๋ค. Selector ํด๋์ค์ ์ ๋ฌํ๋ ๊ฒ๊ณผ ๊ฐ์ต๋๋ค. <fetcher_class>.display_config()๋ก ํ์ฌ ์ค์ ์ ์ธ์ ๋ ํ์ธํ ์ ์์ต๋๋ค.
!!! info "์ ๋ณด"
`adaptive`๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๊บผ์ ธ ์์ต๋๋ค. ์ ์ํ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ค๋ฉด ์ผ์ผ ํฉ๋๋ค.
์์ฒญ๋ณ ํ์ ์ค์ {#set-parser-config-per-request}
์์ ํ์ ์ค์ ์ ํด๋น ํด๋์ค๋ฅผ ํตํด ๋ณด๋ด๋ ๋ชจ๋ ์์ฒญ์ ์ ์ฉ๋ฉ๋๋ค. ๊ณตํต ์ค์ ์ ๊ฐ๋จํ ์ง์ ํ ๋ ์ฌ์ฉํฉ๋๋ค.
์์ฒญ๋ง๋ค ๋ค๋ฅธ ์ค์ ์ด ํ์ํ๋ฉด ์์ฒญ ๋ฉ์๋(fetch/get/post/...)์ selector_config ์ธ์์ ๋์
๋๋ฆฌ๋ฅผ ์ ๋ฌํ์ธ์.
Response ๊ฐ์ฒด {#response-object}
Response๋ Selector๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ฉฐ, ์๋ต ํค๋, ์ํ, ์ฟ ํค ๋ฑ ์ถ๊ฐ ์ ๋ณด๋ ์ ๊ณตํฉ๋๋ค.
from scrapling.fetchers import Fetcher
page = Fetcher.get('https://example.com')
page.status # HTTP status code
page.reason # Status message
page.cookies # Response cookies as a dictionary
page.headers # Response headers
page.request_headers # Request headers
page.history # Response history of redirections, if any
page.body # Raw response body as bytes
page.encoding # Response encoding
page.meta # Response metadata dictionary (e.g., proxy used). Mainly helpful with the spiders system.
๋ชจ๋ Fetcher๋ Response ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. record_requests=True๋ก ๋ง๋ ๋ธ๋ผ์ฐ์ ์ธ์
์์๋ session.network.search(limit=None)๊ณผ session.network.get(request_id)๋ ์ ์ฅ๋ Response ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. ์ ์ฅ๋ API ์๋ต๊ณผ v5๋ก์ ์ด์ ์ ๋คํธ์ํฌ ๊ธฐ๋ก์ ์ฐธ๊ณ ํ์ธ์.
Response ๊ฐ์ฒด๋ ํ ์ค๋ก ํ์ด์ง๋ฅผ ์ ๋ฆฌ๋ LLM์ฉ Markdown์ผ๋ก ๋ฐ๊ฟ ์๋ ์์ต๋๋ค.
markdown = Fetcher.get('https://example.com').markdown(main_content_only=True)
๋ณํ ์ ์ ์คํฌ๋ฆฝํธ, ์คํ์ผ, ์จ๊ฒจ์ง ๋ด์ฉ๊ณผ ํ๋กฌํํธ ์ธ์ ์
๋ด์ฉ์ ํญ์ ์ ๊ฑฐํฉ๋๋ค. css_selector๋ก ํน์ ์์๋ง ๋ณํํ ์๋ ์์ต๋๋ค. rag ์ถ๊ฐ ํจํค์ง๊ฐ ํ์ํ๋ฉฐ ai/shell/all์๋ ํฌํจ๋ฉ๋๋ค. ์์ธํ ์ค๋ช
์ RAG ์์คํ
๋ง๋ค๊ธฐ๋ฅผ ์ฐธ๊ณ ํ์ธ์.
!!! note "์ฐธ๊ณ "
v0.4๋ถํฐ `Response`์ ๋ณธ๋ฌธ์ [Selector](../parsing/main_classes.md#selector)์ ๋ฌ๋ฆฌ ํญ์ ๋ฐ์ดํธ์
๋๋ค.