๐ท๏ธ 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: "BeautifulSoup์์ Scrapling์ผ๋ก ์ฎ๊ธฐ๊ธฐ" description: "BeautifulSoup์ ์์ ์ ํ, ํ ์คํธ, ์์ฑ, ํ์ ์์ ์ Scrapling์ผ๋ก ๋ฐ๊ฟ ๋ณด์ธ์. ์ค์ ์์ ๋ก API ์ฐจ์ด๋ฅผ ํ์ธํฉ๋๋ค."
BeautifulSoup์์ Scrapling์ผ๋ก ์ฎ๊ธฐ๊ธฐ {#migrating-from-beautifulsoup-to-scrapling}
BeautifulSoup์ ์ต์ํ๋ค๋ฉด Scrapling๋ ์ฝ๊ฒ ์์ํ ์ ์์ต๋๋ค. Scrapling์ ๋ ๋น ๋ฅด๊ฒ ์๋ํ๋ฉฐ, BeautifulSoup์ ํ์ฑ ๊ธฐ๋ฅ์ ๋ํด ์๋ก์ด ํ์ฑ ๊ธฐ๋ฅ๋ ์ ๊ณตํฉ๋๋ค. ํ๋์ ์ธ ์นํ์ด์ง๋ฅผ ๊ฐ์ ธ์ค๊ณ ์ฒ๋ฆฌํ๋ ๊ธฐ๋ฅ๋ ์์ต๋๋ค. ์ด ์๋ด๋ ๊ธฐ์กด BeautifulSoup ์ฝ๋๋ฅผ Scrapling์ผ๋ก ๋น ๋ฅด๊ฒ ๋ฐ๊พธ๋ ๋ฐ ๋์์ ์ค๋๋ค.
์๋ ํ์๋ ์น ์คํฌ๋ํ์์ ์์ฃผ ์ฌ์ฉํ๋ ์์ ์ ์ ๋ฆฌํ์ต๋๋ค. ๊ฐ ํ์์ ๊ฐ์ ์์ ์ BeautifulSoup๊ณผ Scrapling์ผ๋ก ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋น๊ตํ ์ ์์ต๋๋ค.
BeautifulSoup์ ์ผ๋ถ ๋จ์ถ ํํ์ Scrapling์ ์์ต๋๋ค. ์ด๊ฒ์ Scrapling์ด ๋ ๋น ๋ฅธ ์ด์ ์ค ํ๋์ ๋๋ค. ์ด๋ฏธ ์งง์ ํ ์ค๋ก ๊ฐ์ ๊ธฐ๋ฅ์ ์ธ ์ ์๋ค๋ฉด, ๊ทธ ์ค์ ๋ ์ค์ด๊ธฐ ์ํด ์ฑ๋ฅ์ ํฌ์ํ ํ์๋ ์๋ค๊ณ ์๊ฐํฉ๋๋ค :)
| ์์ | BeautifulSoup ์ฝ๋ | Scrapling ์ฝ๋ |
|---|---|---|
| ํ์ ๊ฐ์ ธ์ค๊ธฐ | from bs4 import BeautifulSoup |
from scrapling.parser import Selector |
| ๋ฌธ์์ด์ HTML ํ์ฑ | soup = BeautifulSoup(html, 'html.parser') |
page = Selector(html) |
| ์์ ํ๋ ์ฐพ๊ธฐ | element = soup.find('div', class_='example') |
element = page.find('div', class_='example') |
| ์ฌ๋ฌ ์์ ์ฐพ๊ธฐ | elements = soup.find_all('div', class_='example') |
elements = page.find_all('div', class_='example') |
| ์์ ํ๋ ์ฐพ๊ธฐ(์์ 2) | element = soup.find('div', attrs={"class": "example"}) |
element = page.find('div', {"class": "example"}) |
| ์์ ํ๋ ์ฐพ๊ธฐ(์์ 3) | element = soup.find(re.compile("^b")) |
element = page.find(lambda e: re.search(r"^b", e.tag) is not None) |
| ์์ ํ๋ ์ฐพ๊ธฐ(์์ 4) | element = soup.find(lambda e: len(list(e.children)) > 0) |
element = page.find(lambda e: len(e.children) > 0) |
| ์์ ํ๋ ์ฐพ๊ธฐ(์์ 5) | element = soup.find(["a", "b"]) |
element = page.find(["a", "b"]) |
| ํ ์คํธ ๋ด์ฉ์ผ๋ก ์์ ์ฐพ๊ธฐ | element = soup.find(text="some text") |
element = page.find_by_text("some text", partial=False) |
| CSS ์ ํ์๋ก ์ฒซ ๋ฒ์งธ ์ผ์น ์์ ์ฐพ๊ธฐ | elements = soup.select_one('div.example') |
elements = page.css('div.example').first |
| CSS ์ ํ์๋ก ๋ชจ๋ ์ผ์น ์์ ์ฐพ๊ธฐ | elements = soup.select('div.example') |
elements = page.css('div.example') |
| ํ์ด์ง๋ ์์์ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ฆฌํ ์์ค ๊ฐ์ ธ์ค๊ธฐ | prettified = soup.prettify() |
prettified = page.prettify() |
| ํ์ด์ง๋ ์์์ ์ ๋ฆฌํ์ง ์์ ์์ค ๊ฐ์ ธ์ค๊ธฐ | source = str(soup) |
source = page.html_content |
| ์์์ ํ๊ทธ ์ด๋ฆ ๊ฐ์ ธ์ค๊ธฐ | name = element.name |
name = element.tag |
| ์์์ ํ ์คํธ ์ถ์ถ | string = element.string |
string = element.text |
| ๋ฌธ์ ์ ์ฒด๋ ํ๊ทธ ์๋์ ๋ชจ๋ ํ ์คํธ ์ถ์ถ | text = soup.get_text(strip=True) |
text = page.get_all_text(strip=True) |
| ์์ฑ ๋์ ๋๋ฆฌ์ ์ ๊ทผ | attrs = element.attrs |
attrs = element.attrib |
| ์์ฑ ์ถ์ถ | attr = element['href'] |
attr = element['href'] |
| ๋ถ๋ชจ๋ก ์ด๋ | parent = element.parent |
parent = element.parent |
| ์์์ ๋ชจ๋ ์กฐ์ ๊ฐ์ ธ์ค๊ธฐ | parents = list(element.parents) |
parents = list(element.iterancestors()) |
| ์กฐ์ ์ค์์ ์์ ์ฐพ๊ธฐ | target_parent = element.find_parent("a") |
target_parent = element.find_ancestor(lambda p: p.tag == 'a') |
| ๋ชจ๋ ํ์ ์์ ๊ฐ์ ธ์ค๊ธฐ | ์ง์ ์ ํจ | siblings = element.siblings |
| ๋ค์ ํ์ ์์ ๊ฐ์ ธ์ค๊ธฐ | next_element = element.next_sibling |
next_element = element.next |
| ํ์ ์ค์์ ์์ ํ๋ ์ฐพ๊ธฐ | target_sibling = element.find_next_sibling("a")target_sibling = element.find_previous_sibling("a") |
target_sibling = element.siblings.search(lambda s: s.tag == 'a') |
| ํ์ ์ค์์ ์ฌ๋ฌ ์์ ์ฐพ๊ธฐ | target_sibling = element.find_next_siblings("a")target_sibling = element.find_previous_siblings("a") |
target_sibling = element.siblings.filter(lambda s: s.tag == 'a') |
| ๋ค์ ๋์ค๋ ์์ ์ค ํ๋ ์ฐพ๊ธฐ | target_parent = element.find_next("a") |
target_parent = element.below_elements.search(lambda p: p.tag == 'a') |
| ๋ค์ ๋์ค๋ ์์ ์ค ์ฌ๋ฌ ๊ฐ ์ฐพ๊ธฐ | target_parent = element.find_all_next("a") |
target_parent = element.below_elements.filter(lambda p: p.tag == 'a') |
| ์กฐ์ ์ค์์ ์์ ํ๋ ์ฐพ๊ธฐ | target_parent = element.find_previous("a") ยน |
target_parent = element.path.search(lambda p: p.tag == 'a') |
| ์กฐ์ ์ค์์ ์ฌ๋ฌ ์์ ์ฐพ๊ธฐ | target_parent = element.find_all_previous("a") ยน |
target_parent = element.path.filter(lambda p: p.tag == 'a') |
| ์ด์ ํ์ ์์ ๊ฐ์ ธ์ค๊ธฐ | prev_element = element.previous_sibling |
prev_element = element.previous |
| ์์์ผ๋ก ์ด๋ | children = list(element.children) |
children = element.children |
| ๋ชจ๋ ์์ ์์ ๊ฐ์ ธ์ค๊ธฐ | children = list(element.descendants) |
children = element.below_elements |
| story ํด๋์ค๊ฐ ์๋ ๋ฌธ๋จ ์์ ๋งํฌ ์ ํ | group = soup.select('p.story a') |
group = page.css('p.story a') |
์ปดํ์ผํ ์ ๊ท์์ Scrapling์ find()๋ find_all()์ ์ง์ ์ ๋ฌํ๋ฉด ํ๊ทธ ์ด๋ฆ์ด ์๋๋ผ ์์์ ํ
์คํธ๋ฅผ ๊ฒ์ฌํฉ๋๋ค. ํ๊ทธ ์ด๋ฆ์๋ ์ ํ์ lambda๋ฅผ ์ฌ์ฉํ๊ณ , ํ
์คํธ๋ฅผ ์ฐพ์ ๋๋ page.find_by_regex(r"^b")๋ฅผ ์ง์ ์ฌ์ฉํ์ธ์.
ยน ์ฐธ๊ณ : BS4์ find_previous/find_all_previous๋ ๋ฌธ์ ์์์ ์์ ์๋ ๋ชจ๋ ์์๋ฅผ ๊ฒ์ํฉ๋๋ค. ๋ฐ๋ฉด Scrapling์ path๋ ์กฐ์, ์ฆ ๋ถ๋ชจ๋ฅผ ๋ฐ๋ผ ์ฌ๋ผ๊ฐ ์์๋ง ๋ฐํํฉ๋๋ค. ์์ ํ ๊ฐ์ ๊ธฐ๋ฅ์ ์๋์ง๋ง, ์กฐ์ ๊ฒ์์ผ๋ก ๊ฐ์ฅ ํํ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
๊ธฐ์ตํ ์ : BeautifulSoup์ ํ์ฑ ํ ํ์ด์ง๋ฅผ ์์ ํ๊ณ ์กฐ์ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. Scrapling์ ํ์ด์ง์์ ๋ฐ์ดํฐ๋ฅผ ๋น ๋ฅด๊ฒ ์์งํ๋ ๋ฐ ๋ ์ง์คํฉ๋๋ค. ์ถ์ถํ ๋ฐ์ดํฐ๋ ์ดํ์ ์ํ๋ ๋๋ก ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ๋ ๋๊ตฌ ๋ชจ๋ ์น ์คํฌ๋ํ์ ์ฌ์ฉํ ์ ์์ง๋ง Scrapling์ ๊ทธ ์ฉ๋์ ํนํ๋์ด ์์ต๋๋ค :)
์ ์ฒด ์์ {#putting-it-all-together}
BeautifulSoup๊ณผ Scrapling์ผ๋ก ์นํ์ด์ง์ ๋ชจ๋ ๋งํฌ๋ฅผ ์ถ์ถํ๋ ๊ฐ๋จํ ์์ ์ ๋๋ค.
BeautifulSoup ์ฌ์ฉ:
import requests
from bs4 import BeautifulSoup
url = 'https://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
links = soup.find_all('a')
for link in links:
print(link['href'])
Scrapling ์ฌ์ฉ:
from scrapling import Fetcher
url = 'https://example.com'
page = Fetcher.get(url)
links = page.css('a::attr(href)').getall()
for link in links:
print(link)
Scrapling์ ํ์ด์ง ๊ฐ์ ธ์ค๊ธฐ์ ํ์ฑ์ ํ ๋จ๊ณ๋ก ํฉ์ณ ์ฝ๋๋ฅผ ๋ ๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ผ๋ก ๋ง๋ญ๋๋ค.
!!! abstract "์ถ๊ฐ ์ฐธ๊ณ ์ฌํญ:"
- **ํ์ ์ฐจ์ด**: BeautifulSoup์์๋ ํ์ฑ ์์ง์ ์ ํํ ์ ์์ผ๋ฉฐ `lxml`๋ ๊ทธ์ค ํ๋์
๋๋ค. Scrapling์ ์์ง์ ๋ฐ๊พธ์ง ์๊ณ ์ฑ๋ฅ์ ์ํด ๊ธฐ๋ณธ์ผ๋ก `lxml`์ ์ฌ์ฉํฉ๋๋ค.
- **์์ ํ์
**: BeautifulSoup์ ์์๋ `Tag`, Scrapling์ ์์๋ `Selector` ๊ฐ์ฒด์
๋๋ค. ํ์๊ณผ ๋ฐ์ดํฐ ์ถ์ถ์๋ ๋น์ทํ ๋ฉ์๋์ ์์ฑ์ ์ ๊ณตํฉ๋๋ค.
- **์ค๋ฅ ์ฒ๋ฆฌ**: ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ชจ๋ ์์๋ฅผ ์ฐพ์ง ๋ชปํ๋ฉด `None`์ ๋ฐํํฉ๋๋ค. ์๋ฅผ ๋ค์ด `soup.find()`๋ `page.find()`๊ฐ ๊ทธ๋ ์ต๋๋ค. Scrapling์ `page.css()`๋ ์ผ์นํ๋ ์์๊ฐ ์์ผ๋ฉด ๋น `Selectors` ๋ฆฌ์คํธ๋ฅผ ๋ฐํํฉ๋๋ค. `page.css('.foo').first`๋ก ์ฒซ ๋ฒ์งธ ์ผ์น ์์ ๋๋ `None`์ ์์ ํ๊ฒ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ์ค๋ฅ๋ฅผ ํผํ๋ ค๋ฉด ์์ฑ์ ์ ๊ทผํ๊ธฐ ์ ์ `None`์ด๋ ๋น ๊ฒฐ๊ณผ์ธ์ง ํ์ธํ์ธ์.
- **ํ
์คํธ ์ถ์ถ**: Scrapling์ `TextHandler`๋ก ํ
์คํธ ์ฒ๋ฆฌ ๋ฉ์๋๋ฅผ ๋ ์ ๊ณตํฉ๋๋ค. ์๋ฅผ ๋ค์ด `clean()`์ ๋ถํ์ํ ๊ณต๋ฐฑ ๋ฌธ์, ์ฐ์๋ ๊ณต๋ฐฑ, ์์น ์๋ ๋ฌธ์๋ฅผ ์ ๊ฑฐํ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค. ์ ์ฒด ๋ชฉ๋ก์ ๋ฌธ์๋ฅผ ํ์ธํ์ธ์.
๋ฌธ์์๋ Scrapling์ ๊ธฐ๋ฅ๊ณผ ๊ฐ ๋ฉ์๋์ ์ ๋ฌํ ์ ์๋ ์ธ์๊ฐ ์์ธํ ๋์ ์์ต๋๋ค.
์ด ์๋ด๊ฐ BeautifulSoup์์ Scrapling์ผ๋ก ์ฎ๊ธฐ๋ ๋ฐ ๋์์ด ๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค. ์ฆ๊ฒ๊ฒ ์คํฌ๋ํํ์ธ์!