π·οΈ 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: "CSS, XPath, ν μ€νΈ, μ κ·μμΌλ‘ HTML λ°μ΄ν° μΆμΆνκΈ°" description: "CSS, XPath, ν μ€νΈ, μ κ·μμΌλ‘ HTML μμλ₯Ό μ ννμΈμ. Scrapling μμ λ₯Ό ν΅ν΄ λΉμ·ν μμλ₯Ό μ°Ύκ³ νμν κ°μ μΆμΆν©λλ€."
μμ μ°ΎκΈ° {#querying-elements}
Selectorλ HTMLμ νμ±ν©λλ€. μ μν κΈ°λ₯μ΄ XMLμμλ μλνμ§ μκΈ° λλ¬Έμ
λλ€. XML, RSS, Atom νΌλλ XML νμλ₯Ό μ¬μ©ν΄ κ° νΌλ λ
Έλλ₯Ό μ§μ μ λ¬νλ XMLFeedSpiderλ₯Ό μ¬μ©νμΈμ.
Scraplingμμ μμλ₯Ό μ°Ύλ μ£Όμ λ°©λ²μ λ€μ― κ°μ§μ λλ€.
- CSS3 μ νμ.
- XPath μ νμ.
- νν°λ 쑰건μΌλ‘ μ°ΎκΈ°.
- νΉμ ν μ€νΈλ₯Ό ν¬ν¨νλ μμ μ°ΎκΈ°.
- νΉμ μ κ·μκ³Ό μΌμΉνλ λ΄μ©μ κ°μ§ μμ μ°ΎκΈ°.
κ°μ μ μΌλ‘ μμλ₯Ό μ°Ύλ λ€λ₯Έ λ°©λ²λ μμ§λ§, μ¬κΈ°μλ μ£Όμ λ°©λ²μ μμΈν λ€λ£Ήλλ€. μ΄λ―Έ μ°Ύμ μμμ λΉμ·ν μμλ₯Ό μ°Ύλ κΈ°λ₯λ μ€λͺ ν©λλ€. ν΄λΉ μΉμ μΌλ‘ λ°λ‘ μ΄λν μλ μμ΅λλ€.
μΉ μ€ν¬λνμ΄ μ²μμ΄κ³ μ νμλ₯Ό μμ±ν κ²½νμ΄ μ λ€λ©΄, μ¬κΈ°μ find/find_all λ©μλλΆν° λ°°μ°λ κ²μ κΆμ₯ν©λλ€.
CSS/XPath μ νμ {#cssxpath-selectors}
CSS μ νμλ? {#what-are-css-selectors}
CSSλ HTML λ¬Έμμ μ€νμΌμ μ μ©νλ μΈμ΄μ λλ€. μ νμλ μ€νμΌμ μ μ©ν HTML μμλ₯Ό μ§μ ν©λλ€.
Scraplingμ W3C λͺ
μΈμ λ°λ₯Έ CSS3 μ νμλ₯Ό μ§μν©λλ€. CSS μ§μμλ cssselectλ₯Ό μ¬μ©νλ―λ‘, μ§μνλ μ νμμ μμ¬ ν¨μ λ° μμ¬ μμλ ν΄λΉ λ¬Έμλ₯Ό μ°Έκ³ νμΈμ.
Scraplingμ λ€μκ³Ό κ°μ λΉνμ€ μμ¬ μμλ μ§μν©λλ€.
- ν
μ€νΈ λ
Έλλ
::textλ‘ μ νν©λλ€. - μμ±κ°μ
::attr(name)μΌλ‘ μ νν©λλ€. nameμ κ°μ κ°μ Έμ¬ μμ±μ μ΄λ¦μ λλ€.
Scrapyλ Parselμ μ¬μ©ν΄ λ΄€λ€λ©΄ μ΅μν λ°©μμ λλ€. λ§μ μ¬μ©μκ° μ΄λ―Έ μ΅μν λ°©μμ κ΅³μ΄ λ°κΏ νμλ μμΌλκΉμ :)
CSSλ‘ μμλ₯Ό μ ννλ €λ©΄ css λ©μλλ₯Ό μ¬μ©ν©λλ€. κ²°κ³Όλ Selectorsμ
λλ€. [0]μΌλ‘ 첫 λ²μ§Έ μμλ₯Ό κ°μ Έμ€κ±°λ, .get() / .getall()λ‘ ν
μ€νΈλ μμ± μ ν κ²°κ³Όμ κ°μ μΆμΆν μ μμ΅λλ€.
XPath μ νμλ? {#what-are-xpath-selectors}
XPathλ XML λ¬Έμμ λ Έλλ₯Ό μ ννλ μΈμ΄μ΄λ©° HTMLμλ μ¬μ©ν μ μμ΅λλ€. μ΄ μμ½νλ XPathλ₯Ό λ°°μ°λ λ° λμμ΄ λ©λλ€. Scraplingμ lxmlμ ν΅ν΄ XPath μ νμλ₯Ό μ 곡ν©λλ€.
CSSμ λ§μ°¬κ°μ§λ‘ Scrapyλ Parselκ³Ό κ°μ λ°©μμ
λλ€. λ€λ§ Scraplingμ XPath νμ₯ ν¨μ has-classλ₯Ό μ 곡νμ§ μμ΅λλ€. λμ λ°νλ μμμμ κ°μ λͺ©μ μΌλ‘ has_class λ©μλλ₯Ό μ¬μ©ν μ μμ΅λλ€.
XPathλ‘ μμλ₯Ό μ ννλ €λ©΄ xpathλ₯Ό μ¬μ©ν©λλ€. λμ λ°©μμ μμ CSS λ©μλμ κ°μ΅λλ€.
cssμxpathμλ μ μν κΈ°λ₯μ μν μΆκ° μΈμλ μμ΅λλ€. μ μν κΈ°λ₯μ λ³λ νμ΄μ§μμ μμΈν μ€λͺ νλ―λ‘ μ¬κΈ°μλ λ€λ£¨μ§ μμ΅λλ€.
μ νμ μμ {#selectors-examples}
CSSμ XPathλ‘ κ°μ μμ μ νλ μλ₯Ό λ³΄κ² μ΅λλ€.
ν΄λμ€κ° productμΈ λͺ¨λ μμλ₯Ό μ νν©λλ€.
products = page.css('.product')
products = page.xpath('//*[@class="product"]')
!!! info "μ°Έκ³ "
μμμ λ€λ₯Έ ν΄λμ€λ μμΌλ©΄ μ΄ XPath μλ μ ννμ§ μμ΅λλ€. **ν΄λμ€λ‘ μ νν λλ CSSλ₯Ό μ¬μ©νλ νΈμ΄ μ’μ΅λλ€.**
ν΄λμ€κ° productμΈ μ²« λ²μ§Έ μμλ₯Ό μ νν©λλ€.
product = page.css('.product')[0]
product = page.xpath('//*[@class="product"]')[0]
νκ·Έκ° h1μΈ μ²« λ²μ§Έ μμμ ν
μ€νΈλ₯Ό κ°μ Έμ΅λλ€.
title = page.css('h1::text').get()
title = page.xpath('//h1//text()').get()
λ€μ μ½λμ κ°μ΅λλ€.
title = page.css('h1')[0].text
title = page.xpath('//h1')[0].text
νκ·Έκ° aμΈ μ²« λ²μ§Έ μμμ href μμ±μ κ°μ Έμ΅λλ€.
link = page.css('a::attr(href)').get()
link = page.xpath('//a/@href').get()
ν΄λμ€κ° productμΈ μμ μλμμ Phoneμ ν¬ν¨νλ 첫 λ²μ§Έ h1 μμμ ν
μ€νΈλ₯Ό κ°μ Έμ΅λλ€.
title = page.css('.product h1:contains("Phone")::text').get()
title = page.xpath('//*[@class="product"]//h1[contains(text(),"Phone")]/text()').get()
κ²°κ³Όκ° μλ ν μ νμλ₯Ό μ€μ²©νκ±°λ μ΄μ΄μ μ¬μ©ν μ μμ΅λλ€.
page.css('.product')[0].css('h1:contains("Phone")::text').get()
page.xpath('//*[@class="product"]')[0].xpath('.//h1[contains(text(),"Phone")]/text()').get()
page.xpath('//*[@class="product"]')[0].css('h1:contains("Phone")::text').get()
νμ¬ μμλ₯Ό κΈ°μ€μΌλ‘ XPathλ₯Ό μ€ννλ €λ©΄ .//λ₯Ό μ¬μ©νμΈμ. //λ‘ μμνλ©΄ μν μμλ₯Ό μ νν λ€μλ λ¬Έμ μ 체μμ κ²μν©λλ€.
λ€λ₯Έ μλ λ³΄κ² μ΅λλ€.
'href' μμ±μ 'image'κ° λ€μ΄ μλ λͺ¨λ λ§ν¬λ₯Ό μ°Ύμ΅λλ€.
links = page.css('a[href*="image"]')
links = page.xpath('//a[contains(@href, "image")]')
for index, link in enumerate(links):
link_value = link.attrib['href'] # Cleaner than link.css('::attr(href)').get()
link_text = link.text
print(f'Link number {index} points to this url {link_value} with text content as "{link_text}"')
ν μ€νΈ λ΄μ©μΌλ‘ μ ννκΈ° {#text-content-selection}
Scraplingμ μμμ μ§μ λ€μ΄ μλ ν μ€νΈλ‘ μμλ₯Ό μ νν μ μμ΅λλ€. λ κ°μ§ λ°©λ²μ΄ μμ΅λλ€.
find_by_textλ μ§μ ν ν μ€νΈλ₯Ό ν¬ν¨νλ μμλ₯Ό μ°ΎμΌλ©° μ¬λ¬ μ΅μ μ μ 곡ν©λλ€.find_by_regexλ μ§μ ν μ κ·μκ³Ό ν μ€νΈκ° μΌμΉνλ μμλ₯Ό μ°ΎμΌλ©° μ¬λ¬ μ΅μ μ μ 곡ν©λλ€.
μ κ·μμ μ΅μνλ€λ©΄ find_by_textμ μμ
μ find_by_regexλ‘λ ν μ μμ΅λλ€. νμ§λ§ λͺ¨λ μ¬μ©μκ° μ½κ² μ¬μ©ν μ μλλ‘ λ λ°©λ²μ μ 곡ν©λλ€.
find_by_textμλ 첫 λ²μ§Έ μΈμλ‘ ν
μ€νΈλ₯Ό, find_by_regexμλ μ κ·μμ μ λ¬ν©λλ€. λ λ©μλλ λ€μ μΈμλ₯Ό ν¨κ» μ§μν©λλ€.
- first_match:
Trueμ΄λ©΄ 첫 λ²μ§Έ κ²°κ³Όλ§ λ°νν©λλ€. κΈ°λ³Έκ°λ κ°μ΅λλ€. - case_sensitive:
Trueμ΄λ©΄ λμλ¬Έμλ₯Ό ꡬλΆν©λλ€. - clean_match:
Trueμ΄λ©΄ λΉκ΅ μ μ 곡백 λ¬Έμμ μ°μ 곡백μ νλμ 곡백μΌλ‘ λ°κΏλλ€.
find_by_textλ κΈ°λ³Έμ μΌλ‘ μ νν μΌμΉνλ ν
μ€νΈλ₯Ό μ°Ύμ΅λλ€. μμμ μ 체 ν
μ€νΈκ° μ
λ ₯ν ν
μ€νΈμ κ°μμΌ ν©λλ€. μ΄λ₯Ό λ°κΎΈλ μΆκ° μΈμκ° μμ΅λλ€.
- partial: μΌλ©΄
find_by_textκ° μ λ ₯ν ν μ€νΈλ₯Ό ν¬ν¨νλ μμλ₯Ό λ°νν©λλ€. μ μ²΄κ° μΌμΉν νμλ μμ΅λλ€.
!!! abstract "μ°Έκ³ "
`find_by_regex`μ 첫 λ²μ§Έ μΈμλ μΌλ° λ¬Έμμ΄μ΄λ μ»΄νμΌλ μ κ·μ λͺ¨λ κ°λ₯ν©λλ€. μλ μμμ νμΈν μ μμ΅λλ€.
λΉμ·ν μμ μ°ΎκΈ° {#finding-similar-elements}
Scraplingμ μ΄λ―Έ μ°Ύμ μμμ λΉμ·ν μμλ₯Ό μ°Ύμ μ μμ΅λλ€. AutoScraperμμ μμ΄λμ΄λ₯Ό μ»μμ§λ§, Scraplingμμλ μ΄λ€ λ°©λ²μΌλ‘ μ°Ύμ μμμλ μ μ©ν μ μμ΅λλ€. ν μ€νΈλ‘ μμλ₯Ό μ°Ύμ λ€ μ¬μ©νλ κ²½μ°κ° λ§μΌλ―λ‘ μ¬κΈ°μ μ€λͺ νκ² μ΅λλ€.
μ΄λ»κ² μλν κΉμ?
μν μ΄λ¦μΌλ‘ μμλ₯Ό μ°Ύμ λ€, κ°μ νλ 컨ν
μ΄λ μμ λ€λ₯Έ μνλ μΆμΆνκ³ μΆλ€κ³ κ°μ ν΄ λ³΄κ² μ΅λλ€. μ°Ύμ μμμμ .find_similar()λ₯Ό νΈμΆνλ©΄ Scraplingμ΄ λ€μ μμ
μ ν©λλ€.
- DOM νΈλ¦¬μμ κ°μ κΉμ΄μ μλ λͺ¨λ μμλ₯Ό μ°Ύμ΅λλ€.
- νκ·Έ μ΄λ¦, λΆλͺ¨μ νκ·Έ μ΄λ¦, μ‘°λΆλͺ¨μ νκ·Έ μ΄λ¦μ΄ κ°μ§ μμ μμλ₯Ό μ μΈν©λλ€.
- μ΄ λ¨κ³μμλ μνλ μμμΌ κ°λ₯μ±μ΄ μ½ 99%μ λλ€. λ§μ§λ§μΌλ‘ μμ±μ μ μ¬λλ₯Ό λΉκ΅ν΄ μλ μμμ μΆ©λΆν λΉμ·νμ§ μμ μμλ₯Ό μ μΈν©λλ€. μ΄λ μ¬μ©ν λΉμ¨μ μ€μ ν μ μμ§λ§, κΈ°λ³Έ μ€μ μΌλ‘ μνλ μμλ₯Ό μ°Ύμ§ λͺ»ν λλ§ λ°κΎΈλ κ²μ κΆμ₯ν©λλ€.
μ€λͺ μ΄ κΈΈμμ§λ§ λμ μ리λ₯Ό μλ €λλ¦¬κ³ μΆμμ΅λλ€. λ€μ μΉμ μμ μλ₯Ό 보기 μ μ λ©μλμ μΈμλ₯Ό μ΄ν΄λ³΄κ² μ΅λλ€.
- similarity_threshold: 3λ¨κ³μμ μμ±μ λΉκ΅νλ μ μ¬λ κΈ°μ€μ λλ€. κΈ°λ³Έκ°μ 0.2λ‘, λ μμμ μμ±μ΄ μ μ΄λ 20% λΉμ·ν΄μΌ νλ€λ λ»μ λλ€. 0μΌλ‘ μ€μ νλ©΄ μ΄ κ²μ¬λ₯Ό λ μ μμ§λ§, λ¨Όμ λ€λ₯Έ μΈμλ μ½μ΄λ³΄μΈμ.
- ignore_attributes: λ§μ§λ§ λΉκ΅μμ 무μν μμ± μ΄λ¦μ
λλ€. κΈ°λ³Έκ°μ
('href', 'src',)μ λλ€. URLμ μμλ§λ€ ν¬κ² λ¬λΌμ§ μ μμ΄ λΉκ΅ κΈ°μ€μΌλ‘ μ ν©νμ§ μκΈ° λλ¬Έμ λλ€. - match_text:
Trueμ΄λ©΄ 3λ¨κ³μμ μμμ ν μ€νΈλ λΉκ΅ν©λλ€. μΌλ°μ μΈ κ²½μ°μλ κΆμ₯νμ§ μμ§λ§ μμ μ λ°λΌ μ μ©ν μ μμ΅λλ€.
μ΄μ μμ λ₯Ό μ΄ν΄λ³΄κ² μ΅λλ€.
μμ {#examples}
μΌλ° ν μ€νΈμ μ κ·μμΌλ‘ μμλ₯Ό μ°Ύλ μμ λλ€.
μ¬κΈ°μλ Fetcher ν΄λμ€λ₯Ό μ¬μ©ν©λλ€. μμΈν μ€λͺ
μ λ€μμ λ€λ£Ήλλ€.
from scrapling.fetchers import Fetcher
page = Fetcher.get('https://books.toscrape.com/index.html')
ν μ€νΈκ° μ§μ ν λ¬Έμμ΄κ³Ό μμ ν κ°μ 첫 λ²μ§Έ μμλ₯Ό μ°Ύμ΅λλ€.
>>> page.find_by_text('Tipping the Velvet')
<data='<a href="catalogue/tipping-the-velvet_99...' parent='<h3><a href="catalogue/tipping-the-velve...'>
page.urljoinμ μ¬μ©ν΄ μλ κ²½λ‘μΈ hrefλ₯Ό μ 체 URLλ‘ λ°κΏλλ€.
>>> page.find_by_text('Tipping the Velvet').attrib['href']
'catalogue/tipping-the-velvet_999/index.html'
>>> page.urljoin(page.find_by_text('Tipping the Velvet').attrib['href'])
'https://books.toscrape.com/catalogue/tipping-the-velvet_999/index.html'
μΌμΉνλ μμκ° μ¬λ¬ κ°λΌλ©΄ λͺ¨λ κ°μ Έμ΅λλ€. κ²°κ³Όκ° λ¦¬μ€νΈλΌλ μ μ μ£Όλͺ©νμΈμ.
>>> page.find_by_text('Tipping the Velvet', first_match=False)
[<data='<a href="catalogue/tipping-the-velvet_99...' parent='<h3><a href="catalogue/tipping-the-velve...'>]
λΆλΆ μΌμΉλ₯Ό μ¬μ©ν΄ theλ₯Ό ν¬ν¨νλ λͺ¨λ μμλ₯Ό κ°μ Έμ΅λλ€.
>>> results = page.find_by_text('the', partial=True, first_match=False)
>>> [i.text for i in results]
['A Light in the ...',
'Tipping the Velvet',
'The Requiem Red',
'The Dirty Little Secrets ...',
'The Coming Woman: A ...',
'The Boys in the ...',
'The Black Maria',
'Mesaerion: The Best Science ...',
"It's Only the Himalayas"]
λμλ¬Έμλ₯Ό ꡬλΆνμ§ μμΌλ―λ‘ κ²°κ³Όμλ μλ¬Έμ theλΏ μλλΌ Theλ μμ΅λλ€. theλ§ ν¬ν¨νλ μμλ‘ μ νν΄ λ³΄κ² μ΅λλ€.
>>> results = page.find_by_text('the', partial=True, first_match=False, case_sensitive=True)
>>> [i.text for i in results]
['A Light in the ...',
'Tipping the Velvet',
'The Boys in the ...',
"It's Only the Himalayas"]
κ°κ²©μ μ°Ύλ μ κ·μκ³Ό ν μ€νΈκ° μΌμΉνλ 첫 λ²μ§Έ μμλ₯Ό κ°μ Έμ΅λλ€.
>>> page.find_by_regex(r'Β£[\d\.]+')
<data='<p class="price_color">Β£51.77</p>' parent='<div class="product_price"> <p class="pr...'>
>>> page.find_by_regex(r'Β£[\d\.]+').text
'Β£51.77'
μ»΄νμΌλ μ κ·μμ μ λ¬ν΄λ κ°μ΅λλ€. Scraplingμ΄ μ λ ₯ νμ μ μμμ μ²λ¦¬ν©λλ€.
>>> import re
>>> regex = re.compile(r'Β£[\d\.]+')
>>> page.find_by_regex(regex)
<data='<p class="price_color">Β£51.77</p>' parent='<div class="product_price"> <p class="pr...'>
>>> page.find_by_regex(regex).text
'Β£51.77'
μ κ·μκ³Ό μΌμΉνλ λͺ¨λ μμλ₯Ό κ°μ Έμ΅λλ€.
>>> page.find_by_regex(r'Β£[\d\.]+', first_match=False)
[<data='<p class="price_color">Β£51.77</p>' parent='<div class="product_price"> <p class="pr...'>,
<data='<p class="price_color">Β£53.74</p>' parent='<div class="product_price"> <p class="pr...'>,
<data='<p class="price_color">Β£50.10</p>' parent='<div class="product_price"> <p class="pr...'>,
<data='<p class="price_color">Β£47.82</p>' parent='<div class="product_price"> <p class="pr...'>,
...]
μ΄λ° μμΌλ‘ μ¬μ©ν μ μμ΅λλ€.
νμ¬ μμμ μμΉ λ° μμ±μ΄ λΉμ·ν λͺ¨λ μμλ₯Ό μ°Ύμ΅λλ€. μ¬κΈ°μλ 'title' μμ±μ λΉκ΅μμ μ μΈν©λλ€.
>>> element = page.find_by_text('Tipping the Velvet')
>>> element.find_similar(ignore_attributes=['title'])
[<data='<a href="catalogue/a-light-in-the-attic_...' parent='<h3><a href="catalogue/a-light-in-the-at...'>,
<data='<a href="catalogue/soumission_998/index....' parent='<h3><a href="catalogue/soumission_998/in...'>,
<data='<a href="catalogue/sharp-objects_997/ind...' parent='<h3><a href="catalogue/sharp-objects_997...'>,
...]
μμκ° 20κ°κ° μλλΌ 19κ°μΈ μ΄μ λ νμ¬ μμ μ체λ κ²°κ³Όμ ν¬ν¨λμ§ μκΈ° λλ¬Έμ λλ€.
>>> len(element.find_similar(ignore_attributes=['title']))
19
λΉμ·ν λͺ¨λ μμμ href μμ±μ κ°μ Έμ΅λλ€.
>>> [
element.attrib['href']
for element in element.find_similar(ignore_attributes=['title'])
]
['catalogue/a-light-in-the-attic_1000/index.html',
'catalogue/soumission_998/index.html',
'catalogue/sharp-objects_997/index.html',
...]
μ‘°κΈ λ 볡μ‘ν μλ‘, μ°Ύμ μμλ₯Ό μμμ μΌλ‘ μΌμ λͺ¨λ μ± μ λ°μ΄ν°λ₯Ό κ°μ Έμ λ³΄κ² μ΅λλ€.
>>> first_product = element.parent.parent
>>> for product in [first_product, *first_product.find_similar()]:
print({
"name": product.css('h3 a::text').get(),
"price": product.css('.price_color')[0].re_first(r'[\d\.]+'),
"stock": product.css('.availability::text').getall()[-1].clean()
})
{'name': 'Tipping the Velvet', 'price': '53.74', 'stock': 'In stock'}
{'name': 'A Light in the ...', 'price': '51.77', 'stock': 'In stock'}
{'name': 'Soumission', 'price': '50.10', 'stock': 'In stock'}
{'name': 'Sharp Objects', 'price': '47.82', 'stock': 'In stock'}
...
κ³ κΈ μμ {#advanced-examples}
find_similarλ₯Ό μ¬μ©νλ λ 볡μ‘νκ³ μ€μ©μ μΈ μμ
λλ€.
μΌνλͺ° μν μΆμΆ:
def extract_product_grid(page):
# Find the first product card
first_product = page.find_by_text('Add to Cart').find_ancestor(
lambda e: e.has_class('product-card')
)
# Find similar product cards
products = [first_product, *first_product.find_similar()]
return [
{
'name': p.css('h3::text').get(),
'price': p.css('.price::text').re_first(r'\d+\.\d{2}'),
'stock': 'In stock' in p.get_all_text(),
'rating': p.css('.rating')[0].attrib.get('data-rating')
}
for p in products
]
νμ ν μΆμΆ:
def extract_table_data(page):
# Find the first data row
first_row = page.css('table tbody tr')[0]
# Find similar rows
rows = [first_row, *first_row.find_similar()]
return [
{
'column1': row.css('td:nth-child(1)::text').get(),
'column2': row.css('td:nth-child(2)::text').get(),
'column3': row.css('td:nth-child(3)::text').get()
}
for row in rows
]
νΌ νλ μΆμΆ:
def extract_form_fields(page):
# Find first form field container
first_field = page.css('input')[0].find_ancestor(
lambda e: e.has_class('form-field')
)
# Find similar field containers
fields = [first_field, *first_field.find_similar()]
return [
{
'label': f.css('label::text').get(),
'type': f.css('input')[0].attrib.get('type'),
'required': 'required' in f.css('input')[0].attrib
}
for f in fields
]
μ¬μ΄νΈμ 리뷰 μΆμΆ:
def extract_reviews(page):
# Find first review
first_review = page.find_by_text('Great product!')
review_container = first_review.find_ancestor(
lambda e: e.has_class('review')
)
# Find similar reviews
all_reviews = [review_container, *review_container.find_similar()]
return [
{
'text': r.css('.review-text::text').get(),
'rating': r.attrib.get('data-rating'),
'author': r.css('.reviewer::text').get()
}
for r in all_reviews
]
νν°λ‘ μ°ΎκΈ° {#filters-based-searching}
μ΄ λ°©λ²μ κΈ°λ₯μ΄ λ€μνλ©΄μλ μ νμλ₯Ό μμ±νλ κ²λ³΄λ€ λ°°μ°κΈ° μ¬μ, μ€ν¬λνμ μ²μ μμν λ νΉν μ μ©ν©λλ€.
BeautifulSoupμ find_allμμ μμ΄λμ΄λ₯Ό μ»μ find_allκ³Ό findλ μ¬λ¬ νν°λ₯Ό λ°κ³ , λͺ¨λ 쑰건μ λ§λ μμλ₯Ό μ°Ύμ΅λλ€.
μΈμλ λ€μκ³Ό κ°μ΄ ν΄μν©λλ€.
- λ¬Έμμ΄μ νκ·Έ μ΄λ¦μΌλ‘ λ΄ λλ€.
- List/Tuple/Set κ°μ λ°λ³΅ κ°λ₯ν κ°μ²΄λ νκ·Έ μ΄λ¦μ λͺ¨μμΌλ‘ λ΄ λλ€.
- λμ λ리λ HTML μμμ μμ± μ΄λ¦κ³Ό κ°μ λνλ λλ€.
- μ κ·μμ
find_by_regexμ²λΌ μμμ λ΄μ©μΌλ‘ νν°λ§ν©λλ€. - ν¨μλ μμλ₯Ό νν°λ§νλ 쑰건μΌλ‘ μ¬μ©ν©λλ€.
- ν€μλ μΈμλ HTML μμ± μ΄λ¦κ³Ό κ°μΌλ‘ λ΄ λλ€.
μ λ¬ν μΈμλ₯Ό λͺ¨μ νν°λ₯Ό μ°¨λ‘λ‘ μ μ©ν©λλ€. κ° νν°μ κ²°κ³Όκ° λ€μ νν°λ‘ μ λ¬λ©λλ€.
νμ¬ νμ΄μ§λ μμ μμμ λ€μ μμλ‘ νν°λ§ν©λλ€.
- μ λ¬ν νκ·Έ μ΄λ¦μ λ§λ μμλ₯Ό λͺ¨μλλ€.
- μ λ¬ν λͺ¨λ μμ±κ³Ό μΌμΉνλ μμλ₯Ό λͺ¨μλλ€. μ λ¨κ³κ° μμλ€λ©΄ κ·Έ κ²°κ³Όλ§ νν°λ§ν©λλ€.
- μ λ¬ν λͺ¨λ μ κ·μκ³Ό μΌμΉνλ μμλ₯Ό λͺ¨μλλ€. μ λ¨κ³κ° μμλ€λ©΄ κ·Έ κ²°κ³Όλ§ νν°λ§ν©λλ€.
- μ λ¬ν λͺ¨λ ν¨μμ 쑰건μ λ§μ‘±νλ μμλ₯Ό λͺ¨μλλ€. μ λ¨κ³κ° μμλ€λ©΄ κ·Έ κ²°κ³Όλ§ νν°λ§ν©λλ€.
!!! note "μ°Έκ³ "
1. μ μμμμ μ²μμΌλ‘ μ£Όμ΄μ§ νν°λΆν° μμν©λλ€. νκ·Έ μ΄λ¦ μμ΄ μμ±λ§ μ λ¬νλ€λ©΄ 2λ¨κ³λΆν° μμν©λλ€.
2. μΈμλ₯Ό μ λ¬ν μμλ μν₯μ μ£Όμ§ μμ΅λλ€. μμμ μ€λͺ
ν μμλ§ μ¬μ©ν©λλ€.
μμ λ₯Ό 보면 λ μ½κ² μ΄ν΄ν μ μμ΅λλ€ :)
μμ {#examples_1}
from scrapling.fetchers import Fetcher
page = Fetcher.get('https://quotes.toscrape.com/')
νκ·Έκ° divμΈ λͺ¨λ μμλ₯Ό μ°Ύμ΅λλ€.
>>> page.find_all('div')
[<data='<div class="container"> <div class="row...' parent='<body> <div class="container"> <div clas...'>,
<data='<div class="row header-box"> <div class=...' parent='<div class="container"> <div class="row...'>,
...]
ν΄λμ€κ° quoteμΈ λͺ¨λ div μμλ₯Ό μ°Ύμ΅λλ€.
>>> page.find_all('div', class_='quote')
[<data='<div class="quote" itemscope itemtype="h...' parent='<div class="col-md-8"> <div class="quote...'>,
<data='<div class="quote" itemscope itemtype="h...' parent='<div class="col-md-8"> <div class="quote...'>,
...]
μμ κ°μ μμ μ λλ€.
>>> page.find_all('div', {'class': 'quote'})
[<data='<div class="quote" itemscope itemtype="h...' parent='<div class="col-md-8"> <div class="quote...'>,
<data='<div class="quote" itemscope itemtype="h...' parent='<div class="col-md-8"> <div class="quote...'>,
...]
ν΄λμ€κ° quoteμΈ λͺ¨λ μμλ₯Ό μ°Ύμ΅λλ€.
>>> page.find_all({'class': 'quote'})
[<data='<div class="quote" itemscope itemtype="h...' parent='<div class="col-md-8"> <div class="quote...'>,
<data='<div class="quote" itemscope itemtype="h...' parent='<div class="col-md-8"> <div class="quote...'>,
...]
ν΄λμ€κ° quoteμΈ div μ€, λ΄μ©μ 'world'κ° μλ .text μμλ₯Ό ν¬ν¨νλ κ²μ μ°Ύμ΅λλ€.
>>> page.find_all('div', {'class': 'quote'}, lambda e: "world" in e.css('.text::text').get())
[<data='<div class="quote" itemscope itemtype="h...' parent='<div class="col-md-8"> <div class="quote...'>]
μμμ΄ μλ λͺ¨λ μμλ₯Ό μ°Ύμ΅λλ€.
>>> page.find_all(lambda element: len(element.children) > 0)
[<data='<html lang="en"><head><meta charset="UTF...'>,
<data='<head><meta charset="UTF-8"><title>Quote...' parent='<html lang="en"><head><meta charset="UTF...'>,
<data='<body> <div class="container"> <div clas...' parent='<html lang="en"><head><meta charset="UTF...'>,
...]
λ΄μ©μ 'world'κ° μλ λͺ¨λ μμλ₯Ό μ°Ύμ΅λλ€.
>>> page.find_all(lambda element: "world" in element.text)
[<data='<span class="text" itemprop="text">βThe...' parent='<div class="quote" itemscope itemtype="h...'>,
<data='<a class="tag" href="/tag/world/page/1/"...' parent='<div class="tags"> Tags: <meta class="ke...'>]
μ§μ ν μ κ·μκ³Ό μΌμΉνλ λͺ¨λ span μμλ₯Ό μ°Ύμ΅λλ€.
>>> page.find_all('span', re.compile(r'world'))
[<data='<span class="text" itemprop="text">βThe...' parent='<div class="quote" itemscope itemtype="h...'>]
ν΄λμ€κ° 'quote'μΈ divμ spanμ μ°Ύμ΅λλ€. ν΄λΉ spanμ΄ μμΌλ―λ‘ divλ§ λ°νλ©λλ€.
>>> page.find_all(['div', 'span'], {'class': 'quote'})
[<data='<div class="quote" itemscope itemtype="h...' parent='<div class="col-md-8"> <div class="quote...'>,
<data='<div class="quote" itemscope itemtype="h...' parent='<div class="col-md-8"> <div class="quote...'>,
...]
μ¬λ¬ 쑰건μ μμ΄ μ¬μ©ν μλ μμ΅λλ€.
>>> page.find_all({'itemtype':"http://schema.org/CreativeWork"}, 'div').css('.author::text').getall()
['Albert Einstein',
'J.K. Rowling',
...]
μΆκ° ν: href μμ±κ°μ΄ 'Einstein'μΌλ‘ λλλ λͺ¨λ μμλ₯Ό μ°Ύμ΅λλ€.
>>> page.find_all({'href$': 'Einstein'})
[<data='<a href="/author/Albert-Einstein">(about...' parent='<span>by <small class="author" itemprop=...'>,
<data='<a href="/author/Albert-Einstein">(about...' parent='<span>by <small class="author" itemprop=...'>,
<data='<a href="/author/Albert-Einstein">(about...' parent='<span>by <small class="author" itemprop=...'>]
λ λ€λ₯Έ ν: href μμ±κ°μ '/author/'κ° ν¬ν¨λ λͺ¨λ μμλ₯Ό μ°Ύμ΅λλ€.
>>> page.find_all({'href*': '/author/'})
[<data='<a href="/author/Albert-Einstein">(about...' parent='<span>by <small class="author" itemprop=...'>,
<data='<a href="/author/J-K-Rowling">(about)</a...' parent='<span>by <small class="author" itemprop=...'>,
<data='<a href="/author/Albert-Einstein">(about...' parent='<span>by <small class="author" itemprop=...'>,
...]
μ΄λ° μμΌλ‘ μ¬μ©ν μ μμ΅λλ€.
μ νμ μμ±νκΈ° {#generating-selectors}
μ΄λ€ λ°©λ²μΌλ‘ μ°Ύμ μμλ CSS/XPath μ νμλ₯Ό μμ±ν μ μμ΅λλ€. μμ±ν μ νμλ μ΄κ³³μ΄λ λ€λ₯Έ λꡬμμ λ€μ μ¬μ©ν μ μμ΅λλ€.
url_elementμ μ§§μ CSS μ νμλ₯Ό λ§λλλ€. μ€μΌ μ μμΌλ©΄ μ 체 μ νμλ₯Ό λ§λλλ€.
>>> url_element = page.find({'href*': '/author/'})
>>> url_element.generate_css_selector
'body > div > div:nth-of-type(2) > div > div > span:nth-of-type(2) > a'
νμ΄μ§ μμλΆν° url_elementκΉμ§μ μ 체 CSS μ νμλ₯Ό λ§λλλ€.
>>> url_element.generate_full_css_selector
'body > div > div:nth-of-type(2) > div > div > span:nth-of-type(2) > a'
url_elementμ μ§§μ XPath μ νμλ₯Ό λ§λλλ€. μ€μΌ μ μμΌλ©΄ μ 체 μ νμλ₯Ό λ§λλλ€.
>>> url_element.generate_xpath_selector
'//body/div/div[2]/div/div/span[2]/a'
νμ΄μ§ μμλΆν° url_elementκΉμ§μ μ 체 XPath μ νμλ₯Ό λ§λλλ€.
>>> url_element.generate_full_xpath_selector
'//body/div/div[2]/div/div/span[2]/a'
!!! abstract "μ°Έκ³ "
μ§§μ μ νμλ₯Ό λ§λ€ λ Scraplingμ `id` μμ±μ΄ μλ μμμ²λΌ κ³ μ νκ² μλ³ν μ μλ μμλ₯Ό μ°Ύμ κ²½λ‘μ μμμ μΌλ‘ μ¬μ©ν©λλ€. μ΄ μμμλ κ·Έλ° μμκ° μμ΄ μ§§μ μ νμμ μ 체 μ νμκ° κ°μ΅λλ€.
μ νμμ μ κ·μ ν¨κ» μ¬μ©νκΈ° {#using-selectors-with-regular-expressions}
parsel/scrapyμ²λΌ reμ re_firstλ‘ μ κ·μμ μ¬μ©ν΄ λ°μ΄ν°λ₯Ό μΆμΆν μ μμ΅λλ€. νμ§λ§ Scraplingμμλ Selector/Selectors/TextHandler, TextHandlers λ± κ±°μ λͺ¨λ ν΄λμ€κ° μ΄ λ©μλλ₯Ό μ 곡ν©λλ€. λ°λΌμ ν
μ€νΈ λ
Έλλ₯Ό μ ννμ§ μμλ μμμ λ°λ‘ μ¬μ©ν μ μμ΅λλ€.
TextHandlerλ₯Ό μ€λͺ ν λ μμΈν λ€λ£Ήλλ€. κΈ°λ³Έ μ¬μ©λ²μ λ€μκ³Ό κ°μ΅λλ€.
>>> page.css('.price_color')[0].re_first(r'[\d\.]+')
'51.77'
>>> page.css('.price_color').re_first(r'[\d\.]+')
'51.77'
>>> page.css('.price_color').re(r'[\d\.]+')
['51.77',
'53.74',
'50.10',
'47.82',
'54.23',
...]
>>> page.css('.product_pod h3 a::attr(href)').re(r'catalogue/(.*)/index.html')
['a-light-in-the-attic_1000',
'tipping-the-velvet_999',
'soumission_998',
'sharp-objects_997',
...]
>>> filtering_function = lambda e: e.parent.tag == 'h3' and e.parent.parent.has_class('product_pod') # As above selector
>>> page.find('a', filtering_function).attrib['href'].re(r'catalogue/(.*)/index.html')
['a-light-in-the-attic_1000']
>>> page.find_by_text('Tipping the Velvet').attrib['href'].re(r'catalogue/(.*)/index.html')
['tipping-the-velvet_999']
μ΄μ λλ΅μ μΈ μ¬μ©λ²μ μ μ μμ κ²μ λλ€. λ€μ νμ΄μ§μμ TextHandlerμ ν¨κ» λ μμΈν μ€λͺ νκ² μ΅λλ€.