# -*- coding: utf-8 -*- # @Author: wangkun # @Time: 2023/2/8 import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.common.desired_capabilities import DesiredCapabilities class Translate: @classmethod def google_translate(cls, strs, machine): # 打印请求配置 ca = DesiredCapabilities.CHROME ca["goog:loggingPrefs"] = {"performance": "ALL"} # 不打开浏览器运行 chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--headless") chrome_options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36') chrome_options.add_argument("--no-sandbox") # driver初始化 if machine == 'aliyun' or machine == 'aliyun_hk': driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options) elif machine == 'macpro': driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, service=Service('/Users/lieyunye/Downloads/chromedriver_v86/chromedriver')) elif machine == 'macair': driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, service=Service('/Users/piaoquan/Downloads/chromedriver')) else: driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, service=Service('/Users/wangkun/Downloads/chromedriver/chromedriver_v110/chromedriver')) driver.implicitly_wait(10) # url = 'https://fanyi.baidu.com/?aldtype=16047#auto/zh' # 百度翻译 url = 'https://translate.google.de/?hl=zh-CN' # 谷歌翻译 driver.get(url) time.sleep(3) # driver.save_screenshot('./1.png') accept_btns = driver.find_elements(By.XPATH, '//span[text()="全部接受"]') accept_btns_eng = driver.find_elements(By.XPATH, '//span[text()="Accept all"]') if len(accept_btns) != 0: accept_btns[0].click() time.sleep(2) elif len(accept_btns_eng) != 0: accept_btns_eng[0].click() time.sleep(2) textarea = driver.find_element(By.XPATH, '//textarea[@class="er8xn"]') textarea.send_keys(strs) time.sleep(5) translate_list = driver.find_elements(By.XPATH, '//span[@class="ryNqvb"]') translate_word_list = [] for text in translate_list: word = text.get_attribute("innerHTML") translate_word_list.append(word) translate_word = "".join(translate_word_list) time.sleep(1) driver.quit() return translate_word @classmethod def baidu_translate(cls, strs, machine): # 打印请求配置 ca = DesiredCapabilities.CHROME ca["goog:loggingPrefs"] = {"performance": "ALL"} # 不打开浏览器运行 chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--headless") chrome_options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36') chrome_options.add_argument("--no-sandbox") # driver初始化 if machine == 'aliyun' or machine == 'aliyun_hk': driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options) elif machine == 'macpro': driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, service=Service('/Users/lieyunye/Downloads/chromedriver_v86/chromedriver')) elif machine == 'macair': driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, service=Service('/Users/piaoquan/Downloads/chromedriver')) else: driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, service=Service('/Users/wangkun/Downloads/chromedriver/chromedriver_v110/chromedriver')) # driver.implicitly_wait(10) url = 'https://fanyi.baidu.com/?aldtype=16047#auto/zh' # 百度翻译 # url = 'https://translate.google.de/?hl=zh-CN' # 谷歌翻译 driver.get(url) time.sleep(1) # driver.save_screenshot('./1.png') close_btns = driver.find_elements(By.XPATH, '//span[@class="app-guide-close"]') accept_btns = driver.find_elements(By.XPATH, '//span[text()="全部接受"]') accept_btns_eng = driver.find_elements(By.XPATH, '//span[text()="Accept all"]') if len(close_btns) != 0: close_btns[0].click() time.sleep(2) elif len(accept_btns) != 0: accept_btns[0].click() time.sleep(2) elif len(accept_btns_eng) != 0: accept_btns_eng[0].click() time.sleep(2) textarea = driver.find_element(By.XPATH, '//textarea[@id="baidu_translate_input"]') textarea.send_keys(strs) time.sleep(5) translate_list = driver.find_elements(By.XPATH, '//p[@class="ordinary-output target-output clearfix"]') translate_word_list = [] for text in translate_list: word = text.find_elements(By.TAG_NAME, 'span') for word_text in word: word = word_text.text translate_word_list.append(word) # print(word) translate_word = "".join(translate_word_list) # print(translate_word) time.sleep(1) driver.quit() return translate_word @classmethod def is_contains_chinese(cls, strs): """ 检查语句中是否包含中文字符 :param strs: 需要查询的句子 :return: 包含:True,不包含:False """ for _char in strs: if '\u4e00' <= _char <= '\u9fa5': return True return False @classmethod def is_all_chinese(cls, strs): """ 检查语句中是否全是中文字符,有标点符号也会返回 False :param strs:需要查询的句子 :return:包含:True,不包含:False """ for _char in strs: if not '\u4e00' <= _char <= '\u9fa5': return False return True if __name__ == "__main__": # is_contains_chinese = Translate.is_contains_chinese('SENSATIONAL Singer Wins the GROUP GOLDEN BUZZER with a STUNNING Audition! | Got Talent Global') # print(is_contains_chinese) # # is_all_chinese = Translate.is_all_chinese('SENSATIONAL Singer Wins the GROUP GOLDEN BUZZER with a STUNNING Audition!') # is_all_chinese = Translate.is_all_chinese('啊啊啊') # print(is_all_chinese) strs1 = 'SENSATIONAL Singer Wins the GROUP GOLDEN BUZZER with a STUNNING Audition! | Got Talent Global' # Translate.google_translate(strs1, 'local') Translate.baidu_translate(strs1, 'local') pass