웹 크롤링/Jupyter Notebook - 크롤링
주피터 - 웹 크롤링 - 네이버 책 검색기
빵으니
2020. 6. 15. 17:52
네이버 책 검색기¶
폰트 조정¶
In [1]:
%%html
<!-- 에디터 폰트를 조정합니다. -->
<style type='text/css'>
.CodeMirror{
font-size: 14px;
font-family: consolas;
</style>
library import¶
In [2]:
# library import
import requests
from bs4 import BeautifulSoup
주소 딕셔너리 형태{key:value}로 변환¶
In [3]:
# 주소 딕셔너리 형태{key:value}로 변환
url = 'https://book.naver.com/search/search.nhn'
params = {'sm':'sta_hty.book', 'sug':'', 'where':'nexearch', 'query':'bigdata'}
GET 요청¶
In [4]:
# GET 요청
response = requests.get(url, params=params)
status_code = response.status_code
print(status_code)
if status_code == 200:
text = response.text
BeautifulSoup 객체로 str->obj 변환¶
In [5]:
# str => BeautifulSoup
soup = BeautifulSoup(text)
# print(text)
책 전체 정보 가져오기 (크롬 개발자 도구)¶
In [6]:
# class 보단 유일무이한 값인 id로 가져오기
# 세 방법 중 하나 편한거 쓰기
book_all = soup.find(id='searchBiblioList')
# book_all = soup.find(attrs={'id':'searchBiblioList'})
# book_all = soup.select_one('#searchBiblioList')
# print(book_all)
In [7]:
# 책 한 권 정보
find_one = soup.find(class_='thumb_type thumb_type2')
#find_one = soup.find(attrs={'class':'thumb_type thumb_type2'})
############아니니닫find_one = soup.select_one('/thumb_type thumb_type2')
print(find_one)
In [8]:
# class 하위 정보 li 가져오기
# 책 각각, 전체
book_all_li_all = book_all.select('li')
# print(book_all_li_all)
# 책 한 권
book_all_li_one = book_all.select_one('li')
print(book_all_li_one)
In [9]:
# 책 bid 가져오기
bid_one = book_all_li_one.a['href'].split('=')[1]
# print(bid_one)
# 책 bid 여러개 가져오기
# for book_all_li_one in book_all_li_all:
# print(book_all_li_one.a['href'].split('=')[1])
# 책 bid 여러개 가져와서 list에 담기
bid_list = []
for book_all_li_one in book_all_li_all:
b_id = book_all_li_one.a['href'].split('=')[1]
bid_list.append(b_id)
print(bid_list)
In [10]:
# 책 제목 하나 가져오기
title_one = book_all_li_one.img['alt']
#print(title_one)
# 책 전체 가져와서 list에 담기
title_list = []
for book_all_li_one in book_all_li_all:
b_title = book_all_li_one.img['alt']
title_list.append(b_title)
print(title_list)
문제 풀기¶
In [11]:
# 저자 / 출판사 / 출판일 / 가격 정보가져오기
# 저자/출판사/출판일 정보 하나씩 가져오기
book_text = book_all_li_one.select_one('dd.txt_block').text
#print(book_text)
book_text = book_text.replace('\n','').replace('\r','')
book_text = book_text.replace('\t','').replace('\xa0','')
book_text_list = book_text.split('|')
book_author = book_text_list[0]
book_publisher = book_text_list[1]
book_pubdate = book_text_list[2]
print(book_author,book_publisher,book_pubdate)
# publisher = soup.find(class_='txt_block').a.text
# print(publisher)
# publisher_list = []
# for book_all_li_one in book_all_li_all:
# print(soup.find(class_='txt_block').a.text)
# publisher = soup.find(class_='txt_block').a.text
# publisher_list.append(publisher)
# print(publisher_list)
#pub_date =
#price = book_all_li_one.select(#'buy_btn_15516543')
#print(price)
In [12]:
# 저자 전체 정보 불러오기
author_list = list()
publisher_list = list()
pubdate_list = list()
for book_all_li_one in book_all_li_all:
book_text = book_all_li_one.select_one('dd.txt_block').text
book_text = book_text.replace('\n','').replace('\r','')
book_text = book_text.replace('\t','').replace('\xa0','')
book_text_list = book_text.split('|')
#print(book_text_list)
if(len(book_text_list)) == 4:
book_author = book_text_list[0] + book_text_list[1]
book_publisher = book_text_list[2]
book_pubdate = book_text_list[3]
else:
book_author = book_text_list[0]
book_publisher = book_text_list[1]
book_pubdate = book_text_list[2]
author_list.append(book_author)
publisher_list.append(book_publisher)
pubdate_list.append(book_pubdate)
print(author_list)
print(publisher_list)
print(pubdate_list)
In [13]:
# 책 한 권 가격 정보 가져오기
book_txt_desc = book_all_li_all[1].select_one('dd.txt_desc') # 전체 정보 중에 1index 값 정보 가져오기
#print(book_txt_desc)
price_org = book_txt_desc.select_one('strike').text
#print(price_org)
price_org = price_org.split('원')[0]
print(price_org)
price_disc = book_txt_desc.select_one('em.price').text
#print(price_disc)
price_disc = price_disc.split('원')[0]
print(price_disc)
In [14]:
# 책 정보 모두 가져오기
price_list = list()
for book_all_li_one in book_all_li_all:
book_txt_desc = book_all_li_one.select_one('dd.txt_desc')
price_org = book_txt_desc.select_one('strike')
price_disc = book_txt_desc.select_one('em.price')
# pirce가 None인 경우 해결하기
if price_org == None:
price_org = 0
else:
price_org = price_org.text.split('원')[0] # text만 뽑아서 작업
if price_disc == None:
price_disc = 0
else:
price_disc = price_disc.text.split('원')[0]
price_list.append((price_org, price_disc))
print(price_list)
In [15]:
# 최종 총 정보 dict로 뽑아 list에 담아주기
book_info_list = list()
for i in range(len(bid_list)):
book_info_dict = dict() # 동일한 키 값이 올 수 없으므로 반복할 떄마다 새로운 key값 담아줌
book_info_dict['bid'] = bid_list[i]
book_info_dict['title'] = title_list[i]
book_info_dict['author'] = author_list[i]
book_info_dict['publisher'] = publisher_list[i]
book_info_dict['pubdate'] = pubdate_list[i]
book_info_dict['price_org_disc'] = price_list[i]
book_info_list.append(book_info_dict)
print(book_info_list)
In [16]:
# 최종 정보에서 원하는 값만 뽑기
print(book_info_list[7]['author'])
print(book_info_list[10]['price_org_disc'])