This commit is contained in:
parent
e11d2d4125
commit
b25e323abc
26
gui2.py
26
gui2.py
|
|
@ -5,6 +5,8 @@ from PySide6.QtGui import QPixmap, QKeyEvent, QWheelEvent
|
|||
from PySide6.QtCore import Qt
|
||||
import sqlite3
|
||||
import sys
|
||||
from io import BytesIO
|
||||
import requests
|
||||
|
||||
class ProductViewer(QWidget):
|
||||
def __init__(self, db_path):
|
||||
|
|
@ -111,20 +113,29 @@ class ProductViewer(QWidget):
|
|||
self.price_label.setText(f"가격: {product[4]}")
|
||||
self.page_button.setText(f"{self.product_index + 1}/{self.total_products}")
|
||||
|
||||
# 중간 레이아웃에 있는 카드 레이아웃 초기화 및 데이터 로드
|
||||
# 이미지 로드
|
||||
self.load_image_from_url(self.image_label, product[2])
|
||||
|
||||
self.reset_card_widgets() # 상품 카드 리셋
|
||||
|
||||
# 중간 레이아웃에 있는 카드 레이아웃 초기화 및 데이터 로드
|
||||
search_results = self.load_search_results(product[0])
|
||||
for i, card in enumerate(self.card_layouts):
|
||||
if i < len(search_results):
|
||||
result = search_results[i]
|
||||
print(f"f{result}")
|
||||
card["name"].setText(result[0][:30] + "..." if len(result[0]) > 30 else result[2]) # 상품명 길이 제한
|
||||
card["price"].setText(result[2])
|
||||
card["select"].setText("미선택")
|
||||
card["widget"].setStyleSheet("border: 2px solid black;")
|
||||
# 각 카드에 이미지 로드
|
||||
self.load_image_from_url(card["image"], result[3]) # 이미지 URL에 맞춰 수정
|
||||
|
||||
else:
|
||||
card["name"].setText("")
|
||||
card["price"].setText("")
|
||||
card["select"].setText("미선택")
|
||||
card["image"].setText("")
|
||||
card["widget"].setStyleSheet("border: 1px solid grey;")
|
||||
|
||||
def reset_card_widgets(self):
|
||||
|
|
@ -132,13 +143,24 @@ class ProductViewer(QWidget):
|
|||
card["name"].setText("")
|
||||
card["price"].setText("")
|
||||
card["select"].setText("미선택")
|
||||
card["image"].setText("")
|
||||
card["widget"].setStyleSheet("border: 2px solid grey;")
|
||||
card["select"].setStyleSheet("color: black; font-weight: bold;")
|
||||
|
||||
def load_image_from_url(self, label, url):
|
||||
try:
|
||||
response = requests.get(url)
|
||||
if response.status_code == 200:
|
||||
pixmap = QPixmap()
|
||||
pixmap.loadFromData(BytesIO(response.content).read())
|
||||
label.setPixmap(pixmap.scaled(label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
|
||||
except Exception as e:
|
||||
print(f"Error loading image from URL {url}: {e}")
|
||||
|
||||
def load_search_results(self, product_id):
|
||||
# DB에서 search_results 테이블의 데이터를 로드
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("SELECT title, source, price, encrypted_url FROM search_results WHERE product_id=?", (product_id,))
|
||||
cursor.execute("SELECT title, source, price, imgurl FROM search_results WHERE product_id=?", (product_id,))
|
||||
return cursor.fetchall()
|
||||
|
||||
def previous_product(self):
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class BaiduImageSearcher:
|
|||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36",
|
||||
"Referer": "https://www.baidu.com/",
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
|
||||
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"
|
||||
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
"DNT": "1", # Do Not Track 요청 헤더
|
||||
"Connection": "keep-alive",
|
||||
|
|
@ -35,13 +35,13 @@ class BaiduImageSearcher:
|
|||
"Cache-Control": "max-age=0"
|
||||
})
|
||||
|
||||
initial_url = 'https://graph.baidu.com/pcpage/index?tpl_from=pc'
|
||||
self.initial_url = 'https://graph.baidu.com/pcpage/index?tpl_from=pc'
|
||||
upload_button_xpath = '//*[@id="app"]/div/div[1]/div[7]/div/span[1]/span[1]'
|
||||
|
||||
self.logger.info("goto URL")
|
||||
# self.page.route("**/*", lambda route, request: route.abort() if request.resource_type in ["image", "stylesheet", "font"] else route.continue_())
|
||||
|
||||
self.page.goto(initial_url) # 최초 접속 URL
|
||||
self.page.goto(self.initial_url) # 최초 접속 URL
|
||||
self.page.wait_for_selector(upload_button_xpath)
|
||||
|
||||
def close_browser(self):
|
||||
|
|
@ -53,29 +53,22 @@ class BaiduImageSearcher:
|
|||
|
||||
def upload_image(self, image_path):
|
||||
try:
|
||||
# 페이지 로드 대기
|
||||
self.page.wait_for_load_state('load')
|
||||
# 현재 URL 확인 및 로그 출력
|
||||
# current_url = self.page.url
|
||||
current_url = self.page.evaluate("() => window.location.href")
|
||||
self.logger.warning(f"Timeout occurred. Current URL: {current_url}")
|
||||
|
||||
# 현재 URL 재확인
|
||||
current_url = self.page.url
|
||||
self.logger.info(f"Initial Current URL: {current_url}")
|
||||
|
||||
# 에러 페이지 조건을 만족하는지 확인하는 루프
|
||||
for _ in range(5): # 5회 반복 (필요시 조정 가능)
|
||||
time.sleep(1) # 1초 대기 후 URL 확인
|
||||
current_url = self.page.url
|
||||
self.logger.info(f"Checked Current URL: {current_url}")
|
||||
|
||||
if current_url.startswith('https://graph.baidu.com/errpage'):
|
||||
self.logger.warning("Error page detected. Navigating back.")
|
||||
self.page.go_back()
|
||||
break
|
||||
else:
|
||||
self.logger.info("No error page detected, continuing.")
|
||||
self.logger.info(f"Current URL: {current_url}")
|
||||
|
||||
# 에러 페이지 조건 확인
|
||||
if current_url.startswith('https://graph.baidu.com/errpage'):
|
||||
self.logger.warning("Error page detected. Navigating back.")
|
||||
self.page.go_back() # 뒤로 가기
|
||||
# self.page.goto(self.initial_url) # 최초 접속 URL
|
||||
|
||||
|
||||
# 첫 번째 검색과 이후 검색의 선택자를 다르게 설정
|
||||
# if self.is_first_search:
|
||||
if self.is_first_search:
|
||||
self.logger.info("is_first_search")
|
||||
upload_button_xpath = '//*[@id="app"]/div/div[1]/div[7]/div/span[1]/span[1]'
|
||||
|
|
@ -86,10 +79,11 @@ class BaiduImageSearcher:
|
|||
upload_button_xpath = '//*[@id="app"]/div/div[1]/div/div[1]/div/div/div[1]/span[1]/span[1]'
|
||||
upload_input_xpath = '//*[@id="app"]/div/div[1]/div/div[1]/div/div/div[1]/div/div[2]/div[2]/div/form/input'
|
||||
|
||||
self.page.wait_for_selector(upload_button_xpath)
|
||||
|
||||
# 이미지 업로드 버튼 클릭 및 파일 업로드
|
||||
self.page.wait_for_selector(upload_button_xpath)
|
||||
self.page.click(upload_button_xpath)
|
||||
|
||||
self.page.wait_for_selector(upload_input_xpath)
|
||||
self.page.set_input_files(upload_input_xpath, image_path)
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error in upload_image: {e}", exc_info=True)
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class MainProcessor:
|
|||
|
||||
os.remove(image_path)
|
||||
self.logger.debug(f"Processed product ID: {product_id}")
|
||||
time.sleep(4)
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
self.logger.warning(f"Failed to process product ID: {product_id} - {e}", exc_info=True)
|
||||
|
||||
|
|
|
|||
BIN
products.db
BIN
products.db
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
Loading…
Reference in New Issue