- html 태그 : 웹 페이지 표현
- head 태그 : 눈에 보이지 않지만 문서에 필요한 헤더 정보 보관
- body 태그 : 눈에 보이는 정보 보관
- Beautiful Soup 테스트를 위해 html 작성
Beautiful Soup
: 태그로 이루어진 문서를 해석하는 기능을 가진 모듈html.parser
: Beautiful Soup의 html을 읽는 엔진 중 하나(lxml도 많이 사용)prettify()
: html을 태그 기준으로 들여쓰기를 통해 정리해주는 기능 page = open("../data/03. Zerobase.html", "r").read()
soup = BeautifulSoup(page, "html.parser")
print(soup.prettify())
# head 태그 확인
soup.head
# p 태그 확인
# 처음 발견한 p 태그만 출력
# find()
soup.p
soup.find("p")
# find_all() : 여러개 확인 시 사용
soup.find("p", class_="inner-text second-item")
# 원하는 내용만 출력
soup.find("p", {"class":"outer-text first-item"}).text.strip()
# 다중 조건
soup.find("p", {"class": "inner-text first-item", "id":"first"})
# find_all() : 여러개의 태그 반환
# list형태로 반환
# soup.find("p")는 하나만 출력
soup.find_all("p")
# 특정태그 확인
soup.find_all(class_="outer-text")
soup.find_all(id="pw-link")[0].text
'PinkWink'
soup.find_all("p", class_="inner-text second-item")
# p 태그 리스트에서 텍스트 속성만 출력
print(soup.find_all("p")[0].text)
print(soup.find_all("p")[1].string)
print(soup.find_all("p")[1].get_text())
text 키워드
: 태그에 포함된 텍스트를 반환string 키워드
: 태그에 포함된 텍스트를 반환(단, 단일 태그인 경우에만 동작)get_text()
: 태그에 포함된 텍스트를 반환'구분자'
: 태그 사이의 구분자 설정strip 옵션
: 데이터의 양끝 공백 제거 설정(True / False)stripped_strings 키워드
: for을 사용하여 리스트, 튜플 등의 형태로 반환할 수 있다.# p 태그 리스트에서 텍스트 속성만 출력
for each_tag in soup.find_all("p"):
print("=" * 50)
print(each_tag.text)
find("a")
로 링크 태그를 찾는다.태그[속성]
, 태그.get(속성)
: 해당 태그의 속성 값 반환# a 태그에서 href 속성값에 있는 값 추출
links = soup.find_all("a")
links
for each in links:
href = each.get("href")
text = each.get_text()
print(text + "=>" + href)
# import
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = "https://finance.naver.com/marketindex/"
page = urlopen(url)
soup = BeautifulSoup(page, "html.parser")
print(soup.prettify())
.status 키워드
: http 상태 코드로 200일 경우 정상적으로 정보를 받았다는 의미# 환율 정보
soup.find_all("span","value"), len(soup.find_all("span","value"))
# soup.find_all("span", class_="value"), len(soup.find_all("span","value")) 와 동일
# soup.find_all("span", {"class":"value"}), len(soup.find_all("span","value")) 와 동일 (딕셔너리 타입)
import requests
from bs4 import BeautifulSoup
from urllib import response
url = "https://finance.naver.com/marketindex/"
response = requests.get(url)
soup = BeautifulSoup(response.text,"html.parser")
print(soup.prettify())
exchangeList = soup.select("#exchangeList > li") #id태그 : #, class태그: '.'
len(exchangeList), exchangeList
#title
title = exchangeList[0].select_one(".h_lst").text
#exchange
exchange = exchangeList[0].select_one(".value").text
#change
change = exchangeList[0].select_one(".change").text
#updown
updown = exchangeList[0].select_one("div.head_info > .blind").text
#link
title, exchange, change, updown
--> ('미국 USD', '1,432.50', ' 1.50', '하락')
baseUrl = "https://finance.naver.com"
baseUrl + exchangeList[0].select_one("a").get("href")
--> 'https://finance.naver.com/marketindex/exchangeDetail.naver?marketindexCd=FX_USDKRW'
# 환전 고시 환율 추출
# 4개 데이터 수집
import pandas as pd
exchange_datas = []
baseUrl = "https://finance.naver.com"
for item in exchangeList:
data = {
"title": item.select_one(".h_lst").text,
"exchange":item.select_one(".value").text,
"change":item.select_one(".change").text,
"updown": item.select_one("div.head_info > .blind").text,
"link":baseUrl + item.select_one("a").get("href")
}
exchange_datas.append(data)
df = pd.DataFrame(exchange_datas)
df
df.to_excel("./naverfinance.xlsx", encoding="utf-8")
from urllib.request import urlopen, Request
import urllib
import pandas as pd
from bs4 import BeautifulSoup
html = "https://ko.wikipedia.org/wiki/{search_words}"
req = Request(html.format(search_words=urllib.parse.quote("여명의_눈동자"))) #글자를 URL로 인코딩
response = urlopen(req)
# response.status --> 200출력 확인
response
soup = BeautifulSoup(response, "html.parser")
print(soup.prettify)
# 주요 인물 부분의 태그 확인
# ul임을 확인
n = 0
# 주인공 나오는 줄 확인
for each in soup.find_all("ul"):
print("=>"+str(n)+"==============")
print(each.get_text())
n += 1
31 번째 ul이 원하는 정보를 가지고 있음을 확인(인덱스는 0부터 시작)
# replace 함수를 이용해 필요없는 부분 제거
soup.find_all("ul")[31].text.strip().replace("\xa0", "").replace("\n","")
--> '채시라: 윤여옥 역 (아역: 김민정)박상원: 장하림(하리모토 나츠오) 역 (아역: 김태진)최재성: 최대치(사카이) 역 (아역: 장덕수)'