112 lines
4.4 KiB
Python
112 lines
4.4 KiB
Python
import sys
|
|
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QTextEdit, QMessageBox, QGridLayout, QSizePolicy
|
|
from PyQt5.QtCore import Qt
|
|
from PyQt5.QtGui import QPixmap
|
|
from web_scraper_with_re import WebScraper
|
|
|
|
class MainApp(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.initUI()
|
|
self.scraper = WebScraper()
|
|
|
|
def initUI(self):
|
|
self.searchLayout = QHBoxLayout()
|
|
self.searchLabel = QLabel('검색어 입력:', self)
|
|
self.searchLineEdit = QLineEdit(self)
|
|
self.searchButton = QPushButton('검색 실행', self)
|
|
self.searchButton.clicked.connect(self.start_search)
|
|
self.searchLineEdit.returnPressed.connect(self.start_search)
|
|
self.searchButton.setEnabled(False) # 버튼을 초기에 비활성화합니다.
|
|
|
|
self.searchLayout.addWidget(self.searchLabel)
|
|
self.searchLayout.addWidget(self.searchLineEdit)
|
|
self.searchLayout.addWidget(self.searchButton)
|
|
|
|
self.logLayout = QVBoxLayout()
|
|
self.logTextEdit = QTextEdit(self)
|
|
self.logTextEdit.setReadOnly(True)
|
|
self.logLayout.addWidget(self.logTextEdit)
|
|
|
|
self.mainLayout = QVBoxLayout()
|
|
self.mainLayout.addLayout(self.searchLayout)
|
|
self.mainLayout.addLayout(self.logLayout)
|
|
|
|
self.setLayout(self.mainLayout)
|
|
self.setWindowTitle('Web Scraper')
|
|
self.setGeometry(300, 300, 300, 400)
|
|
|
|
self.prepare_search() # 페이지 로딩과 관련된 초기 설정
|
|
|
|
def prepare_search(self):
|
|
# self.scraper.setup_browser() # 브라우저 설정
|
|
self.searchButton.setEnabled(True) # 페이지 로드 후 버튼 활성화
|
|
|
|
def start_search(self):
|
|
term = self.searchLineEdit.text()
|
|
if not term:
|
|
QMessageBox.warning(self, "경고", "검색어를 입력해주세요.")
|
|
return
|
|
|
|
self.logTextEdit.append("검색을 시작합니다...")
|
|
results = self.scraper.search_for_term(term)
|
|
if results:
|
|
self.logTextEdit.append("검색 완료. 결과를 처리합니다...")
|
|
self.show_results(results)
|
|
else:
|
|
self.logTextEdit.append("검색 결과가 없거나 오류가 발생했습니다.")
|
|
QMessageBox.information(self, "검색 결과 없음", "검색 결과가 없으므로 지재권에 안심하시면 됩니다.", QMessageBox.Ok)
|
|
|
|
def show_results(self, results):
|
|
# 이전 결과 위젯이 있으면 닫기
|
|
if self.results_widget is not None:
|
|
self.results_widget.close()
|
|
|
|
self.results_widget = QWidget()
|
|
layout = QVBoxLayout()
|
|
self.results_widget.setLayout(layout)
|
|
total_count = results['total_count']
|
|
set_count = min(total_count, 10)
|
|
grid_layout = QGridLayout()
|
|
layout.addLayout(grid_layout)
|
|
grid_index = 0
|
|
grid_columns = 5
|
|
|
|
for i in range(1, set_count + 1):
|
|
result_key = f"result_{i}"
|
|
if result_key in results:
|
|
result = results[result_key]
|
|
item_layout = QVBoxLayout()
|
|
item_widget = QWidget()
|
|
item_widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.MinimumExpanding)
|
|
|
|
image_label = QLabel()
|
|
image_label.setFixedSize(150, 150)
|
|
image_data = self.scraper.fetch_image_data(result['IDimageURL'])
|
|
if image_data:
|
|
pixmap = QPixmap()
|
|
pixmap.loadFromData(image_data)
|
|
scaled_pixmap = pixmap.scaled(image_label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
|
|
image_label.setPixmap(scaled_pixmap)
|
|
image_label.setAlignment(Qt.AlignCenter)
|
|
image_label.setScaledContents(True)
|
|
item_layout.addWidget(image_label)
|
|
|
|
info_text = f"{result['title']} - {result['status']}"
|
|
info_label = QLabel(info_text)
|
|
item_layout.addWidget(info_label)
|
|
|
|
grid_layout.addLayout(item_layout, grid_index // grid_columns, grid_index % grid_columns)
|
|
grid_index += 1
|
|
|
|
self.results_widget.setGeometry(300, 300, 800, 600)
|
|
self.results_widget.setWindowTitle('Search Results')
|
|
self.results_widget.show()
|
|
|
|
if __name__ == '__main__':
|
|
app = QApplication(sys.argv)
|
|
ex = MainApp()
|
|
ex.show()
|
|
sys.exit(app.exec_())
|
|
|