π·οΈ 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: "λ§ν¬, μ¬μ΄νΈλ§΅, νΌλ, Markdownμ© μ€νμ΄λ ν νλ¦Ώ" description: "CrawlSpider, SitemapSpider, XMLFeedSpider, CSVFeedSpider, ContentSpider μ€μμ μ ννμΈμ. νμ΄μ§, νΌλ νλͺ©, Markdown λ¬Έμλ₯Ό μμ§ν κ·μΉμ μ€μ ν©λλ€."
μΌλ° μ€νμ΄λ ν νλ¦Ώ {#generic-spider-templates}
λλΆλΆμ ν¬λ‘€λ§μ νΉμ ν¨ν΄μ λ§ν¬λ₯Ό λ°λΌκ°κ±°λ μ¬μ΄νΈλ§΅μ μλ λͺ¨λ URLμ λ°©λ¬Έν©λλ€. Scraplingμ λ λ°©μμ ν
νλ¦Ώμ μ 곡νλ―λ‘ κ°μ parse() μ½λλ₯Ό λ§€λ² μμ±ν νμκ° μμ΅λλ€.
CrawlSpiderμ SitemapSpiderλ LinkExtractorλ₯Ό μ¬μ©ν©λλ€. μ΄ ν΄λμ€λ Responseμμ URLμ μΆμΆνκ±°λ matches()λ‘ URL νλλ₯Ό κ²μ¬ν©λλ€. SitemapSpiderλ gzip μμΆ μ¬λΆμ κ΄κ³μμ΄ sitemap.xml / sitemap_index.xml λ³Έλ¬Έλ λ΄λΆμμ νμ±ν©λλ€.
μΌλ° Spider.parse() μμμ LinkExtractorλ₯Ό μ§μ μ¬μ©ν μλ μμ΅λλ€. ν
νλ¦Ώμ μ΄ μ°κ²° μμ
μ λμ ν΄ μ€ λΏμ
λλ€.
CrawlSpider {#crawlspider}
CrawlSpiderλ μ μΈν κ·μΉμ λ°λΌ λ§ν¬λ₯Ό μλμΌλ‘ λ°λΌκ°λλ€.
from scrapling.spiders import CrawlSpider, CrawlRule, LinkExtractor
class QuotesSpider(CrawlSpider):
name = "blog"
start_urls = ["https://quotes.toscrape.com/"]
def rules(self):
return [
CrawlRule(LinkExtractor(allow=r"/author/"), callback=self.parse_author),
CrawlRule(LinkExtractor(allow=r"/page/\d+/")), # follow pagination, no callback
]
async def parse_author(self, response):
yield {
'.author-title': response.css('.author-title::text').get(),
"birthday": response.css('.author-born-date::text').get(),
"url": response.url,
}
result = QuotesSpider().start()
CrawlRuleμ LinkExtractorμ μ νμ callback(μ€νμ΄λμ λ°μΈλ©λ λ©μλ), μμ±ν Requestμ μ νμ priority, μ νμ process_requestλ₯Ό μ°κ²°ν©λλ€. λ§μ§λ§ μΈμλ κ° Requestλ₯Ό λ΄λ³΄λ΄κΈ° μ μ μμ νλ λ°μΈλ©λ λ©μλμ
λλ€. κΈ°λ³Έ parse()λ λͺ¨λ μλ΅μ λͺ¨λ κ·μΉμ μ μ©νκ³ , μΌμΉνλ URLλ§λ€ Requestλ₯Ό λ΄λ³΄λ
λλ€.
κ·μΉμ μ½λ°±μ΄ μμΌλ©΄ ν΄λΉ URLμ μ€νμ΄λμ κΈ°λ³Έ parse()λ‘ μ λ¬ν©λλ€. λ€μ νμ΄μ§ λ§ν¬λ₯Ό μΆμΆν΄ λ³λ μ½λ°± μμ΄ κ³μ λ°©λ¬Έν μ μμΌλ―λ‘ νμ΄μ§ λλκΈ°μ μ μ©ν©λλ€.
ν¬λ‘€λ§ μμ {#crawl-order}
CrawlSpiderλ ContentSpiderλ₯Ό ν¬ν¨ν νμ ν΄λμ€μμ crawl_strategyλ₯Ό μ€μ ν΄ λκΈ° μμ²μ μμλ₯Ό μ ννμΈμ.
crawl_strategy |
κ°μ Request.priority μμμμ μμ |
|---|---|
"bfs"(κΈ°λ³Έκ°) |
νμ λ¨Όμ λ€μ΄μ¨ μμ²λΆν° |
"dfs" |
νμ λμ€μ λ€μ΄μ¨ μμ²λΆν° |
μ€μΌμ€λ¬λ Request.priorityκ° λμ λκΈ° μμ²μ λ¨Όμ μ νν©λλ€. μ λ΅μ μ°μ μμκ° κ°μ μμ² μ¬μ΄μ μμλ§ μ ν©λλ€.
μ νν ν μμλλ‘ μμ²μ νλμ© μ²λ¦¬νλ €λ©΄ concurrent_requests = 1μ μ€μ νμΈμ. μ€λ³΅ URLμ κ³μ μ μΈνλ©°, κ°μ URLμ λ€μ λ°κ²¬ν΄λ νμ μλ μμ²μ μμΉλ λ°λμ§ μμ΅λλ€. λμ μμ²μ μ¬μ©νλ©΄ νμ΄μ§κ° λλλ μμκ° λ¬λΌμ§κ³ , νμ λ¨κ³λ λΆκΈ°κ° κ²ΉμΉ μ μμ΅λλ€.
체ν¬ν¬μΈνΈμμ μ¬κ°ν λλ κ°μ crawl_strategyλ₯Ό μ μ§νμΈμ.
κ·μΉμ μνλ λμ λνκΈ° {#combining-rules-with-custom-logic}
parse()λ₯Ό μ¬μ μνκ³ super().parse(response)λ₯Ό νΈμΆνλ©΄ κ·μΉμ λ°λ₯Έ λμκ³Ό μ§μ λ΄λ³΄λ΄λ κ²°κ³Όλ₯Ό ν¨κ» μ¬μ©ν μ μμ΅λλ€.
class MySpider(CrawlSpider):
def rules(self):
return [CrawlRule(LinkExtractor(allow=r"/posts/"), callback=self.parse_post)]
async def parse(self, response):
yield {"page_url": response.url}
async for req in super().parse(response):
yield req
process_requestλ‘ μμ² λ°κΎΈκΈ° {#mutating-requests-with-process_request}
def add_priority(self, request, response):
request.priority = 10
return request
def rules(self):
return [CrawlRule(
LinkExtractor(allow=r"/posts/"),
callback=self.parse_post,
process_request=self.add_priority,
)]
SitemapSpider {#sitemapspider}
SitemapSpiderλ sitemap.xmlμ URLμμ ν¬λ‘€λ§μ μμν©λλ€. CrawlSpiderμ κ°μ rules() APIλ₯Ό μ¬μ©νλ―λ‘ λμ λ°©μμ ν¨κ» μ΄ν΄ν μ μμ΅λλ€.
from scrapling.spiders import SitemapSpider, CrawlRule, LinkExtractor
class MySitemap(SitemapSpider):
name = "sm"
sitemap_urls = ["https://example.com/sitemap.xml"]
def rules(self):
return [
CrawlRule(LinkExtractor(allow=r"/posts/"), callback=self.parse_post),
CrawlRule(LinkExtractor(allow=r"/products/"), callback=self.parse_product),
]
async def parse_post(self, response):
yield {"title": response.css("h1::text").get()}
async def parse_product(self, response):
yield {"sku": response.css(".sku::text").get()}
result = MySitemap().start()
URLμ μ½λ°±μΌλ‘ μ λ¬νλ λ°©μ {#how-urls-are-dispatched}
SitemapSpiderλ μ¬μ΄νΈλ§΅μ URLλ§λ€ κ° κ·μΉμ LinkExtractor.matches(url)μ μμλλ‘ νμΈν©λλ€. μ²μ μΌμΉν κ·μΉμ μ ννκ³ , κ·Έ μ½λ°±μ κ°μ§ Requestλ₯Ό λ΄λ³΄λ
λλ€. rules()κ° λΉμ΄ μμ§ μμλ° μΌμΉνλ κ·μΉμ΄ μμΌλ©΄ URLμ μ μΈν©λλ€. rules()κ° λΉ λ¦¬μ€νΈμ΄λ©΄ λͺ¨λ URLμ μ€νμ΄λμ parse()λ‘ μ λ¬ν©λλ€. μ΄ λ©μλλ μ¬μ μνμ§ μμΌλ©΄ κΈ°λ³Έμ μΌλ‘ NotImplementedErrorλ₯Ό λ°μμν΅λλ€.
μ¬μ΄νΈλ§΅ μΈλ±μ€ {#sitemap-indexes}
SitemapSpiderκ° μ¬μ΄νΈλ§΅ λͺ©λ‘μΈ <sitemapindex>λ₯Ό λ§λλ©΄ νμ μ¬μ΄νΈλ§΅μ μλμΌλ‘ λ°©λ¬Έν©λλ€. λ°©λ¬Έν νμ μ¬μ΄νΈλ§΅μ μ ννλ €λ©΄ sitemap_followμ LinkExtractorλ₯Ό μ€μ νμΈμ.
class MySitemap(SitemapSpider):
name = "sm"
sitemap_urls = ["https://example.com/sitemap.xml"]
sitemap_follow = LinkExtractor(allow=r"/posts-sitemap-\d+\.xml") # only post sitemaps
robots.txt μ§μ {#robotstxt-support}
robots.txt URLμ sitemap_urlsμ λ°λ‘ λ£μΌλ©΄ SitemapSpiderκ° νμΌμ μμλ³΄κ³ , λμ΄λ λͺ¨λ μ¬μ΄νΈλ§΅μ μΆμΆν΄ λ°©λ¬Έν©λλ€.
class MySitemap(SitemapSpider):
name = "sm"
sitemap_urls = ["https://example.com/robots.txt"]
λ€λ₯Έ μΈμ΄μ URL {#alternate-language-urls}
sitemap_alternate_links = Trueλ₯Ό μ€μ νλ©΄ <xhtml:link rel="alternate" hreflang="...">μ URLλ κ·μΉμΌλ‘ μ²λ¦¬ν©λλ€.
XMLFeedSpider {#xmlfeedspider}
XMLFeedSpiderλ RSS, Atom, μν νΌλ λ± XML νΌλμ λ
Έλλ₯Ό μνν©λλ€. itertagμ μνλ λ
Έλ μ΄λ¦μ μ€μ νκ³ (κΈ°λ³Έκ°: "item"), μΌμΉνλ λ
Έλλ§λ€ νΈμΆν parse_node()λ₯Ό μ¬μ μνμΈμ.
from scrapling.spiders import XMLFeedSpider
class RSSSpider(XMLFeedSpider):
name = "rss"
start_urls = ["https://example.com/feed.xml"]
itertag = "item"
async def parse_node(self, response, node):
yield {
"title": node.findtext("title"),
"link": node.findtext("link"),
"date": node.findtext("pubDate"),
}
result = RSSSpider().start()
λ€λ₯Έ μ½λ°±μ²λΌ parse_node()λ Requestλ₯Ό λ΄λ³΄λΌ μ μμ΅λλ€. μλ₯Ό λ€μ΄ response.follow(node.findtext("link"), callback=self.parse_post)λ‘ νΌλκ° κ°λ¦¬ν€λ νμ΄μ§λ₯Ό λ°©λ¬Έν μ μμ΅λλ€.
λ Έλ μ νκ³Ό νμ± {#how-nodes-are-matched-and-parsed}
parse_node()μ μ λ¬νλ λ
Έλλ λ€μμ€νμ΄μ€λ₯Ό λͺ¨λ μ κ±°ν lxml μμμ
λλ€. λ°λΌμ node.findtext("title"), node.find("thumbnail").get("url"), λμλ¬Έμλ₯Ό ꡬλΆνλ node.xpath(...)λ₯Ό μ΄λ€ νΌλμμλ λ€μμ€νμ΄μ€ λ§€ν μμ΄ μ¬μ©ν μ μμ΅λλ€. "entry"μ²λΌ λ¨μν itertagλ λ€μμ€νμ΄μ€μ κ΄κ³μμ΄ μ΄λ¦μΌλ‘ λ
Έλλ₯Ό μ°Ύμ΅λλ€. Atomκ³Ό λλΆλΆμ λ€μμ€νμ΄μ€κ° μλ νΌλμ μ ν©ν©λλ€. νΉμ λ€μμ€νμ΄μ€μ λ
Έλλ§ μ°ΎμΌλ €λ©΄ μ λμ¬κ° μλ itertagλ₯Ό μ¬μ©νκ³ namespacesμ μ λμ¬λ₯Ό μ μνμΈμ.
class ThumbnailSpider(XMLFeedSpider):
name = "thumbs"
start_urls = ["https://example.com/feed.xml"]
itertag = "media:thumbnail"
namespaces = (("media", "http://search.yahoo.com/mrss/"),)
async def parse_node(self, response, node):
yield {"thumbnail": node.get("url")}
gzip νΌλ(.xml.gz λλ gzip μ½ν
μΈ νμ
μΌλ‘ μ 곡λλ νΌλ)λ μ¬μ΄νΈλ§΅ μ€νμ΄λμ κ°μ λ³΄νΈ κΈ°λ₯μΌλ‘ μλ μμΆ ν΄μ ν©λλ€. μλͺ»λ XMLμ ν¬λ‘€λ§μ μ€λ¨νμ§ μκ³ κ²½κ³ λ₯Ό κΈ°λ‘ν©λλ€.
CSVFeedSpider {#csvfeedspider}
CSVFeedSpiderλ CSV νΌλμ νμ μνν©λλ€. μ΄ μ΄λ¦μ ν€λ‘ νλ λμ
λλ¦¬λ‘ κ° νμ λ°λ parse_row()λ₯Ό μ¬μ μνμΈμ.
from scrapling.spiders import CSVFeedSpider
class PriceSpider(CSVFeedSpider):
name = "prices"
start_urls = ["https://example.com/products.csv"]
async def parse_row(self, response, row):
yield {"product": row["title"], "price": float(row["price"])}
result = PriceSpider().start()
κΈ°λ³Έμ μΌλ‘ 첫 νμ ν€λλ‘ μ¬μ©ν©λλ€. ν€λ νμ΄ μμΌλ©΄ headersμ μ΄ μ΄λ¦μ μ§μ μ§μ νμΈμ. νμ€ μΌν νμμ΄ μλλΌλ©΄ delimiter/quotecharλ₯Ό μ€μ νμΈμ.
class PriceSpider(CSVFeedSpider):
name = "prices"
start_urls = ["https://example.com/products.csv"]
headers = ["title", "price", "url"]
delimiter = ";"
μμ XMLFeedSpiderμ²λΌ gzip νΌλλ μλμΌλ‘ μμΆ ν΄μ ν©λλ€.
ContentSpider {#contentspider}
ContentSpiderλ CrawlSpiderλ₯Ό λ°νμΌλ‘ μ¬μ΄νΈλ₯Ό λ°©λ¬Ένκ³ κ° νμ΄μ§λ₯Ό LLMμ μ λ¬νκΈ° μ’μ μ 리λ MarkdownμΌλ‘ λ°κΏλλ€. νμ΄μ§λ§λ€ νλͺ© νλλ₯Ό λ΄λ³΄λ΄λ©°, νμνλ©΄ Markdown νμΌλ νλμ© μμ±ν©λλ€.
from scrapling.spiders import ContentSpider
class DocsSpider(ContentSpider):
name = "docs"
start_urls = ["https://example.com/docs/"]
allowed_domains = {"example.com"}
output_dir = "docs_markdown"
result = DocsSpider().start()
ν¬λ‘€λ§ λ²μλ₯Ό λμ μ¬μ΄νΈλ‘ μ ννλλ‘ allowed_domainsλ₯Ό μ€μ ν΄μΌ ν©λλ€. μ 체 μ΅μ
κ³Ό RAG νμ΄νλΌμΈμ κ²°κ³Όλ₯Ό μ λ¬νλ λ°©λ²μ RAG μμ€ν
λ§λ€κΈ°λ₯Ό μ°Έκ³ νμΈμ.
LinkExtractor μ§μ μ¬μ©νκΈ° {#using-linkextractor-directly}
ν
νλ¦Ώμ νμκ° μλλλ€. LinkExtractorλ μΌλ° Spider μμμλ μ¬μ©ν μ μμ΅λλ€.
from scrapling.spiders import Spider, LinkExtractor
class CustomSpider(Spider):
name = "custom"
start_urls = ["https://example.com"]
def __init__(self):
super().__init__()
self._links = LinkExtractor(allow=r"/posts/", deny_domains="ads.example.com")
async def parse(self, response):
for url in self._links.extract(response):
yield response.follow(url, callback=self.parse_post)
async def parse_post(self, response):
yield {"title": response.css("h1::text").get()}
LinkExtractor μ°Έκ³ μλ£ {#linkextractor-reference}
| μΈμ | κΈ°λ³Έκ° | μ€λͺ |
|---|---|---|
allow |
() |
μ μ§ν URL ν¨ν΄μ
λλ€. λΉμ΄ μμΌλ©΄ λͺ¨λ νμ©ν©λλ€. λ¬Έμμ΄, μ»΄νμΌν Pattern, λλ μ΄ κ°μ λ΄μ λ°λ³΅ κ°λ₯ν κ°μ²΄λ₯Ό λ°μ΅λλ€ |
deny |
() |
μ μΈν URL ν¨ν΄μ
λλ€. νμ allowλ³΄λ€ μ°μ ν©λλ€ |
allow_domains |
() |
νμ©ν νΈμ€νΈ μ΄λ¦μ
λλ€. νμ λλ©μΈλ νμ©νλ―λ‘ example.comμ api.example.comκ³Όλ μΌμΉν©λλ€ |
deny_domains |
() |
μ μΈν νΈμ€νΈ μ΄λ¦ |
restrict_css |
() |
DOMμμ λ§ν¬λ₯Ό μΆμΆν μμμ μ ννλ CSS μ νμ |
restrict_xpath |
() |
DOMμμ λ§ν¬λ₯Ό μΆμΆν μμμ μ ννλ XPath μ νμ |
tags |
("a", "area") |
λ§ν¬λ₯Ό μ°Ύμ μμ νκ·Έ |
attrs |
("href",) |
ν΄λΉ νκ·Έμμ URLμ μ½μ μμ± |
canonicalize |
True |
쿼리 μΈμλ₯Ό μ λ ¬νκ³ κ²½λ‘λ₯Ό μ κ·νν©λλ€ |
strip |
True |
μΆμΆν URL μ λμ 곡백μ μ κ±°ν©λλ€ |
keep_fragment |
False |
μ κ·νν λ #fragmentλ₯Ό μ μ§ν©λλ€ |
deny_extensions |
IGNORED_EXTENSIONS |
μ μΈν νμΌ νμ₯μμ λλ€. pdf, zip, μ΄λ―Έμ§, λμμ λ±μ΄ ν΄λΉν©λλ€ |
process |
None |
νν°λ§ μ μ μΆμΆν URLλ§λ€ μ μ©ν μ νμ νΈμΆ κ°λ₯ κ°μ²΄μ λλ€. κ±°μ§μΌλ‘ νκ°λλ κ°μ λ°ννλ©΄ URLμ μ μΈν©λλ€ |
LinkExtractor.extract(response)λ νν°λ§κ³Ό μ€λ³΅ μ κ±°λ₯Ό λ§μΉ μ λ URLμ list[str]λ₯Ό λ°νν©λλ€.
LinkExtractor.matches(url)μ boolμ λ°ννλ URL μ μ© νν°μ
λλ€. νμ©/μ μΈ ν¨ν΄, λλ©μΈ, νμ₯μλ₯Ό κ²μ¬ν©λλ€. SitemapSpiderλ μ΄ λ©μλλ‘ Response μμ΄ URLμ μ λ¬ν κ·μΉμ μ ν©λλ€.