๐ท๏ธ 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: "์ ์ํ ํ์ฑ์ผ๋ก HTML ์์๋ฅผ ์ถ์ ํ๊ณ ๋ค์ ์ฐพ๊ธฐ" description: "์์์ ํน์ง์ ์ ์ฅํ๊ณ HTML์ด ๋ฐ๋ ๋ค์๋ ์ผ์นํ๋ ์์๋ฅผ ์ฐพ์ผ์ธ์. ์ ์ํ ํ์ฑ์ ์ผ๊ณ save, retrieve, relocate๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์๋ด ๋๋ค."
์ ์ํ ์คํฌ๋ํ {#adaptive-scraping}
!!! success "์ฌ์ ์ค๋น"
1. [์์ ์ฐพ๊ธฐ](../parsing/selection.md)๋ฅผ ์ฝ๊ณ [Selector](../parsing/main_classes.md#selector) ๊ฐ์ฒด์์ ์์๋ฅผ ์ฐพ๊ณ ์ถ์ถํ๋ ๋ฐฉ๋ฒ์ ์์๋์ธ์.
2. [์ฃผ์ ํด๋์ค](../parsing/main_classes.md)๋ฅผ ์ฝ๊ณ [Selector](../parsing/main_classes.md#selector) ํด๋์ค๋ฅผ ์์๋์ธ์.
์ ์ํ ์คํฌ๋ํ์ ์ด์ ์ automatch๋ผ๊ณ ๋ถ๋ ธ๋ Scrapling์ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ ๋๋ค. ์์๋ฅผ ์ถ์ ํ๊ณ ๋ค์ ์ฐพ์์ ์ฌ์ดํธ๊ฐ ๋ฐ๋์ด๋ ์คํฌ๋ํผ๊ฐ ๊ณ์ ์๋ํ๋๋ก ๋์ต๋๋ค.
๋ค์๊ณผ ๊ฐ์ ๊ตฌ์กฐ์ ํ์ด์ง์์ ๋ฐ์ดํฐ๋ฅผ ์์งํ๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค.
<div class="container">
<section class="products">
<article class="product" id="p1">
<h3>Product 1</h3>
<p class="description">Description 1</p>
</article>
<article class="product" id="p2">
<h3>Product 2</h3>
<p class="description">Description 2</p>
</article>
</section>
</div>
ID๊ฐ p1์ธ ์ฒซ ๋ฒ์งธ ์ํ์ ์ถ์ถํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ ํ์๋ฅผ ์์ฑํ ๊ฒ์
๋๋ค.
page.css('#p1')
๊ทธ๋ฐ๋ฐ ์ฌ์ดํธ ์ด์์๊ฐ ๊ตฌ์กฐ๋ฅผ ๋ค์์ฒ๋ผ ๋ฐ๊พธ๋ฉด,
<div class="new-container">
<div class="product-wrapper">
<section class="products">
<article class="product new-class" data-id="p1">
<div class="product-info">
<h3>Product 1</h3>
<p class="new-description">Description 1</p>
</div>
</article>
<article class="product new-class" data-id="p2">
<div class="product-info">
<h3>Product 2</h3>
<p class="new-description">Description 2</p>
</div>
</article>
</section>
</div>
</div>
์ ํ์๊ฐ ๋๋ ์๋ํ์ง ์์ ์ฝ๋๋ฅผ ์์ ํด์ผ ํฉ๋๋ค. ์ด๋ Scrapling์ adaptive ๊ธฐ๋ฅ์ด ๋์์ด ๋ฉ๋๋ค.
Scrapling์์๋ ์์๋ฅผ ์ฒ์ ์ ํํ ๋ adaptive ๊ธฐ๋ฅ์ ์ผค ์ ์์ต๋๋ค. ๋ค์์ ๊ฐ์ ์์๋ฅผ ์ ํํ๋๋ฐ ์ฐพ์ง ๋ชปํ๋ฉด, ์ ์ฅํ ํน์ง์ ๋ฐํ์ผ๋ก ์ฌ์ดํธ์์ ๊ฐ์ฅ ๋น์ทํ ์์๋ฅผ ์ฐพ์ต๋๋ค. AI ์์ด๋ ๊ฐ๋ฅํฉ๋๋ค :)
from scrapling import Selector, Fetcher
# Before the change
page = Selector(page_source, adaptive=True, url='example.com')
# or
Fetcher.adaptive = True
page = Fetcher.get('https://example.com')
# then
element = page.css('#p1', auto_save=True)
if not element: # One day website changes?
element = page.css('#p1', adaptive=True) # Scrapling still finds it!
# the rest of your code...
๋จผ์ ์ฌ์ฉ ์๋ฅผ ๋ณด๊ณ , ์ดํ์ ์ฌ์ฉ๋ฒ๊ณผ ๋์ ์๋ฆฌ๋ฅผ ์์ธํ ์ค๋ช ํ๊ฒ ์ต๋๋ค. ์ด ๊ธฐ๋ฅ์ CSS/XPath๋ฟ ์๋๋ผ ๋ชจ๋ ์์ ์ ํ ๋ฐฉ๋ฒ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ค์ ์ฌ์ดํธ๋ฅผ ์ฌ์ฉํ ์ {#real-world-scenario}
์ค์ ์ฌ์ดํธ๋ฅผ ์๋ก ๋ค์ด Fetcher๋ก ์์ค๋ฅผ ๊ฐ์ ธ์ ๋ณด๊ฒ ์ต๋๋ค. ์๋๋ผ๋ฉด ๋์์ธ์ด๋ ๊ตฌ์กฐ๋ฅผ ๋ฐ๊พธ๊ธฐ ์ง์ ์ธ ์ฌ์ดํธ๋ฅผ ์ฐพ์ ์์ค๋ฅผ ์ ์ฅํ๊ณ ๋ณ๊ฒฝ์ ๊ธฐ๋ค๋ ค์ผ ํฉ๋๋ค. ์ฌ์ดํธ ์ด์์๋ฅผ ์์ง ๋ชปํ๋ฉด ๋ณ๊ฒฝ ์์ ์ ๋ฏธ๋ฆฌ ์๊ธฐ๋ ๊ฑฐ์ ๋ถ๊ฐ๋ฅํฉ๋๋ค. ์ด์์๋ฅผ ์๋ค๋ฉด ์ฐ์ถํ ํ ์คํธ๊ฐ ๋๊ฒ ์ฃ :)
๊ทธ๋์ The Web Archive์ Wayback Machine์ ์ฌ์ฉํ๊ฒ ์ต๋๋ค. 2010๋
StackOverFlow ์ฌ์ดํธ์ ์ฌ๋ณธ์
๋๋ค. ๊ฝค ์ค๋๋์ฃ ?
๊ฐ์ ์ ํ์๋ก 2010๋
๋์์ธ๊ณผ ํ์ฌ ๋์์ธ์์ ๋์ผํ ๋ฒํผ์ ์ฐพ์ ์ ์๋์ง ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค :)
์ด์ ๋์์ธ์์ Questions ๋ฒํผ์ ์ถ์ถํ๋ ค๋ฉด #hmenus > div:nth-child(1) > ul > li:nth-child(1) > a ๊ฐ์ ์ ํ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. Google Chrome์ด ์์ฑํ ์ ํ์๋ผ ์ง๋์น๊ฒ ๊ตฌ์ฒด์ ์
๋๋ค.
์ด์ ๋ ๋ฒ์ ์์ ๊ฐ์ ์ ํ์๋ฅผ ์ํํด ๋ณด๊ฒ ์ต๋๋ค.
from scrapling import Fetcher
selector = '#hmenus > div:nth-child(1) > ul > li:nth-child(1) > a'
old_url = "https://web.archive.org/web/20100102003420/http://stackoverflow.com/"
new_url = "https://stackoverflow.com/"
Fetcher.configure(adaptive = True, adaptive_domain='stackoverflow.com')
page = Fetcher.get(old_url, timeout=30)
element1 = page.css(selector, auto_save=True)[0]
# Same selector but used in the updated website
page = Fetcher.get(new_url)
element2 = page.css(selector, adaptive=True)[0]
if element1.text == element2.text:
print('Scrapling found the same element in the old and new designs!') # Spoiler alert: it does!
์ฌ๊ธฐ์๋ adaptive_domain์ด๋ผ๋ ์ ์ธ์๋ฅผ ์ฌ์ฉํ์ต๋๋ค. Scrapling์ ๋ ๋๋ฉ์ธ(archive.org์ stackoverflow.com)์ ๋ค๋ฅธ ์ฌ์ดํธ๋ก ๋ณด๋ฏ๋ก adaptive ๋ฐ์ดํฐ๋ฅผ ๋ฐ๋ก ์ ์ฅํฉ๋๋ค. ๋ ์ฃผ์๊ฐ ๊ฐ์ ์ฌ์ดํธ์์ ์๋ ค์ฃผ๋ ค๋ฉด, ์์ชฝ์ adaptive ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๋ ๊ฐ์ ์ฌ์ฉ์ ์ง์ ๋๋ฉ์ธ์ ์ ๋ฌํด์ผ ํฉ๋๋ค.
์ค์ ์ํฉ์์๋ ์ฝ๋๋ ๊ฐ์ง๋ง ๋ ์์ฒญ์ ๊ฐ์ URL์ ์ฌ์ฉํ๋ฏ๋ก adaptive_domain์ ํ์ํ์ง ์์ต๋๋ค. ๊ฐ๋ฅํ ํ ์ค์ ์ํฉ์ ๊ฐ๊น๊ฒ ๋ง๋ ์์
๋๋ค. ์ดํดํ๋ ๋ฐ ๋์์ด ๋์์ผ๋ฉด ์ข๊ฒ ์ต๋๋ค :)
์์ ๋ ์์์๋ Selector์ Fetcher ํด๋์ค๋ฅผ ๋ชจ๋ ์ฌ์ฉํด ์ ์ํ ๊ธฐ๋ฅ์ ๋์ ๋ฐฉ์์ด ๊ฐ๋ค๋ ๊ฒ์ ๋ณด์ฌ์ค๋๋ค.
!!! info "์ ๋ณด"
`adaptive_domain`์ ์ฌ์ดํธ๊ฐ ๋์์ธ์ด๋ ๊ตฌ์กฐ์ ํจ๊ป URL๋ ๋ฐ๊ฟ ๋๋ฅผ ์ํด ๋ง๋ค์์ต๋๋ค. ์ด ์ธ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ URL์์๋ ์ด์ ์ ์ ์ฅํ ์ ์ํ ๋ฐ์ดํฐ๋ฅผ ๊ณ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด Scrapling์ ์ ์ฌ์ดํธ๋ก ํ๋จํ๊ณ ์ด์ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ์ง ์์ต๋๋ค.
์ ์ํ ์คํฌ๋ํ์ ๋์ ์๋ฆฌ {#how-the-adaptive-scraping-feature-works}
์ ์ํ ์คํฌ๋ํ์ ๋ ๋จ๊ณ๋ก ๋์ํฉ๋๋ค.
- ์ ์ฅ ๋จ๊ณ: ์์๋ฅผ ๊ตฌ๋ถํ๋ ํน์ง์ ์ ์ฅํฉ๋๋ค.
- ์ผ์น ์์ ์ฐพ๊ธฐ ๋จ๊ณ: ๋์ค์ ๋น์ทํ ํน์ง์ ๊ฐ์ง ์์๋ฅผ ์ฐพ์ต๋๋ค.
์ด๋ค ๋ฐฉ๋ฒ์ผ๋ก๋ ์์๋ฅผ ์ ํํ ๋ค, ๋ค์์ ์ฌ์ดํธ๋ฅผ ์์งํ ๋ ๊ตฌ์กฐ๋ ๋์์ธ์ด ๋ฐ๋์ด๋ ๊ฐ์ ์์๋ฅผ ์ฐพ๊ณ ์ถ๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค.
๊ธฐ์ ์ ์ธ ์ค๋ช ์ ์ค์ด๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์๋์์ ์ค๋ช ํ ๋ฐฉ๋ฒ ์ค ํ๋๋ก ์์์ ํน์ง์ ์ ์ฅํ๋๋ก Scrapling์ ์์ฒญํฉ๋๋ค.
Scrapling์ ์ค์ ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ฐ ์์์ ํน์ง์ ์ ์ฅํฉ๋๋ค. ๊ธฐ๋ณธ๊ฐ์ SQLite์ ๋๋ค.
์ฌ์ดํธ ์ด์์๋ ์์์ ๋ชจ๋ ์์ฑ์ ๋ฐ๊พธ๊ฑฐ๋ ์ง์ธ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ์์ ์์ฒด์ ์์ฑ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ณ ์ ์๋ณ์๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ ์ฅ ์์คํ ์ ๋ ๊ฐ์ง ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.
- ํ์ฌ ์ฌ์ดํธ์ ๋๋ฉ์ธ์
๋๋ค.
Selector๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ์ด๊ธฐํํ ๋ ์ ๋ฌํ์ธ์. Fetcher๋ URL์์ ๋๋ฉ์ธ์ ์๋์ผ๋ก ๊ฐ์ ธ์ต๋๋ค. - ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์์์ ํน์ง์ ์กฐํํ
identifier์ ๋๋ค. ํญ์ ์ง์ ์ง์ ํด์ผ ํ๋ ๊ฒ์ ์๋๋ฉฐ, ๋ค์์ ์ค๋ช ํฉ๋๋ค.
๋์ค์ ์ด ๋ ๊ฐ์ ํจ๊ป ์ฌ์ฉํด ์ ์ฅ๋ ์์์ ํน์ง์ ๋ถ๋ฌ์ต๋๋ค.
- ํ์ฌ ์ฌ์ดํธ์ ๋๋ฉ์ธ์
๋๋ค.
์ฌ์ดํธ ๊ตฌ์กฐ๊ฐ ๋ฐ๋๋ฉด
adaptive๋ฅผ ์ผ์ ์์๋ฅผ ์ฐพ์ต๋๋ค. Scrapling์ ์ ์ฅํ ํน์ง์ ๋ถ๋ฌ์ ํ์ด์ง์ ๋ชจ๋ ์์์ ๋น๊ตํ๊ณ , ์ํ๋ ์์์์ ์ ์ฌ๋๋ฅผ ๊ณ์ฐํฉ๋๋ค. ์๋์์ ๋ณด๋ฏ ์ฌ๋ฌ ํน์ง์ ๋ชจ๋ ๊ณ ๋ คํฉ๋๋ค.๊ฐ์ฅ ๋น์ทํ ์์๋ฅผ ๋ฐํํฉ๋๋ค. ์ ์๊ฐ ๊ฐ์ ์์๊ฐ ์ฌ๋ฌ ๊ฐ๋ผ๋ฉด ํจ๊ป ๋ฐํํฉ๋๋ค.
์์๋ฅผ ๊ตฌ๋ถํ๋ ํน์ง {#the-unique-properties}
์์์ ๋ชจ๋ ์์ฑ์ด ๋ฐ๋๊ฑฐ๋ ์ฌ๋ผ์ง ์ ์๋๋ฐ ์ด๋ค ํน์ง์ ์ ์ฅํ๋์ง ๊ถ๊ธํ ์ ์์ต๋๋ค.
Scrapling์ ๋ค์ ํน์ง์ ์ฌ์ฉํฉ๋๋ค.
- ์์์ ํ๊ทธ ์ด๋ฆ, ํ ์คํธ, ์์ฑ์ ์ด๋ฆ๊ณผ ๊ฐ, ํ์ ์์์ ํ๊ทธ ์ด๋ฆ, ๊ฒฝ๋ก์ ์๋ ํ๊ทธ ์ด๋ฆ.
- ๋ถ๋ชจ ์์์ ํ๊ทธ ์ด๋ฆ, ์์ฑ์ ์ด๋ฆ๊ณผ ๊ฐ, ํ ์คํธ.
์์ ํ ๊ฐ์์ง๋ฅผ ๋น๊ตํ๋ ๊ฒ์ด ์๋๋ผ ๊ฐ์ด ์ผ๋ง๋ ๋น์ทํ์ง๋ฅผ ๋น๊ตํฉ๋๋ค. ๊ฐ์ ์์๋ ๊ณ ๋ คํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋ณ๊ฒฝ ์ ํ์ ์์์ ํด๋์ค ์ด๋ฆ์ด ์ด๋ค ์์๋ก ์ฐ์๋์ง๋ ๋น๊ตํฉ๋๋ค.
์ ์ํ ๊ธฐ๋ฅ ์ฌ์ฉํ๊ธฐ {#how-to-use-adaptive-feature}
์ ์ํ ๊ธฐ๋ฅ์ ์ฐพ์ ๋ชจ๋ ์์์ ์ ์ฉํ ์ ์์ต๋๋ค. ์์์ ๋ณธ ๊ฒ์ฒ๋ผ CSS/XPath ์ ํ ๋ฉ์๋์ ์ธ์๋ก๋ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ๋ค์์ ๋ค์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
๋จผ์ Selector๋ฅผ ์ด๊ธฐํํ ๋ adaptive=True๋ฅผ ์ ๋ฌํด adaptive๋ฅผ ์ผ์ธ์. ๋๋ ์ฌ์ฉํ๋ Fetcher์์ ๊ฐ์ ๊ธฐ๋ฅ์ ์ผ๋ฉด ๋ฉ๋๋ค.
์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
from scrapling import Selector, Fetcher
page = Selector(html_doc, adaptive=True)
# OR
Fetcher.adaptive = True
page = Fetcher.get('https://example.com')
Selector๋ฅผ ์ฌ์ฉํ ๋๋ ์ฌ์ดํธ URL์ url ์ธ์๋ก ์ ๋ฌํ์ธ์. Scrapling์ด ๊ฐ ์์์ ํน์ง์ ๋๋ฉ์ธ๋ณ๋ก ๊ตฌ๋ถํด์ ์ ์ฅํ ์ ์์ต๋๋ค.
URL์ ์ ๋ฌํ์ง ์์ผ๋ฉด ์์์ ํน์ง์ ์ ์ฅํ ๋ URL ๋์ default๋ผ๋ ๊ฐ์ ์ฌ์ฉํฉ๋๋ค. ๋์ค์ ๋ค๋ฅธ ์ฌ์ดํธ์์ ๊ฐ์ ์๋ณ์๋ฅผ ์ฐ๋ฉด์ URL๋ ์ ๋ฌํ์ง ์์ผ๋ฉด ๋ฌธ์ ๊ฐ ์๊ธธ ์ ์์ต๋๋ค. ์ ์ฅํ ๋ ์ด์ ๋ฐ์ดํฐ๋ฅผ ๋ฎ์ด์ฐ๋ฉฐ, adaptive๋ ๊ฐ์ฅ ์ต๊ทผ์ ์ ์ฅํ ํน์ง๋ง ์ฌ์ฉํฉ๋๋ค.
storage์ storage_args๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ์ค์ ํ๋ ์ธ์์
๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ ๊ณตํ๋ SQLite ํด๋์ค๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ง์ ์ ์ฅ ์์คํ
์ ๋ง๋ค ๋๋ง ํ์ํ ์ค์ ์ด๋ฉฐ, ๊ฐ๋ฐ ์น์
์ ๋ณ๋ ํ์ด์ง์์ ์ค๋ช
ํฉ๋๋ค.
์ด์ adaptive๋ฅผ ์ ์ฒด ์ค์ ์์ ์ผฐ์ผ๋ฏ๋ก ๋ ๊ฐ์ง ์ฃผ์ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
CSS/XPath ์ ํ์ผ๋ก ์ฌ์ฉํ๊ธฐ {#the-cssxpath-selection-way}
์ ์์ฒ๋ผ ํ์ด์ง์ ์์๊ฐ ์์ง ์์ ๋ auto_save ์ธ์๋ก ํน์ง์ ์ ์ฅํฉ๋๋ค.
element = page.css('#p1', auto_save=True)
์์๊ฐ ๋๋ ์ ํ๋์ง ์์ผ๋ฉด ๊ฐ์ ์ ํ์์ adaptive ์ธ์๋ฅผ ์ฌ์ฉํด ๋ค์ ์ฐพ์ ์ ์์ต๋๋ค.
element = page.css('#p1', adaptive=True)
๊ฐ๋จํ์ฃ ?
๋ด๋ถ์์๋ ์ฌ๋ฌ ์์
์ด ์ผ์ด๋ฉ๋๋ค. ์์์ ํน์ง์ ๋ค์ ๋ถ๋ฌ์ค๊ธฐ ์ํด ํ์ํ ์๋ณ์๋ฅผ ๊ธฐ์ตํ์๋์? css/xpath ๋ฉ์๋๋ ํธ์๋ฅผ ์ํด ์ ๋ฌํ ์ ํ์๋ฅผ ์๋ณ์๋ก ์๋ ์ฌ์ฉํฉ๋๋ค :)
์ด ๋ฉ์๋๋ค์ identifier ์ธ์๋ฅผ ์ ๋ฌํ๋ฉด ์๋ณ์๋ฅผ ์ง์ ์ง์ ํ ์๋ ์์ต๋๋ค. auto_save๋ก ํน์ง์ ์ ์ฅํ ๋๋ฅผ ๋น๋กฏํด ์ฌ๋ฌ ์ํฉ์์ ์ ์ฉํฉ๋๋ค.
์ง์ ์ ์ฅํ๊ณ ์ฐพ๊ธฐ {#the-manual-way}
์์์ ํน์ง์ ์ง์ ์ ์ฅํ๊ณ ๋ถ๋ฌ์จ ๋ค, adaptive ๊ธฐ๋ฅ์ผ๋ก ์์๋ฅผ ๋ค์ ์ฐพ์ ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ด๋ค ๋ฐฉ๋ฒ์ผ๋ก ์ ํํ ์์๋ผ๋ ๋ค์ ์ฐพ์ ์ ์์ต๋๋ค.
๋จผ์ ํ ์คํธ๋ก ์์๋ฅผ ์ฐพ์๋ค๊ณ ๊ฐ์ ํ๊ฒ ์ต๋๋ค.
element = page.find_by_text('Tipping the Velvet', first_match=True)
save ๋ฉ์๋๋ก ํน์ง์ ์ ์ฅํ ์ ์์ต๋๋ค. ์ด๋๋ ์๋ณ์๋ฅผ ์ง์ ์ง์ ํด์ผ ํฉ๋๋ค. ์์์๋ my_special_element๋ฅผ ์ฌ์ฉํ์ง๋ง, ์ค์ ์ฝ๋์์๋ ๋ณ์ ์ด๋ฆ์ฒ๋ผ ์๋ฏธ๊ฐ ๋ถ๋ช
ํ ์ด๋ฆ์ ์ฐ๋ ๊ฒ์ด ์ข์ต๋๋ค :)
page.save(element, 'my_special_element')
๋์ค์ ํน์ง์ ๋ถ๋ฌ์ adaptive๋ก ํ์ด์ง์์ ์์๋ฅผ ๋ค์ ์ฐพ์ผ๋ ค๋ฉด ๋ค์์ฒ๋ผ ์์ฑํฉ๋๋ค.
>>> element_dict = page.retrieve('my_special_element')
>>> page.relocate(element_dict, selector_type=True)
[<data='<a href="catalogue/tipping-the-velvet_99...' parent='<h3><a href="catalogue/tipping-the-velve...'>]
>>> page.relocate(element_dict, selector_type=True).css('::text').getall()
['Tipping the Velvet']
์ฌ๊ธฐ์๋ retrieve์ relocate ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
๊ฒฐ๊ณผ๋ฅผ lxml.etree ๊ฐ์ฒด๋ก ์ ์งํ๋ ค๋ฉด selector_type ์ธ์๋ฅผ ์๋ตํ์ธ์.
>>> page.relocate(element_dict)
[<Element a at 0x105a2a7b0>]
๋ฌธ์ ํด๊ฒฐ {#troubleshooting}
์ผ์นํ๋ ์์๊ฐ ์์ ๋ {#no-matches-found}
# 1. Check if data was saved
element_data = page.retrieve('identifier')
if not element_data:
print("No data saved for this identifier")
# 2. Try with different identifier
products = page.css('.product', adaptive=True, identifier='old_selector')
# 3. Save again with new identifier
products = page.css('.new-product', auto_save=True, identifier='new_identifier')
๋ค๋ฅธ ์์๊ฐ ์ ํ๋ ๋ {#wrong-elements-matched}
# Use more specific selectors
products = page.css('.product-list .product', auto_save=True)
# Or save with more context
product = page.find_by_text('Product Name').parent
page.save(product, 'specific_product')
์๋ ค์ง ์ ํ ์ฌํญ {#known-issues}
adaptive๋ก ์ ์ฅํ ๋๋ ์ ํ ๊ฒฐ๊ณผ์ ์ฒซ ๋ฒ์งธ ์์์ ๋ํ ํน์ง๋ง ์ ์ฅํฉ๋๋ค. ์ ํ์๊ฐ ํ์ด์ง์ ์ฌ๋ฌ ์์น์์ ๋ค๋ฅธ ์์๋ค์ ์ ํํ๋๋ผ๋, ๋์ค์ adaptive๋ก ๋ค์ ์ฐพ์ผ๋ฉด ์ฒซ ๋ฒ์งธ ์์๋ง ๋ฐํํฉ๋๋ค. ์ผํ๋ก ๋ฌถ์ CSS ์ ํ์๋ ์์ธ์
๋๋ค. ๊ฐ ์ ํ์๋ฅผ ๋ถ๋ฆฌํด ๋ฐ๋ก ์คํํ๊ธฐ ๋๋ฌธ์
๋๋ค.
๋ง๋ฌด๋ฆฌ {#final-thoughts}
์ด ๊ธฐ๋ฅ์ ์ฝ๊ณ ์์ธํ๊ฒ ์ค๋ช ํ๊ธฐ๊ฐ ์๊ฐ๋ณด๋ค ์ด๋ ค์ ์ต๋๋ค. ์์ง ๊ถ๊ธํ ์ ์ด ์๋ค๋ฉด ํ ๋ก ๊ฒ์ํ, Discord ์๋ฒ ๋๋ ๊ฐ์ธ ๋ฉ์์ง๋ก ์๋ ค์ฃผ์ธ์. ๊ฐ๋ฅํ ํ ๋นจ๋ฆฌ ๋ตํ๊ฒ ์ต๋๋ค :)