-
Notifications
You must be signed in to change notification settings - Fork 1
/
youdao.py
75 lines (72 loc) · 2.3 KB
/
youdao.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import random
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
print("有道翻译工具")
print("by Mirage 2021.5.19")
print("使用前请确认待翻译文件trans.txt已放在本目录")
print("正在启动chromedriver...")
browser = webdriver.Chrome(r'./chromedriver')
browser.get('https://fanyi.youdao.com/')
print('等待网页加载...')
time.sleep(3)
inputArea = browser.find_element(By.ID, 'inputOriginal')
fileName = 'Translate-' + time.strftime("%Y-%m-%d_%H.%M.%S", time.localtime()) + '.txt'
result = open(fileName, 'w', encoding='utf-8')
target = ''
lines = 0
current = 0
i = 0
if os.path.exists('log'):
current = int(open('log', encoding='utf-8', errors='ignore').read())
else:
open('log', 'w', encoding='utf-8')
print('正在加载进度...')
for line in open('trans.txt', encoding='utf-8', errors='ignore'):
while i <= current:
i += 1
break
if len(target) + len(line) >= 4500:
target = target + line
lines += 1
print(target)
inputArea.send_keys(target)
print('等待返回结果...')
print('行数:' + str(lines))
time.sleep(6)
for index in range(lines):
xpath = '//div[@id=\'transTarget\']/p[' + str(index + 1) + ']/span'
text = browser.find_elements_by_xpath(xpath)
for index2 in range(len(text)):
print(text[index2].text)
if text[index2].text != '\n' and text[index2].text != '':
result.write(text[index2].text)
result.write('\n')
result.flush()
inputArea.clear()
lines = 0
target = ''
time.sleep(random.randint(600, 1000) / 100)
else:
target = target + line
lines += 1
print(target)
inputArea.send_keys(target)
print('等待返回结果...')
print('行数:' + str(lines))
time.sleep(6)
for index in range(lines):
xpath = '//div[@id=\'transTarget\']/p[' + str(index + 1) + ']/span'
text = browser.find_elements_by_xpath(xpath)
for index2 in range(len(text)):
print(text[index2].text)
if text[index2].text != '\n' and text[index2].text != '':
result.write(text[index2].text)
result.write('\n')
result.flush()
inputArea.clear()
lines = 0
target = ''
browser.quit()
browser.stop_client()