์คํฐ๋ ๋ ธํธ (BeautifulSoup Web data 1)
๐ BeautifulSoup - Web data 1
๐ฉ ๋ชฉํ ๋ฐ์ดํฐ (ํ์จ)
- ๋๋ผ ์ด๋ฆ
- ํ์ฌ ํ์จ
- ๋ณ๋ ํญ
- ์์น or ํ๋ฝ ๊ฐ
๐ฉ urllib, BeautifulSoup ํ์ฉ
- ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ค๋ url๊ณผ urlopen, BeautifulSoup ๋ฑ์ ํจ์๋ฅผ ์ฌ์ฉ
- html.parser, prettify ํจ์๋ก ์ ๋ฆฌ๋ web data๋ฅผ ๋ณผ ์ ์๋ค.
- ๊ฐ์ ธ์ค๋ ค๋ ์งํ
https://finance.naver.com/marketindex/
ํ์ ๊ณ ์ ํ์จ 2023.08.11 18:39 ํ๋์ํ ๊ธฐ์ค ๊ณ ์ํ์ฐจ 499ํ
finance.naver.com
- status ํจ์, 200์ด ์ถ๋ ฅ๋๋ฉด ์น ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํ ์ ์๋ค
๐ HTTP ์ํ ์ฝ๋ ํ์ธํ๊ธฐ
https://ko.wikipedia.org/wiki/HTTP_%EC%83%81%ED%83%9C_%EC%BD%94%EB%93%9C
HTTP ์ํ ์ฝ๋ - ์ํค๋ฐฑ๊ณผ, ์ฐ๋ฆฌ ๋ชจ๋์ ๋ฐฑ๊ณผ์ฌ์
์ํค๋ฐฑ๊ณผ, ์ฐ๋ฆฌ ๋ชจ๋์ ๋ฐฑ๊ณผ์ฌ์ . ์๋๋ HTTP(ํ์ดํผํ ์คํธ ์ ์ก ํ๋กํ ์ฝ) ์๋ต ์ํ ์ฝ๋์ ๋ชฉ๋ก์ด๋ค. IANA๊ฐ ํ์ฌ ๊ณต์ HTTP ์ํ ์ฝ๋ ๋ ์ง์คํธ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ๊ณ ์๋ค. ๋ชจ๋ HTTP ์๋ต ์ฝ๋๋ 5๊ฐ์
ko.wikipedia.org
๐ฉ ํน์ ๊ฐ ์ฐพ๊ธฐ
- find_all("tag_name", "class_name")
- find_all("tag_name", class_= "class_name")
- find_all("tag_name", {"class" : "class_name"})
๐ฉ requests, BeautifulSoup ํ์ฉ
๐ฉ ํน์ ๊ฐ ์ฐพ๊ธฐ
- select ํจ์๋ฅผ ์ด์ฉํด ํน์ ๊ฐ์ ์ฐพ๋๋ค
- '#' ๊ณผ '.' ์ ์ ๊ตฌ๋ถํด์ผํ๋ค.
- '#' = id, '.'= class
- select("#id_name), select(".class_name)
- select_one("#id_name), select_one(".class_name)
- ๊บฝ์ ๋ ๋ฐ์ดํฐ์ ํ์ ๊ฐ์ ์ฐพ๋๋ค
- 0๋ฒ์งธ ์ธ๋ฑ์ค ๊ฐ ๊ตฌํ๊ธฐ
- '.' (class) ๊ฐ์ ์ฌ์ฉํด ๋ฐ์ดํฐ ๊ฐ๋ค์ ๋ฐํํ๋ค.
โป updown ๊ฐ์์ > ์ ์๋ฏธ? โป
- updown ๊ฐ์ class_name = blind
- ํ์ง๋ง ์ฐพ์ ๋, ์ ์ผ ์ฒ์ ๊ฐ์ ๋ถ๋ฌ์ค๊ธฐ ๋๋ฌธ์ select_one(".blind")์ ์ ๋ ฅํ๋ฉด ๋ฏธ๊ตญ USD๊ฐ ์ถ๋ ฅ๋๋ค.
- ๊ทธ๋์ div ํ๊ทธ์ ์๋ ํด๋์ค head_info ๊ฐ ๋ฐ ๊ธฐ์ค์ผ๋ก ์ฐพ์ผ๋ผ๋ ๊ธฐ์ค์ ์ธ์์ค.
- span ํ๊ทธ์ ์๋ ํด๋์ค txt_krw ๊ฐ ๋ฐ ๊ธฐ์ค blind๋ ์์ง๋ง, ์ ์ด์ head_info ๊ธฐ์ค์ผ๋ก ์ ํด์ ธ์๊ธฐ ๋๋ฌธ์
blind ํด๋์ค ๊ฐ์ธ ์์น์ด ์ถ๋ ฅ๋๋ค.
- span ํ๊ทธ์ ์๋ ํด๋์ค txt_krw ๊ฐ ๋ฐ ๊ธฐ์ค blind๋ ์์ง๋ง, ์ ์ด์ head_info ๊ธฐ์ค์ผ๋ก ์ ํด์ ธ์๊ธฐ ๋๋ฌธ์
๐ฉ ์ฐพ์ ๊ฐ์ DataFrameํ ์ํค๊ธฐ
# 4๊ฐ ๋ฐ์ดํฐ ์์ง
exchange_datas = [] #DataFrameํ ์ํค๊ธฐ ์ํด์ ๋น ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด ์ค
baseUrl = "https://finance.naver.com" #url ๋ฃ๊ธฐ ์ํด baseUrl ์ง์
for item in exchangeList: #exchangeList์์ item์ ํ๋์ฉ ๋ฝ์๋ธ๋ค
data = {
"title" : item.select_one(".h_lst").text, # ์ด๋ฆ ๊ฐ
"exchange" : item.select_one(".value").text, # ์ก์ ๊ฐ
"change" : item.select_one(".change").text, # ๋ณ๋๋ ์ก์ ๊ฐ
"updown" : item.select_one(".head_info > .blind").text, # ์์น ๋ฐ ํ๋ฝ ๊ฐ
"link" : baseUrl + item.select_one("a").get("href") # url ๊ฐ
}
print(data)
exchange_datas.append(data)
exchange_datas
df = pd.DataFrame(exchange_datas)
df.to_excel("./naverfinance.xlsx", encoding="utf-8") # excel ํ์ผ๋ก ์ ์ฅ
>>>>
{'title': '๋ฏธ๊ตญ USD', 'exchange': '1,327.50', 'change': '12.50', 'updown': '์์น', 'link': 'https://finance.naver.com/marketindex/exchangeDetail.naver?marketindexCd=FX_USDKRW'}
{'title': '์ผ๋ณธ JPY(100์)', 'exchange': '918.08', 'change': '7.01', 'updown': '์์น', 'link': 'https://finance.naver.com/marketindex/exchangeDetail.naver?marketindexCd=FX_JPYKRW'}
{'title': '์ ๋ฝ์ฐํฉ EUR', 'exchange': '1,457.33', 'change': '8.07', 'updown': '์์น', 'link': 'https://finance.naver.com/marketindex/exchangeDetail.naver?marketindexCd=FX_EURKRW'}
{'title': '์ค๊ตญ CNY', 'exchange': '182.95', 'change': '1.11', 'updown': '์์น', 'link': 'https://finance.naver.com/marketindex/exchangeDetail.naver?marketindexCd=FX_CNYKRW'}