๐ท๏ธ 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: "Scrapling์ผ๋ก ์น์ฌ์ดํธ๋ฅผ ํฌ๋กค๋งํด RAG์ฉ Markdown ๋ง๋ค๊ธฐ" description: "ํ์ด์ง ์๋ต์ Markdown์ผ๋ก ๋ฐ๊พธ๊ฑฐ๋ ContentSpider๋ก ์ฌ์ดํธ๋ฅผ ์์งํ์ธ์. RAG ์์คํ ์์ ๋๋๊ณ ์ธ๋ฑ์ฑํ ๋ฌธ์ ๋ชจ์์ ์ค๋นํฉ๋๋ค."
Scrapling์ผ๋ก RAG ์์คํ ๋ง๋ค๊ธฐ {#building-rag-systems-with-scrapling}
RAG ํ์ดํ๋ผ์ธ์ ํ์ง์ ์ ๋ ฅํ๋ ํ ์คํธ์ ๋ฌ๋ ค ์์ต๋๋ค. ์๋ณธ HTML์๋ ๋งํฌ์ , ํ์ ๋ฉ๋ด, ์คํฌ๋ฆฝํธ, ์ถ์ ์ฝ๋ ๊ฐ์ ๋ถํ์ํ ๋ด์ฉ์ด ์์ด ํ ํฐ์ ๋ญ๋นํฉ๋๋ค. ์จ๊ฒจ์ง ํ๋กฌํํธ ์ธ์ ์ ๋ด์ฉ์ด ๊ทธ๋๋ก LLM์ ์ ๋ฌ๋ ์๋ ์์ต๋๋ค. Scrapling์ LLM ์์ด ํ์ด์ง๋ ์ฌ์ดํธ ์ ์ฒด๋ฅผ ์ ๋ฆฌ๋ Markdown์ผ๋ก ๋ฐ๊ฟ๋๋ค. ๋ฐ๋ผ์ ์์ง์ด ๋น ๋ฅด๊ณ ํ์ด์ง๋ง๋ค LLM ๋น์ฉ์ด ๋ค์ง ์์ต๋๋ค.
์ค์น {#installation}
pip install "scrapling[rag]"
scrapling install
rag ์ถ๊ฐ ์์กด์ฑ ๊ทธ๋ฃน์ Markdown ๋ณํ์ ์ง์ํ๋ Fetcher๋ฅผ ์ค์นํฉ๋๋ค. ai, shell, all์๋ ํฌํจ๋์ด ์์ต๋๋ค. scrapling install ๋ช
๋ น์ ๋ธ๋ผ์ฐ์ ์์กด์ฑ์ ๋ค์ด๋ก๋ํฉ๋๋ค. ์ด ๋จ๊ณ๋ ๋ธ๋ผ์ฐ์ ๊ธฐ๋ฐ Fetcher์๋ง ํ์ํฉ๋๋ค.
ํ ํ์ด์ง๋ฅผ Markdown์ผ๋ก ๋ฐ๊พธ๊ธฐ {#one-page-to-markdown}
๋ชจ๋ Response์๋ markdown() ๋ฉ์๋๊ฐ ์์ต๋๋ค.
from scrapling.fetchers import Fetcher
markdown = Fetcher.get("https://example.com").markdown(main_content_only=True)
๋ชจ๋ Fetcher์์ ์๋ํ๋ฏ๋ก Cloudflare๋ก ๋ณดํธ๋ ํ์ด์ง๋ ํ ์ค๋ก ๋ณํํ ์ ์์ต๋๋ค.
from scrapling.fetchers import StealthyFetcher
markdown = StealthyFetcher.fetch("https://protected.example.com", solve_cloudflare=True).markdown(main_content_only=True)
์ถ๋ ฅ์ ๋ ์ธ์๋ก ์กฐ์ ํฉ๋๋ค.
main_content_only: ํ์ด์ง์<body>ํ๊ทธ ์์ ์๋ ๋ด์ฉ๋ง ๋ณํํฉ๋๋ค.css_selector: CSS ์ ํ์์ ์ผ์นํ๋ ์์๋ง ๋ณํํฉ๋๋ค. ์ผ์นํ๋ ๋ชจ๋ ๊ฒฐ๊ณผ๋ฅผ ํฉ์นฉ๋๋ค. ํ์ดํ๋ผ์ธ์ ํ์ํ ๋ถ๋ถ๋ง ์ถ์ถํด ํ ํฐ์ ์ค์ผ ์ ์์ต๋๋ค.
markdown = Fetcher.get("https://example.com/docs/page").markdown(css_selector="article")
์ด๋ค ์ธ์๋ฅผ ์ ๋ฌํ๋ ๋ณํ ์ ์ ์คํฌ๋ฆฝํธ, ์คํ์ผ, ์จ๊ฒจ์ง ๋ด์ฉ์ ํญ์ ์ ๊ฑฐํฉ๋๋ค. MCP ์๋ฒ๊ฐ AI ์์ด์ ํธ๋ฅผ ํ๋กฌํํธ ์ธ์ ์
์ผ๋ก๋ถํฐ ๋ณดํธํ ๋๋ ๊ฐ์ ์ ๋ฆฌ ๊ณผ์ ์ ์ฌ์ฉํฉ๋๋ค. ์ธ๋ผ์ธ ์คํ์ผ๋ก ์จ๊ธด ์์, aria-hidden="true" ์์, ์จ๊ฒจ์ง ์ฌ๋กฏ, <template> ํ๊ทธ, HTML ์ฃผ์, ๋๋น๊ฐ 0์ธ ๋ฌธ์๋ฅผ ๋ณํ ์ ์ ์ ๊ฑฐํฉ๋๋ค. MCP ์๋ฒ๋ main_content_only=True์ผ ๋ ์ด ์ ๋ฆฌ ๊ณผ์ ์ ์ฌ์ฉํฉ๋๋ค.
์ฌ์ดํธ ์ ์ฒด๋ฅผ Markdown ๋ฌธ์ ๋ชจ์์ผ๋ก ๋ง๋ค๊ธฐ {#a-whole-website-to-a-markdown-corpus}
ContentSpider ํ
ํ๋ฆฟ์ ์น์ฌ์ดํธ๋ฅผ ํฌ๋กค๋งํ๊ณ ๋ชจ๋ ํ์ด์ง๋ฅผ ๋ณํํฉ๋๋ค. Spider ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ๋์ ์ฒ๋ฆฌ, AutoThrottle, robots.txt ์ค์, ์ผ์ ์ ์ง์ ์ฌ๊ฐ ๊ธฐ๋ฅ๋ ์ด์ฉํ ์ ์์ต๋๋ค.
from scrapling.spiders import ContentSpider
class DocsSpider(ContentSpider):
name = "docs"
start_urls = ["https://example.com/docs/"]
allowed_domains = {"example.com"}
output_dir = "docs_markdown"
max_pages = 200
result = DocsSpider().start()
result.items.to_jsonl("docs.jsonl")
์์งํ ๊ฐ ํ์ด์ง๋ url, title, markdown ํค๊ฐ ์๋ ํญ๋ชฉ์ด ๋ฉ๋๋ค. output_dir๋ฅผ ์ค์ ํ๋ฉด ๊ฐ ํ์ด์ง๋ฅผ URL์ ๋ฐ๋ผ ์ด๋ฆ์ ์ ํ Markdown ํ์ผ๋ก๋ ์ ์ฅํฉ๋๋ค. ๋ฐ๋ผ์ ์ ์์ ๋ .md ํ์ผ์ด ๋ด๊ธด ํด๋์ ์์ง ์์คํ
์ ๋ฐ๋ก ๋ฃ์ ์ ์๋ docs.jsonl์ ํจ๊ป ๋ง๋ญ๋๋ค.
ํฌ๋กค๋ง์ด ๋์ ์ฌ์ดํธ๋ฅผ ๋ฒ์ด๋์ง ์๋๋ก ์ด ํ
ํ๋ฆฟ์๋ allowed_domains๊ฐ ํ์ํฉ๋๋ค. ์ฌ์ฉํ ์ ์๋ ์ต์
์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
css_selector/main_content_only: ๋ชจ๋ ํ์ด์ง์markdown()์ ์ ๋ฌํฉ๋๋ค.main_content_only๋ ๊ธฐ๋ณธ์ผ๋ก ์ผ์ ธ ์์ต๋๋ค.output_dir: ์ค์ ํ๋ฉด ํ์ด์ง๋ง๋ค Markdown ํ์ผ ํ๋๋ฅผ ์ ์ฅํฉ๋๋ค.max_pages: ๋ณํํ ํ์ด์ง ์์ ์ํ์ ๋๋ค. ์ํ์ ๋๋ฌํ์ ๋ ์ด๋ฏธ ํ์ ๋ค์ด๊ฐ ์์ฒญ์ ๊ฐ์ ธ์ฌ ์ ์์ง๋ง ๋ณํํ์ง๋ ์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ธ0์ ์ ํ์ ๋๋๋ค.
๊ธฐ๋ณธ์ผ๋ก allowed_domains ์์ ๋ชจ๋ ํ์ด์ง ๋งํฌ๋ฅผ ๋ฐ๋ผ๊ฐ๋๋ค. ์ด ํ
ํ๋ฆฟ์ CrawlSpider๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ฏ๋ก, rules()๋ฅผ ์ฌ์ ์ํ๊ณ ์ง์ ์ค์ ํ LinkExtractor๋ก ํฌ๋กค๋ง์ ์กฐ์ ํ ์ ์์ต๋๋ค. allow๋ก ์ํ๋ URL ํจํด๋ง ํ์ฉํ๊ณ , deny๋ก ๋ก๊ทธ์ธ ํ์ด์ง, ํ๊ทธ ๋ชฉ๋ก, ์ธ์์ฉ ํ์ด์ง ๊ฐ์ ๋ถํ์ํ ํจํด์ ์ ์ธํฉ๋๋ค.
from scrapling.spiders import CrawlRule, LinkExtractor, ContentSpider
class DocsSpider(ContentSpider):
name = "docs"
start_urls = ["https://example.com/"]
allowed_domains = {"example.com"}
def rules(self):
return [CrawlRule(LinkExtractor(allow=r"/docs/", deny=[r"/docs/changelog/", r"\?print="]))]
deny๊ฐ allow๋ณด๋ค ์ฐ์ ํฉ๋๋ค. ์ผ๋ฐ CrawlSpider์ฒ๋ผ ๊ท์น์ priority๋ process_request ํ
์ ์ง์ ํ ์๋ ์์ต๋๋ค.
๋ฒกํฐ ์ ์ฅ์์ ์ ๋ฌํ๊ธฐ {#feeding-a-vector-store}
JSONL ์ถ๋ ฅ์ ์ด๋ค ์๋ฒ ๋ฉ ํ์ดํ๋ผ์ธ์๋ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค. ๊ฐ๋จํ ์์ ์ ๋๋ค.
import json
with open("docs.jsonl", encoding="utf-8") as f:
for line in f:
page = json.loads(line)
for chunk in split_into_chunks(page["markdown"]):
vector_store.add(text=chunk, metadata={"url": page["url"], "title": page["title"]})
Spider์ css_selector๋ก ํ
์คํธ๋ฅผ ๋๋๊ธฐ ์ ์ ๋ฐ๋ณต๋๋ ๊ณตํต ๋ถ๋ถ์ ์ ๊ฑฐํ์ธ์. ๋ค ๋จ๊ณ์์ ๋ค์ ์ ๋ฆฌํ ํ์๊ฐ ์ค์ด๋ญ๋๋ค. ์๋ฒ ๋ฉ์ ๋ถํ์ํ ๋ด์ฉ์ด ์ ์์๋ก ๊ฒ์ ํ์ง์ด ์ข์์ง๋๋ค.
๋ํํ์ผ๋ก ์ฌ์ฉํ๊ธฐ {#interactive-alternatives}
ํ์ดํ๋ผ์ธ ๋์ ๋ํ๋ก ์คํฌ๋ํํ๋ ค๋ฉด MCP ์๋ฒ๋ฅผ ์ฌ์ฉํ์ธ์. ๊ฐ์ Markdown ์ถ์ถ ๊ธฐ๋ฅ์ AI ์ฑ๋ด์ ๋๊ตฌ๋ก ์ ๊ณตํฉ๋๋ค. Agent Skill์ ์ฝ๋ฉ ์์ด์ ํธ๊ฐ ์ด๋ฐ ์ฝ๋๋ฅผ ์ง์ ์์ฑํ๋๋ก ๋์ต๋๋ค.