π·οΈ 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: "μ€νμ΄λ μμ², μ½λ°±, 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"]μμ νμΈν μ μμ΅λλ€.