반응형
Book.py
#Book.py
class Book:
def __init__(self,title,author,price,content="",publisher="",link="",imglink=""):
self.title = title
self.author = author
self.price = price
self.content = content
self.publisher = publisher
self.link = link
self.imglink=imglink
NaverBookSearcher.py
#NaverBookSearcher.py
import urllib.request
import json
from Book import Book
class NaverBookSearcher:
@staticmethod
def MakeBook(jsbook):
title = jsbook["title"]
author = jsbook["author"]
price = jsbook["price"]
content = jsbook["description"]
publisher = jsbook["publisher"]
link = jsbook["link"]
imglink=jsbook["image"]
return Book(title,author,price,content,publisher,link,imglink)
def __init__(self):
self.client_id = "네이버 개발자센터에서 발급받은 자신의 Client ID"
self.client_secret = "네이버 개발자센터에서 발급받은 자신의 Client Secret"
self.url = "https://openapi.naver.com/v1/search/book.json"
self.query=""
self.query_str=""
def SetQuery(self,qstr):
self.query = "query="+urllib.parse.quote(qstr)
self.query_str = self.url+"?"+self.query
def Request(self,start,display):
start_str = "start="+str(start)
display_str = "display="+str(display)
fquery_str = self.query_str +"&"+start_str+"&"+display_str
request = urllib.request.Request(fquery_str)
request.add_header("X-Naver-Client-Id",self.client_id)
request.add_header("X-Naver-Client-Secret",self.client_secret)
try:
response = urllib.request.urlopen(request)
except:
return None
rescode = response.getcode()
if rescode != 200:
return None
content = response.read()
data = content.decode('utf-8')
jdata = json.loads(data)
total = jdata['total']
return jdata['items'],total
def RequestAll(self):
redatas=list()
start = 1
display=10
datas,total = self.Request(start,display)
redatas.extend(datas)
start = start+display
while (start<total)and(start<1000):
datas,total = self.Request(start,display)
redatas.extend(datas)
start = start+display
return redatas
BookSearcherWidget.py
#BookSearcherWidget.py
from NaverBookSearcher import NaverBookSearcher
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class BookSearcherWidget(QWidget):
def __init__(self):
super().__init__()
self.books = list()
self.setWindowTitle("도서 검색기(feat.네이버 개발자 센터)")
self.resize(1000,1000)
lb_query = QLabel("질의:",self)
lb_query.move(20,20)
self.te_query = QTextEdit("",self)
self.te_query.move(120,15)
self.te_query.resize(600,70)
self.btn_search = QPushButton("검색",self)
self.btn_search.move(740,15)
lb_blist = QLabel("도서 목록",self)
lb_blist.move(20,110)
self.lbox_book = QListWidget(self)
self.lbox_book.move(20,180)
self.lbox_book.resize(300,800)
self.btn_search.clicked.connect(self.SearchBook)
self.lb_title = QLabel("[제목]",self)
self.lb_title.move(340,200)
self.lb_title.resize(650,60)
self.lb_author = QLabel("[저자]",self)
self.lb_author.move(340,280)
self.lb_author.resize(650,60)
self.lb_price = QLabel("[가격]",self)
self.lb_price.move(340,360)
self.lb_price.resize(650,60)
self.te_content = QTextEdit("[내용]",self)
self.te_content.setReadOnly(True)
self.te_content.move(340,440)
self.te_content.resize(650,550)
self.lbox_book.currentItemChanged.connect(self.OnSelectChange)
def OnSelectChange(self):
index = self.lbox_book.currentRow()
try:
book = self.books[index]
except:
return
self.lb_title.setText(book.title)
self.lb_author.setText(book.author)
self.lb_price.setText(str(book.price))
self.te_content.setText(book.content)
def SearchBook(self):
self.books.clear()
self.lbox_book.clear()
q = self.te_query.toPlainText()
ns = NaverBookSearcher()
ns.SetQuery(q)
books = ns.RequestAll()
for bookjs in books:
book = NaverBookSearcher.MakeBook(bookjs)
self.books.append(book)
self.lbox_book.addItem(book.title)
Main.py
import sys
from PyQt5.QtWidgets import QApplication
from BookSearcherWidget import BookSearcherWidget
app = QApplication(sys.argv)
bsw = BookSearcherWidget()
bsw.show()
sys.exit(app.exec_())
반응형
'언어 자료구조 알고리즘 > 파이썬(Python)' 카테고리의 다른 글
[python] 뉴스 검색기V04 feat.네이버 개발자센터 (0) | 2020.11.11 |
---|---|
[python] 뉴스 검색기V03 feat. 네이버 개발자센터, 형태소 분석, MSSQL (0) | 2020.11.11 |
[ python] 뉴스 분석기 feat.네이버 개발자센터 , 형태소 분석 (0) | 2020.11.10 |
[python] 뉴스 검색 - 형태소 분석 (feat. 네이버 개발자센터) (0) | 2020.11.10 |
[python] 네이버 도서 검색 API 활용 - Json (0) | 2020.11.09 |
파이썬에서 별도의 선택문은 없어요. 대신 elif를 이용하세요. (0) | 2020.10.22 |
[python] 13. 리스트의 요소 개수 알아내기 및 정렬하기 (3) | 2016.05.31 |
[python] 12. 리스트에서 자료 삭제하기 (0) | 2016.05.31 |
[python] 11. 리스트에 자료를 추가하기 (0) | 2016.05.23 |
[python] 10. 파이썬을 잘 사용하기 위한 첫 걸음, 리스트를 소개합니다. (0) | 2016.05.20 |