提交 7fe3a354 作者: LiuLiYuan

Edge翻译 01/18

上级 d9bf9b2f
import pyautogui import pyautogui
from bs4 import BeautifulSoup
from retry import retry from retry import retry
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.action_chains import ActionChains
...@@ -8,11 +9,16 @@ from selenium.webdriver.support import expected_conditions as EC ...@@ -8,11 +9,16 @@ from selenium.webdriver.support import expected_conditions as EC
import time import time
from bson import ObjectId from bson import ObjectId
import pymongo import pymongo
from base import BaseCore
baseCore = BaseCore.BaseCore()
log = baseCore.getLogger()
db_storage = pymongo.MongoClient('mongodb://114.115.221.202:27017/', username='admin', password='ZZsn@9988').中科软[
'数据源_0106']
# 获取当前活动窗口的标题 # 获取当前活动窗口的标题
def get_active_window_title(): def get_active_window_title():
window = pyautogui.getActiveWindow() window = pyautogui.getActiveWindow()
print(f'当前活动窗口的标题是:{window.title}') log.info(f'当前活动窗口的标题是:{window.title}')
return window.title if window else None return window.title if window else None
...@@ -50,42 +56,46 @@ def Translate(_id, driver): ...@@ -50,42 +56,46 @@ def Translate(_id, driver):
time.sleep(2) time.sleep(2)
if driver.find_element(By.TAG_NAME, 'body').text[:500] in flag: if driver.find_element(By.TAG_NAME, 'body').text[:500] in flag:
print(f'{_id}---翻译失败,重试') log.error(f'{_id}---翻译失败,重试')
# 使用pyautogui模块模拟按下Alt+Tab键,将Edge浏览器置于最前面 # 使用pyautogui模块模拟按下Alt+Tab键,将Edge浏览器置于最前面
# while get_a # while get_a
# ctive_window_title() != "Edge浏览器": # ctive_window_title() != "Edge浏览器":
while 'Microsoft​ Edge' not in get_active_window_title(): while 'Microsoft​ Edge' not in get_active_window_title():
pyautogui.hotkey('alt', 'tab') pyautogui.hotkey('alt', 'tab')
print('窗口切换操作') log.info('窗口切换操作')
# pyautogui.hotkey('alt', 'tab') # pyautogui.hotkey('alt', 'tab')
# 切换到Edge浏览器窗口 # 切换到Edge浏览器窗口
driver.switch_to.window(edge_handle) driver.switch_to.window(edge_handle)
driver.refresh() driver.refresh()
raise raise
from bs4 import BeautifulSoup
page_source = driver.page_source page_source = driver.page_source
contentWithTag = BeautifulSoup(page_source, 'html.parser') contentWithTag = BeautifulSoup(page_source, 'html.parser')
with open(rf'C:\Users\EDY\Desktop\{_id}.html', 'w', encoding='utf-8') as f: db_storage.update_one({'_id':ObjectId(_id)},{'$set':{'postCode':'18','richText':str(contentWithTag)}})
f.write(str(contentWithTag)) # with open(rf'C:\Users\EDY\Desktop\{_id}.html', 'w', encoding='utf-8') as f:
# f.write(str(contentWithTag))
# print(str(contentWithTag)) # print(str(contentWithTag))
if __name__ == "__main__": def doJob():
driver = webdriver.Edge() driver = webdriver.Edge()
while True:
datas = db_storage.find({'postCode':'2'}).limit(10)
for data in datas:
now = time.time()
_id = str(data['_id'])
richTextForeign = data['richTextForeign']
with open(r'C:\Users\EDY\Desktop\aaa.html', 'w', encoding='utf-8') as f:
f.write(str(richTextForeign))
try:
db_storage = pymongo.MongoClient('mongodb://114.115.221.202:27017/', username='admin', password='ZZsn@9988').中科软[ Translate(_id, driver)
'数据源_0106'] log.info(f'{_id}翻译用时--{time.time() - now}')
datas = db_storage.find({'postCode':'2'}).limit(10) except:
for data in datas: log.error(f'{_id}翻译失败')
now = time.time()
_id = str(data['_id'])
richTextForeign = data['richTextForeign']
with open(r'C:\Users\EDY\Desktop\aaa.html', 'w', encoding='utf-8') as f:
f.write(str(richTextForeign))
try:
Translate(_id, driver)
except: if __name__ == "__main__":
print('翻译失败') doJob()
print(f'{_id}翻译用时--{time.time() - now}') baseCore.close()
\ No newline at end of file \ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论