Skip to content

πŸ•·οΈ 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

9.0 KB
Raw Blame

title: "μŠ€νŒŒμ΄λ” μš”μ²­, 콜백, Response.follow" description: "크둀링 μš”μ²­μ„ λ§Œλ“€κ³  메타데이터λ₯Ό μ „λ‹¬ν•˜λ©° response.follow둜 링크λ₯Ό λ”°λΌκ°€μ„Έμš”. 콜백, μš°μ„ μˆœμœ„, μš”μ²­ 쀑볡 제거λ₯Ό μ•Œμ•„λ΄…λ‹ˆλ‹€."

μš”μ²­κ³Ό 응닡 {#requests-responses}

!!! success "μ‹œμž‘ μ „ 확인"

1. [μ‹œμž‘ν•˜κΈ°](getting-started.md)λ₯Ό 읽고 κ°„λ‹¨ν•œ μŠ€νŒŒμ΄λ”λ₯Ό λ§Œλ“€κ³  μ‹€ν–‰ν•˜λŠ” 방법을 μ•Œμ•„λ‘μ„Έμš”.

이 νŽ˜μ΄μ§€λŠ” Request 객체λ₯Ό μžμ„Ένžˆ μ„€λͺ…ν•©λ‹ˆλ‹€. μš”μ²­ λ§Œλ“€κΈ°, 콜백 κ°„ 데이터 전달, μš°μ„ μˆœμœ„μ™€ 쀑볡 제거 μ œμ–΄, response.follow()둜 링크 따라가기λ₯Ό λ‹€λ£Ήλ‹ˆλ‹€.

Request 객체 {#the-request-object}

RequestλŠ” κ°€μ Έμ˜¬ URL을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 직접 λ§Œλ“€κ±°λ‚˜ response.follow()둜 λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

from scrapling.spiders import Request

# Direct construction
request = Request(
    "https://example.com/page",
    callback=self.parse_page,
    priority=5,
)

# Via response.follow (preferred in callbacks)
request = response.follow("/page", callback=self.parse_page)

Request에 전달할 수 μžˆλŠ” μΈμˆ˜λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

인수 νƒ€μž… κΈ°λ³Έκ°’ μ„€λͺ…
url str ν•„μˆ˜ κ°€μ Έμ˜¬ URL
sid str "" μ‚¬μš©ν•  μ„Έμ…˜μ˜ IDμž…λ‹ˆλ‹€. μ„Έμ…˜μ„ μ°Έκ³ ν•˜μ„Έμš”
callback callable None 응닡을 μ²˜λ¦¬ν•  비동기 μ œλ„ˆλ ˆμ΄ν„° λ©”μ„œλ“œμž…λ‹ˆλ‹€. 기본값은 parse()μž…λ‹ˆλ‹€
priority int 0 값이 클수둝 λ¨Όμ € μ²˜λ¦¬ν•©λ‹ˆλ‹€
dont_filter bool False True이면 쀑볡 제거λ₯Ό κ±΄λ„ˆλ›°κ³  같은 μš”μ²­μ„ ν—ˆμš©ν•©λ‹ˆλ‹€
meta dict {} 응닡에 전달할 μž„μ˜μ˜ 메타데이터
**kwargs μ„Έμ…˜μ˜ κ°€μ Έμ˜€κΈ° λ©”μ„œλ“œμ— 전달할 μΆ”κ°€ ν‚€μ›Œλ“œ μΈμˆ˜μž…λ‹ˆλ‹€. 예: headers, method, data

μΆ”κ°€ ν‚€μ›Œλ“œ μΈμˆ˜λŠ” μ„Έμ…˜μ— 직접 μ „λ‹¬ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ POST μš”μ²­μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

yield Request(
    "https://example.com/api",
    method="POST",
    data={"key": "value"},
    callback=self.parse_result,
)

Response.follow() {#responsefollow}

μ½œλ°±μ—μ„œ λ‹€μŒ μš”μ²­μ„ λ§Œλ“€ λ•ŒλŠ” response.follow()λ₯Ό ꢌμž₯ν•©λ‹ˆλ‹€. Requestλ₯Ό 직접 λ§Œλ“œλŠ” 것보닀 λ‹€μŒκ³Ό 같은 μž₯점이 μžˆμŠ΅λ‹ˆλ‹€.

  • μƒλŒ€ URL을 ν˜„μž¬ νŽ˜μ΄μ§€ URL κΈ°μ€€μœΌλ‘œ μžλ™ λ³€ν™˜ν•©λ‹ˆλ‹€.
  • Referer 헀더에 기본적으둜 ν˜„μž¬ νŽ˜μ΄μ§€ URL을 μ„€μ •ν•©λ‹ˆλ‹€.
  • μ„Έμ…˜ μΈμˆ˜λŠ” μ›λž˜ μš”μ²­μ—μ„œ μƒμ†ν•©λ‹ˆλ‹€. 헀더와 ν”„λ‘μ‹œ μ„€μ • 등이 ν•΄λ‹Ήν•©λ‹ˆλ‹€. 단, method, params, data, json, files, multipartλŠ” μ œμ™Έν•˜λ―€λ‘œ λ§ν¬λŠ” 일반 GET으둜 κ°€μ Έμ˜΅λ‹ˆλ‹€.
  • 콜백, μ„Έμ…˜ ID, μš°μ„ μˆœμœ„λ₯Ό μ§€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ μ›λž˜ μš”μ²­μ˜ 값을 μƒμ†ν•©λ‹ˆλ‹€.
async def parse(self, response: Response):
    # Minimal - inherits callback, sid, priority from current request
    yield response.follow("/next-page")

    # Override specific fields
    yield response.follow(
        "/product/123",
        callback=self.parse_product,
        priority=10,
    )

    # Pass additional metadata to
    yield response.follow(
        "/details",
        callback=self.parse_details,
        meta={"category": "electronics"},
    )
인수 νƒ€μž… κΈ°λ³Έκ°’ μ„€λͺ…
url str ν•„μˆ˜ λ”°λΌκ°ˆ μ ˆλŒ€ λ˜λŠ” μƒλŒ€ URL
sid str "" μ„Έμ…˜ IDμž…λ‹ˆλ‹€. λΉ„μ–΄ 있으면 μ›λž˜ μš”μ²­μ—μ„œ μƒμ†ν•©λ‹ˆλ‹€
callback callable None 콜백 λ©”μ„œλ“œμž…λ‹ˆλ‹€. None이면 μ›λž˜ μš”μ²­μ—μ„œ μƒμ†ν•©λ‹ˆλ‹€
priority int None μš°μ„ μˆœμœ„μž…λ‹ˆλ‹€. None이면 μ›λž˜ μš”μ²­μ—μ„œ μƒμ†ν•©λ‹ˆλ‹€
dont_filter bool False 쀑볡 제거λ₯Ό κ±΄λ„ˆλœλ‹ˆλ‹€
meta dict None ν˜„μž¬ μ‘λ‹΅μ˜ 메타데이터와 ν•©μΉ  메타데이터
referer_flow bool True ν˜„μž¬ URL을 Referer ν—€λ”λ‘œ μ„€μ •ν•©λ‹ˆλ‹€
**kwargs μ›λž˜ μš”μ²­μ˜ μ„Έμ…˜ μΈμˆ˜μ™€ ν•©μΉ©λ‹ˆλ‹€

Referer 전달 끄기 {#disabling-referer-flow}

기본적으둜 response.follow()λŠ” ν˜„μž¬ νŽ˜μ΄μ§€ URL을 Referer ν—€λ”λ‘œ μ„€μ •ν•©λ‹ˆλ‹€. 끄렀면 λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•˜μ„Έμš”.

yield response.follow("/page", referer_flow=False)

콜백 {#callbacks}

μ½œλ°±μ€ 응닡을 μ²˜λ¦¬ν•˜λŠ” μŠ€νŒŒμ΄λ”μ˜ 비동기 μ œλ„ˆλ ˆμ΄ν„° λ©”μ„œλ“œμž…λ‹ˆλ‹€. λ‹€μŒ μ„Έ νƒ€μž… 쀑 ν•˜λ‚˜λ₯Ό yield둜 내보내야 ν•©λ‹ˆλ‹€.

  • dict: 결과에 μΆ”κ°€ν•  μˆ˜μ§‘ ν•­λͺ©μž…λ‹ˆλ‹€.
  • Request: 큐에 μΆ”κ°€ν•  λ‹€μŒ μš”μ²­μž…λ‹ˆλ‹€.
  • None: μ•Œλ¦Ό 없이 λ¬΄μ‹œν•©λ‹ˆλ‹€.
class MySpider(Spider):
    name = "my_spider"
    start_urls = ["https://example.com"]

    async def parse(self, response: Response):
        # Yield items (dicts)
        yield {"url": response.url, "title": response.css("title::text").get("")}

        # Yield follow-up requests
        for link in response.css("a::attr(href)").getall():
            yield response.follow(link, callback=self.parse_page)

    async def parse_page(self, response: Response):
        yield {"content": response.css("article::text").get("")}

!!! tip "μ°Έκ³ "

λͺ¨λ“  μ½œλ°±μ€ `async def`둜 μ •μ˜ν•˜κ³  `return` λŒ€μ‹  `yield`λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€. λ‹€μŒ μš”μ²­ 없이 ν•­λͺ©λ§Œ 내보내더라도 비동기 μ œλ„ˆλ ˆμ΄ν„°μ—¬μ•Ό ν•©λ‹ˆλ‹€.

μš”μ²­ μš°μ„ μˆœμœ„ {#request-priority}

μš°μ„ μˆœμœ„ 값이 높은 μš”μ²­μ„ λ¨Όμ € μ²˜λ¦¬ν•©λ‹ˆλ‹€. 일뢀 νŽ˜μ΄μ§€λ₯Ό λ‹€λ₯Έ νŽ˜μ΄μ§€λ³΄λ‹€ λ¨Όμ € μ²˜λ¦¬ν•΄μ•Ό ν•  λ•Œ μœ μš©ν•©λ‹ˆλ‹€.

async def parse(self, response: Response):
    # High priority - process product pages first
    for link in response.css("a.product::attr(href)").getall():
        yield response.follow(link, callback=self.parse_product, priority=10)

    # Low priority - pagination links processed after products
    next_page = response.css("a.next::attr(href)").get()
    if next_page:
        yield response.follow(next_page, callback=self.parse, priority=0)

response.follow()μ—μ„œλŠ” μƒˆ 값을 μ§€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ μ›λž˜ μš”μ²­μ˜ μš°μ„ μˆœμœ„λ₯Ό μƒμ†ν•©λ‹ˆλ‹€.

쀑볡 제거 {#deduplication}

μŠ€νŒŒμ΄λ”λŠ” URL, HTTP λ©”μ„œλ“œ, μš”μ²­ λ³Έλ¬Έ, μ„Έμ…˜ ID둜 지문을 계산해 쀑볡 μš”μ²­μ„ μ œκ±°ν•©λ‹ˆλ‹€. 두 μš”μ²­μ˜ 지문이 κ°™μœΌλ©΄ 두 번째 μš”μ²­μ„ μ•Œλ¦Ό 없이 μ œμ™Έν•©λ‹ˆλ‹€.

둜그인 ν›„ νŽ˜μ΄μ§€λ₯Ό λ‹€μ‹œ λ°©λ¬Έν•˜λŠ” 경우처럼 쀑볡 μš”μ²­μ΄ ν•„μš”ν•˜λ©΄ dont_filter=Trueλ₯Ό μ„€μ •ν•˜μ„Έμš”.

yield Request("https://example.com/dashboard", dont_filter=True, callback=self.parse_dashboard)

# Or with response.follow
yield response.follow("/dashboard", dont_filter=True, callback=self.parse_dashboard)

μŠ€νŒŒμ΄λ”μ˜ 클래슀 μ†μ„±μœΌλ‘œ 지문에 포함할 λ‚΄μš©μ„ μ‘°μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

속성 κΈ°λ³Έκ°’ λ™μž‘
fp_include_kwargs False 헀더 λ“± μ„Έμ…˜μ˜ κ°€μ Έμ˜€κΈ° λ©”μ„œλ“œμ— μ „λ‹¬ν•œ μΆ”κ°€ μš”μ²­ 인수λ₯Ό 지문에 ν¬ν•¨ν•©λ‹ˆλ‹€
fp_keep_fragments False 지문을 계산할 λ•Œ URL ν”„λž˜κ·Έλ¨ΌνŠΈ(#section)λ₯Ό μœ μ§€ν•©λ‹ˆλ‹€
fp_include_headers False μš”μ²­ 헀더λ₯Ό 지문에 ν¬ν•¨ν•©λ‹ˆλ‹€

예λ₯Ό λ“€μ–΄ https://example.com/page#section1κ³Ό https://example.com/page#section2λ₯Ό μ„œλ‘œ λ‹€λ₯Έ URL둜 μ²˜λ¦¬ν•˜λ €λ©΄ λ‹€μŒκ³Ό 같이 μ„€μ •ν•˜μ„Έμš”.

class MySpider(Spider):
    name = "my_spider"
    fp_keep_fragments = True
    # ...

μš”μ²­ 메타데이터 {#request-meta}

meta λ”•μ…”λ„ˆλ¦¬λ‘œ 콜백 사이에 μ›ν•˜λŠ” 데이터λ₯Ό 전달할 수 μžˆμŠ΅λ‹ˆλ‹€. λ‹€λ₯Έ νŽ˜μ΄μ§€λ₯Ό μ²˜λ¦¬ν•  λ•Œ 이전 νŽ˜μ΄μ§€μ˜ 정보가 ν•„μš”ν•˜λ©΄ μœ μš©ν•©λ‹ˆλ‹€.

async def parse(self, response: Response):
    for product in response.css("div.product"):
        category = product.css("span.category::text").get("")
        link = product.css("a::attr(href)").get()
        if link:
            yield response.follow(
                link,
                callback=self.parse_product,
                meta={"category": category},
            )

async def parse_product(self, response: Response):
    yield {
        "name": response.css("h1::text").get(""),
        "price": response.css(".price::text").get(""),
        # Access meta from the request
        "category": response.meta.get("category", ""),
    }

response.follow()λŠ” ν˜„μž¬ μ‘λ‹΅μ˜ 메타데이터와 μƒˆ 메타데이터λ₯Ό ν•©μΉ©λ‹ˆλ‹€. μƒˆ 값이 μš°μ„ ν•©λ‹ˆλ‹€.

μŠ€νŒŒμ΄λ” μ‹œμŠ€ν…œμ€ 일뢀 메타데이터도 μžλ™μœΌλ‘œ μ €μž₯ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ ν”„λ‘μ‹œ μˆœν™˜μ„ μ‚¬μš©ν•˜λ©΄ μš”μ²­μ— μ‚¬μš©ν•œ ν”„λ‘μ‹œλ₯Ό response.meta["proxy"]μ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.