88 lines
3.2 KiB
Python
88 lines
3.2 KiB
Python
import time
|
|
from selenium import webdriver
|
|
from selenium.webdriver.chrome.options import Options
|
|
from selenium.webdriver.chrome.service import Service
|
|
from selenium.webdriver.common.by import By
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
from tqdm import tqdm
|
|
from selenium_stealth import stealth
|
|
from fake_useragent import UserAgent
|
|
import random
|
|
import logging
|
|
|
|
# 로거 인스턴스 가져오기
|
|
logger = logging.getLogger('default_logger')
|
|
|
|
def trans(original_text):
|
|
|
|
# PC 사용자 에이전트
|
|
USER_AGENTS = [
|
|
# Chrome (Windows 10)
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
|
|
|
|
# Edge (Windows 10)
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.0.0",
|
|
|
|
# Firefox (Windows 10)
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0",
|
|
|
|
# Safari (macOS Monterey)
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15",
|
|
|
|
# Opera (Windows 10)
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 OPR/85.0.0.0"
|
|
]
|
|
random_user_agent = random.choice(USER_AGENTS)
|
|
|
|
# Start a Selenium driver
|
|
options = webdriver.ChromeOptions()
|
|
|
|
# ua = UserAgent()
|
|
# ua.user_agent_list = ua.pc_browsers
|
|
# # 랜덤 PC Agent 선택
|
|
# random_PC_UA = ua.random
|
|
# logger.debug(f"UserAgent : {random_PC_UA}")
|
|
# options.add_argument(f"--user-agent={random_PC_UA}") # 랜덤 user_agent 사용
|
|
|
|
options.add_argument(f"user-agent={random_user_agent}")
|
|
options.add_argument("--disable-blink-features=AutomationControlled")
|
|
options.add_argument('--ignore-certificate-errors')
|
|
options.add_argument('--ssl-protocol=any')
|
|
options.add_argument('--disable-cache')
|
|
options.add_argument('--headless')
|
|
driver = webdriver.Chrome(options=options)
|
|
|
|
# selenium-stealth 설정 적용
|
|
stealth(driver,
|
|
languages=["en-US", "en"],
|
|
vendor="Google Inc.",
|
|
platform="Win32",
|
|
webgl_vendor="Intel Inc.",
|
|
renderer="Intel Iris OpenGL Engine",
|
|
fix_hairline=True,
|
|
)
|
|
|
|
# Reach the deepL website
|
|
|
|
# deepl_url = 'https://www.deepl.com/ko/translator' # 한국어
|
|
# driver.get(deepl_url)
|
|
|
|
deepl_trans_url = "https://www.deepl.com/ko/translator#zh/ko/" + original_text
|
|
|
|
driver.get(deepl_trans_url)
|
|
|
|
time.sleep(4)
|
|
|
|
try:
|
|
WebDriverWait(driver, 10).until(
|
|
lambda driver: driver.find_element(By.CSS_SELECTOR, '[data-testid="translator-target-input"]').text.strip() != "")
|
|
translation_text = driver.find_element(By.CSS_SELECTOR, '[data-testid="translator-target-input"]')
|
|
content = translation_text.text
|
|
except Exception as e:
|
|
logger.error(f"Error fetching translation:{e}", exc_info=True)
|
|
content = "Translation failed"
|
|
|
|
driver.quit()
|
|
|
|
return content |