hear ❤️ Google Translate ❤️ Very good, so I used it ❤️ selenium in Python ❤️ Climb it down!!

hear ❤️ Google Translate ❤️ Very good, so I used it ❤️ selenium in Python ❤️ Climb it down!!

1, Demand analysis

We know that there are many translation platforms on the Internet, such as Google translation, baidu translation, Youdao translation, Microsoft translation and so on. This time, we use selenium module to realize the crawling of Google translation.

We need to upload a file to Google translation, and then save the results of Google translation in a file.

Of course, we are fully automated. In other words, we use the program to automatically perform these translation operations.

2, Introduction to Google translation

Obviously, if you want to access Google translation, you need some magic knowledge, which is inconvenient to describe here, so if you need to, you can trust me privately, and I can teach you how to deal with it in private.

Google translation has two functions: the first is to directly translate text, and the second is to translate the content of a file:


This is the interface for directly translating text.


This is an interface for translating the contents of a file.

The second method we use this time is to upload a file for translation. Of course, as we said earlier, we use programs to automate operations.

3, Analysis of web interface (web page inspection)

We open the analysis interface of web page inspection:

Then, we know that we need to first click the document button, and then click the select File button:
1. Click the document button to jump:
We first find the location of the text navigation button:
We use the element selection tool to select:

2. Click the select document button:
The same method as the one above:

Then, after we finish adding files, a translation button will appear:

We also need to click the translation button to get the translation results.

4, Implement functions using Python (selenium module)

We give the code directly. There are more detailed notes:

from selenium import webdriver
# Import selenium module of python


def transfer(path):
    """
    This function is used to access Google translate, first get the path of the file, and then send it to the browser.
    :param path: The absolute path of the file you want to translate.
    :return: empty
    """

    browser = webdriver.Chrome()
    # Create browser objects

    browser.get('https://translate.google.cn/'
                '?hl=zh-CN&tab=TT&sl=zh-CN&tl=en&op=translate')
    # Visit Google Translate's website
    # The parameters inside set the language translation, which we can modify. Here is to translate Chinese into English

    h1 = browser.current_window_handle
    # Record current page
    # This operation is to place it. After clicking, we can't find the page we want

    ele = browser.find_elements_by_tag_name("button")
    # Get the button tag and click button
    # Here, the method of obtaining the button through the tag name is used to obtain the tag, which is convenient for subsequent click operations

    ele[1].click()
    # Click button

    all_h = browser.window_handles
    # Get all pages

    browser.switch_to.window(all_h[0])
    # Select the current page
    h2 = browser.current_window_handle

    upload = browser.find_element_by_id('i34')
    # Get the label of the button that gets "add file"
    # The id of this tag is obtained by checking the web page

    upload.send_keys(path)
    # Upload file
    # Path is the absolute path of the file, which we choose to specify

    print(upload.get_attribute('value'))
    # Print the path to the display file

    browser.find_elements_by_tag_name("button")[39].click()
    # This is the translation button after successfully uploading the file

    
    res = browser.page_source
    # This is to get the content of the page


    with open("result.txt", "w+") as f:
        # Here is to write the translation results to a file
        # Open a file and write the contents to it

        print(res[30: len(res) - 20])
        # Displays the results of the translation
        
        f.write(res[30: len(res) - 20])
        # Write the translation results to the file
        
        f.close()
        # You still need to close the file after the operation is completed     

    browser.close()
    # After all operations are completed, you need to close the browser


if __name__ == '__main__':
    # main function
    # This is a test

    transfer(path=r"C:\Users\1\Desktop\Google translate\google_transfer0\test.txt")
    # Pass the location of the file to the formal parameter path



The code after removing the comments is as follows:

from selenium import webdriver


def transfer(path):
    browser = webdriver.Chrome()
    browser.get('https://translate.google.cn/'
                '?hl=zh-CN&tab=TT&sl=zh-CN&tl=en&op=translate')
    h1 = browser.current_window_handle
    ele = browser.find_elements_by_tag_name("button")
    ele[1].click()
    all_h = browser.window_handles
    browser.switch_to.window(all_h[0])
    h2 = browser.current_window_handle
    upload = browser.find_element_by_id('i34')
    upload.send_keys(path)
    print(upload.get_attribute('value'))
    browser.find_elements_by_tag_name("button")[39].click()
    res = browser.page_source
    with open("result.txt", "w+") as f:
        print(res[30: len(res) - 20])
        f.write(res[30: len(res) - 20])
        f.close()
    browser.close()


if __name__ == '__main__':
    transfer(path=r"C:\Users\1\Desktop\Google translate\google_transfer0\test.txt")

5, Test function

We test:
test.txt file:

Files generated after translation:

Printing at interactive end:

The above is our test, so we can see that we have realized the functions we want.

6, Summary

In a word, the above is all the operations of crawling Google translation, mainly using the selenium module. I hope it will be of some help to you.

Finally, thank you for reading.

Thank you for your continuous support. We will make persistent efforts in the follow-up.

Tags: Python Google crawler

Posted on Tue, 21 Sep 2021 15:16:46 -0400 by DrAxeman