<jupyter_start><jupyter_text>DeepL Scrape<jupyter_code># Import necessary modules
import time
import pandas as pd
from gtts import gTTS
from playsound import playsound
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Define start time
startTime = time.time()
# DEFINE FUNCTIONS
def convert(seconds): 
    seconds = seconds % (24 * 3600) 
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60
    return "%dh %02dm %02ds" % (hour, minutes, seconds)

def convertTTS(seconds): 
    seconds = seconds % (24 * 3600) 
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60
    return "%d hours %02d minutes %02d seconds" % (hour, minutes, seconds)
# Define a project name
project = 'アニメ'
# Define the URL to be opened
url = 'https://www.deepl.com/translator'
# Create a DataFrame of the sentences to be translated
source = pd.read_excel('源流.xlsx', header = None)
# Define the driver for Selenium to use
driver = webdriver.Chrome('chromedriver.exe')
# Open the specfied URL
driver.get(url)
# Open the "Select Language" dropdown menu
buttonSource = driver.find_element_by_xpath('//*[@id="dl_translator"]/div[3]/div[2]/div[1]/div[1]/div').click()
# Click the Japanese button
button = driver.find_element_by_xpath('//*[@id="dl_translator"]/div[3]/div[2]/div[1]/div[1]/div/div/button[11]').click()
# Find the text field
textFieldSource = driver.find_element_by_xpath('//*[@id="dl_translator"]/div[3]/div[2]/div[1]/div[2]/div[1]/textarea')
# Type the text into the text field
test = source.loc[0:0].to_string(index = False, columns = None, header = False)
textFieldSource.send_keys(source[0])
# Pause to avoid errors
time.sleep(5)
# Store the translated text as a string
translatedText = driver.find_element_by_xpath('//*[@id="target-dummydiv"]')
translatedText_content = translatedText.get_attribute('innerHTML').strip()
# Sleep to avoid errors
time.sleep(2)
# Clean the output text and print
textClean = translatedText_content.replace("&nbsp;", "")
# Sleep to avoid errors
time.sleep(2)
# Close the browser window
driver.quit()
# Turn the scraped string into a list
sentences = textClean.split('.')
sentencesClean = []
for element in sentences:
    sentencesClean.append(element + ".")
# Create a DataFrame
cols = ['日本語', 'English']
df = pd.DataFrame(columns = cols)
df['English'] = sentencesClean
df['日本語'] = source[0]
# Drop nan rows
df.dropna(inplace = True, axis = 0)
# Export DataFrame as an XLSX to Google Drive\Work\Translation folder
now = datetime.now().strftime("%Y-%m-%d_%H.%M.%S")
df.to_excel('日本語\{}.xlsx'.format(project), index=False)
os.startfile('日本語\{}.xlsx'.format(project))
# Play some audio to let alert the user to the scripts completion
tts = gTTS(text = "I have finished translating the sentences in {}. Please fix my mistakes, master.".format(convertTTS(time.time() - startTime)), lang = "en")
tts.save("tts.mp3")
playsound("tts.mp3")
os.remove("tts.mp3")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13643459256946613, 'fraction_numerical': 0.021854511395566656, 'mean_word_length': 4.977611940298507, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '27197', 'license': 'MIT', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1038, 'n_words': 297, 'path': 'Old Scripts/日本語機械翻訳スクラップ.ipynb', 'repo_name': 'mikitz/machine-translation-scraping', 'size': '3203'}