我需要一个循环来重新启动,因为在 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