我需要重新启动循环,因为在google使用googletrans模块阻止我之前,我只能处理多少个元素。
我已经计算出可以使用一个随机的时间延迟获得大约50个元素,直到Google阻止我,但我需要它遍历大约850个。
据我所知,无法重新启动循环,所以我尝试了while循环,但没有看到更新循环并在处理完第一个块后完成。
我还在随机设置两次翻译之间的第二个间隔,以保持循环正常运行。从0到50,然后停止循环
我的代码
from googletrans import Translator
from random import randint
import datetime
should_restart = True
spanish_subs = get_subs(page)# list of over 850 sentances to be translated
counter_num = 1
translator = Translator()
start_block = 0
end_block = 50
while should_restart:
print('start_block ' + str(start_block))# see where the loop is in the process
print('end_block ' + str(end_block))
if end_block < len(get_subs(page)):
translations = translator.translate(spanish_subs[start_block:end_block], src='es')
for translation in translations:
english_subs.append(translation.text)
print('Loop ' + str(counter_num + 1 ))
time.sleep(random())# pauses between 1 and 10 seconds
if end_block >= len(get_subs(page)):
should_restart = False
with open('englist_translation.txt', 'w') as f:
for item in english_subs:
f.write("%s\n" % item)
print('Finished')
start_block = end_block + 50
end_block = end_block + 50 # date the end block
print(english_subs)# print to console to see what was translated
return english_subs
def random():
random_number = randint(0, 10)
return random_number
答案 0 :(得分:0)
此设置将帮助您超越Google的限制,这是一个缓慢的过程,但可处理50,000个字符的列表。
p
然后生成随机时间延迟
%~dp0