π·οΈ 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μΌλ‘ 첫 Python μΉ ν¬λ‘€λ¬ λ§λ€κΈ°" description: "λ°μ΄ν°λ₯Ό μΆμΆνκ³ λ€μ νμ΄μ§λ₯Ό λ°©λ¬Ένλ©° κ²°κ³Όλ₯Ό λ΄λ³΄λ΄λ μ€νμ΄λλ₯Ό λ§λμΈμ. μμ URL, μ½λ°±, λλ©μΈ μ ν, robots.txt μ€μ μ μμλ΄ λλ€."
μμνκΈ° {#getting-started}
μκ° {#introduction}
!!! success "μμ μ νμΈ"
1. [Fetcher κΈ°λ³Έ μ¬ν](../fetching/choosing.md)μ μ½κ³ Fetcher μ’
λ₯μ κ°κ°μ μ©λλ₯Ό μ΄ν΄ν΄μΌ ν©λλ€.
2. [μ£Όμ ν΄λμ€](../parsing/main_classes.md)λ₯Ό μ½κ³ [Selector](../parsing/main_classes.md#selector)μ [Response](../fetching/choosing.md#response-object) ν΄λμ€λ₯Ό μ΄ν΄ν΄μΌ ν©λλ€.
3. [μν€ν
μ²](architecture.md)λ₯Ό μ½κ³ μ€νμ΄λ μμ€ν
μ μ 체 λμμ μμλμΈμ.
μ€νμ΄λ μμ€ν μ μ¬μ©νλ©΄ μ§§μ μ½λλ‘ μ¬λ¬ νμ΄μ§λ₯Ό λμμ μμ§νλ ν¬λ‘€λ¬λ₯Ό λ§λ€ μ μμ΅λλ€. Scrapyλ₯Ό μ¬μ©ν΄ λ΄€λ€λ©΄ μ΅μν λ°©μμ λλ€. μ²μμ΄λΌλ μ΄ μλ΄μ λ°λΌ μμν μ μμ΅λλ€.
첫 μ€νμ΄λ λ§λ€κΈ° {#your-first-spider}
μ€νμ΄λλ μΉμ¬μ΄νΈλ₯Ό νμνκ³ λ°μ΄ν°λ₯Ό μΆμΆνλ λ°©λ²μ μ μνλ ν΄λμ€μ λλ€. κ°μ₯ κ°λ¨ν μλ λ€μκ³Ό κ°μ΅λλ€.
from scrapling.spiders import Spider, Response
class QuotesSpider(Spider):
name = "quotes"
start_urls = ["https://quotes.toscrape.com"]
async def parse(self, response: Response):
for quote in response.css("div.quote"):
yield {
"text": quote.css("span.text::text").get(""),
"author": quote.css("small.author::text").get(""),
}
λͺ¨λ μ€νμ΄λμλ λ€μ μΈ κ°μ§κ° νμν©λλ€.
name: μ€νμ΄λμ κ³ μ ν μ΄λ¦μ λλ€.start_urls: ν¬λ‘€λ§μ μμν URL λͺ©λ‘μ λλ€.parse(): κ° μλ΅μ μ²λ¦¬νκ³ κ²°κ³Όλ₯Ό λ΄λ³΄λ΄λ λΉλκΈ° μ λλ μ΄ν° λ©μλμ λλ€.
μ£Όμ μ²λ¦¬λ parse()μμ ν©λλ€. Scraplingμ Selector/Responseμ κ°μ μ ν λ©μλλ₯Ό μ¬μ©νκ³ , λμ
λ리λ₯Ό yieldλ‘ λ΄λ³΄λ΄ μμ§ν νλͺ©μ μ λ¬ν©λλ€.
μ€νμ΄λ μ€ννκΈ° {#running-the-spider}
μ€νμ΄λ μΈμ€ν΄μ€λ₯Ό λ§λ€κ³ start()λ₯Ό νΈμΆνμΈμ.
result = QuotesSpider().start()
start()κ° λΉλκΈ° μ€νμ λ΄λΆμμ μ²λ¦¬νλ―λ‘ μ΄λ²€νΈ 루νλ₯Ό μ§μ κ΄λ¦¬ν νμκ° μμ΅λλ€. μ€ν μ€μλ λͺ¨λ λμμ΄ ν°λ―Έλμ κΈ°λ‘λκ³ , ν¬λ‘€λ§μ΄ λλλ©΄ μμΈν ν΅κ³λ₯Ό μ»μ΅λλ€.
ν΅κ³μ κ²°κ³Όλ λ°νλ CrawlResult κ°μ²΄μμ νμΈν μ μμ΅λλ€.
result = QuotesSpider().start()
# Access scraped items
for item in result.items:
print(item["text"], "-", item["author"])
# Check statistics
print(f"Scraped {result.stats.items_scraped} items")
print(f"Made {result.stats.requests_count} requests")
print(f"Took {result.stats.elapsed_seconds:.1f} seconds")
# Did the crawl finish or was it paused?
print(f"Completed: {result.completed}")
λ§ν¬ λ°λΌκ°κΈ° {#following-links}
λλΆλΆμ ν¬λ‘€λ§μ λ§ν¬λ₯Ό λ°λΌ μ¬λ¬ νμ΄μ§λ₯Ό λ°©λ¬Έν©λλ€. response.follow()λ‘ λ€μ μμ²μ λ§λμΈμ.
from scrapling.spiders import Spider, Response
class QuotesSpider(Spider):
name = "quotes"
start_urls = ["https://quotes.toscrape.com"]
async def parse(self, response: Response):
# Extract items from the current page
for quote in response.css("div.quote"):
yield {
"text": quote.css("span.text::text").get(""),
"author": quote.css("small.author::text").get(""),
}
# Follow the "next page" link
next_page = response.css("li.next a::attr(href)").get()
if next_page:
yield response.follow(next_page, callback=self.parse)
response.follow()λ μλ URLμ νμ¬ νμ΄μ§μ URLκ³Ό μλμΌλ‘ κ²°ν©ν©λλ€. κΈ°λ³Έμ μΌλ‘ νμ¬ νμ΄μ§λ₯Ό Referer ν€λμλ μ€μ ν©λλ€.
νμ΄μ§ μ’ λ₯μ λ°λΌ λ€λ₯Έ μ½λ°± λ©μλλ₯Ό μ§μ ν μ μμ΅λλ€.
async def parse(self, response: Response):
for link in response.css("a.product-link::attr(href)").getall():
yield response.follow(link, callback=self.parse_product)
async def parse_product(self, response: Response):
yield {
"name": response.css("h1::text").get(""),
"price": response.css(".price::text").get(""),
}
!!! note
λͺ¨λ μ½λ°± λ©μλλ `async def`μ `yield`λ₯Ό μ¬μ©νλ λΉλκΈ° μ λλ μ΄ν°μ¬μΌ ν©λλ€.
λ°μ΄ν° λ΄λ³΄λ΄κΈ° {#exporting-data}
result.itemsμ ItemListμλ λ΄λ³΄λ΄κΈ° λ©μλκ° μμ΅λλ€.
result = QuotesSpider().start()
# Export as JSON
result.items.to_json("quotes.json")
# Export as JSON with pretty-printing
result.items.to_json("quotes.json", indent=True)
# Export as JSON Lines (one JSON object per line)
result.items.to_jsonl("quotes.jsonl")
# Export as CSV or XML
result.items.to_csv("quotes.csv")
result.items.to_xml("quotes.xml")
λͺ¨λ λ©μλλ μμ λλ ν°λ¦¬κ° μμΌλ©΄ μλμΌλ‘ λ§λλλ€.
to_csv()λ μ 체 νλͺ©μμ λ°κ²¬ν λͺ¨λ ν€λ₯Ό μ΄λ‘ λ§λλλ€. νλͺ©λ§λ€ ν€κ° λ¬λΌλ λ΄λ³΄λΌ μ μμΌλ©°, κ°μ΄ μλ μ
μ λΉμ λ‘λλ€. μ΄κ³Ό μμλ₯Ό μ§μ μ ννλ €λ©΄ fields=[...]λ₯Ό, TSV νμΌμ λ§λ€λ €λ©΄ delimiter="\t"λ₯Ό μ λ¬νμΈμ. to_xml()μ κ° νλͺ©μ <item> μμλ‘ κ°μΈκ³ <items> λ£¨νΈ μμ λ£μ΅λλ€. λ μ΄λ¦μ root_tagμ item_tagλ‘ λ³κ²½ν μ μμ΅λλ€.
λ νμ λͺ¨λ μ€μ²© λμ λ리λ 리μ€νΈμ²λΌ λ¨μν μ€μΉΌλΌκ° μλ κ°μ JSONμΌλ‘ κΈ°λ‘ν©λλ€. λ°μ΄ν°κ° μλ¦Ό μμ΄ μ¬λΌμ§μ§ μμ΅λλ€.
λλ©μΈ μ ννκΈ° {#filtering-domains}
allowed_domainsλ‘ λ°©λ¬Έν λλ©μΈμ μ ννμΈμ. μΈλΆ μΉμ¬μ΄νΈμ λ§ν¬λ₯Ό μ€μλ‘ λ°λΌκ°λ μΌμ λ§μ μ μμ΅λλ€.
class MySpider(Spider):
name = "my_spider"
start_urls = ["https://example.com"]
allowed_domains = {"example.com"}
async def parse(self, response: Response):
for link in response.css("a::attr(href)").getall():
# Links to other domains are silently dropped
yield response.follow(link, callback=self.parse)
νμ λλ©μΈλ μλμΌλ‘ νμ©ν©λλ€. μλ₯Ό λ€μ΄ allowed_domains = {"example.com"}μ sub.example.com, blog.example.com λ±λ νμ©ν©λλ€.
μ μΈν μμ² μλ stats.offsite_requests_countμμ νμΈν μ μμ΅λλ€.
robots.txt μ€μνκΈ° {#robotstxt-compliance}
robots_txt_obey = Trueλ₯Ό μ€μ νλ©΄ κ° λλ©μΈμ ν¬λ‘€λ§νκΈ° μ μ robots.txt κ·μΉμ νμΈν©λλ€.
class PoliteSpider(Spider):
name = "polite"
start_urls = ["https://example.com"]
robots_txt_obey = True
async def parse(self, response: Response):
for link in response.css("a::attr(href)").getall():
yield response.follow(link, callback=self.parse)
νμ±ννλ©΄ λ€μκ³Ό κ°μ΄ λμν©λλ€.
- ν¬λ‘€λ§ μ μ
start_urlsμ λͺ¨λ λλ©μΈμμ robots.txtλ₯Ό 미리 κ°μ Έμ΅λλ€. μ¬λ¬ λλ©μΈμ λμμ μ²λ¦¬ν©λλ€. - λͺ¨λ μμ²μ νμΈνμ¬ λλ©μΈμ robots.txt
Disallowκ·μΉμ μ μ©ν©λλ€. κΈμ§λ μμ²μ μλ¦Ό μμ΄ μ μΈνκ³stats.robots_disallowed_countμ μ§κ³ν©λλ€. Crawl-delayμRequest-rateμ§μλ¬Έμ μ€μν©λλ€. μ§μλ¬Έμ΄ μꡬνλ μ§μ°κ³Ό μ€μ νdownload_delayμ€ ν° κ°μ μ¬μ©ν©λλ€. λ°λΌμ robots.txtλ μ€μ ν μ§μ°μ μ€μ΄μ§ μκ³ νμν λλ§ λ립λλ€. AutoThrottleμ΄ νμ±νλμ΄ μμΌλ©΄ μ΅λ μ§μ° μ€μ λ μ μ©λ©λλ€. νμν μ§μ°μ μ§ν¬ μ μλλ‘autothrottle_max_delayλ₯Ό μΆ©λΆν ν¬κ² μ€μ νμΈμ.
robots.txt νμΌμ κΈ°λ³Έ μΈμ
μΌλ‘ κ°μ Έμ€λ©°, ν¬λ‘€λ§μ΄ λλ λκΉμ§ λλ©μΈλ³λ‘ μΊμν©λλ€. ν¬λ‘€λ§ μ€ λ°κ²¬ν λλ©μΈμ΄ start_urlsμ μλ€λ©΄ ν΄λΉ λλ©μΈμ μ²μ μμ²ν λ robots.txtλ₯Ό κ°μ Έμ΅λλ€.
μ°Έκ³ : μμμΉ λͺ»ν λμμ νΌνκΈ° μν΄ robots_txt_obeyλ κΈ°λ³Έμ μΌλ‘ κΊΌμ Έ μμ΅λλ€. νμ±νν΄λ λμ μμ² μ€μ μΈ concurrent_requests, concurrent_requests_per_domainμ λ°λμ§ μμ΅λλ€. μμ² μ¬μ΄μ μ§μ°λ§ μ‘°μ ν©λλ€.
download_delayλ₯Ό μ§μ μ νκ³ μΆμ§ μλ€λ©΄ autothrottle_enabled = Trueλ₯Ό μ€μ νμΈμ. μ€νμ΄λκ° κ° λλ©μΈμ μλ΅ μλμ λ§μΆ° μ§μ°μ μ‘°μ νκ³ , μ°¨λ¨λλ©΄ μλλ₯Ό μ€μ
λλ€. AutoThrottleμ μ°Έκ³ νμΈμ.
λ€μ λ¨κ³ {#whats-next}
κΈ°λ³Έ μ¬νμ μ΅νλ€λ©΄ λ€μ λ΄μ©μ μ΄ν΄λ³΄μΈμ.
- μμ²κ³Ό μλ΅: μμ² μ°μ μμ, μ€λ³΅ μ κ±°, λ©νλ°μ΄ν° λ±μ μμλ΄ λλ€.
- μΈμ : ν μ€νμ΄λμμ HTTP, λΈλΌμ°μ , μ€ν μ€ λ± μ¬λ¬ Fetcher μ’ λ₯λ₯Ό μ¬μ©ν©λλ€.
- νλ‘μμ μ°¨λ¨ μ²λ¦¬: μμ²λ§λ€ νλ‘μλ₯Ό λ°κΎΈκ³ μ°¨λ¨μ μ²λ¦¬ν©λλ€.
- κ³ κΈ κΈ°λ₯: λμ μμ² μ μ΄, AutoThrottle, μΌμ μ€μ§μ μ¬κ°, μ€νΈλ¦¬λ°, μλͺ μ£ΌκΈ° ν , λ‘κΉ μ μμλ΄ λλ€.