์น ํฌ๋กค๋ง ๊ฐ๋จ์์ | Beautiful Soup ์ฌ์ฉ๋ฒ
2021. 4. 18. 03:11ใ๐ป ๊ฐ๋ฐ/Python
์น์์ ๋ฐ์ดํฐ๋ฅผ ํฌ๋กค๋งํ ๋, Python ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ Beautiful Soup์ ํตํด ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
1. ์ค์นํ๊ฒฝ
Python 3.6
2. BeautifulSoup ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
pip install beautifulsoup4
3. ๋ด์ค๊ธฐ์ฌ ํฌ๋กค๋ง
IT์กฐ์ ๋ด์ค ๋ฉ์ธํ์ด์ง๋ฅผ ๋ณด๋ฉด, ๊ธฐ์ฌ๋ค์ด ๋ชจ๋ ๋งํฌ๋ก ๊ฑธ๋ ค์๋ค. ๊ธฐ์ฌ์ URL์ ์์งํ๊ธฐ ์ํด aํ๊ทธ์์ href๋ฅผ ๊ฐ์ ธ์ค๋ฉด ๋๋ค.
4. ํ์ด์ฌ ์ฝ๋
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://it.chosun.com/")
bsObject = BeautifulSoup(html, "html.parser")
for link in bsObject.find_all('a'):
print(link.text.strip(), link.get('href'))
- urlopen์ ์ฌ์ฉํ์ฌ ํด๋น ํ์ด์ง์ HTML์์ค๋ฅผ ๊ฐ์ ธ์จ๋ค
- BeautifulSoup์ ์ฌ์ฉํ์ฌ ํ์ฑํ๊ธฐ
- find_all(): ํด๋น ์กฐ๊ฑด์ ๋ง๋ ๋ชจ๋ ํ๊ทธ๋ค์ ๊ฐ์ ธ์, ๋ฆฌ์คํธ๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค
5. ์คํ๊ฒฐ๊ณผ
'๐ป ๊ฐ๋ฐ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Python ๋์ ๋๋ฆฌ๋ฅผ ํํ ๋ฆฌ์คํธ๋ก ๋ณํ (0) | 2021.09.07 |
---|---|
Python ๋์ ๋๋ฆฌ ์๋ฃํ (0) | 2021.08.18 |
Python ๊ณต๋ฐฑ์์ด ์ ๋ ฅ๋ฐ์ list๋ง๋ค๊ธฐ: strip(), rstrip(), lstrip() ์ฌ์ฉ๋ฒ (0) | 2021.04.09 |