Python+selenium实现谷歌翻译

一、需求分析

我们知道,网上有很多的翻译平台,比如:Google翻译、百度翻译、有道翻译、微软翻译等等。本次我们来使用selenium模块实现对Google翻译的爬取的实现。

我们需要上传一个文件给Google翻译,然后再将Google翻译的结果保存在一个文件之中。

当然,我们有完全自动化的处理程序。换句话说,我们使用该程序自动执行这些翻译操作。

[En]

Of course, we have fully automated processing. In other words, we use the program to carry out these translation operations automatically.

二、Google 翻译介绍

显然,如果你想要访问Google翻译是需要一些魔法知识的,这个不方便在这里叙述,所以如果有需要的话可以私信我,我可以私下教你如何处理了啦。

Google翻译有两种功能,第一种是直接翻译文本,第二种是翻译一个文件里面的内容:

这是用于直接翻译文本的界面。

[En]

This is the interface for translating text directly.

这是用于翻译文件内容的接口。

[En]

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

我们这次使用的是第二种方法,即上传文件进行翻译。当然,正如我们前面所说的,我们使用程序来自动化操作。

[En]

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

; 三、网页界面的分析(网页检查)

我们打开网页检查的分析界面:

[En]

We open the analysis interface for web page inspection:

然后,我们知道需要首先单击文档上的按钮,然后单击按钮选择文件:

[En]

Then, we know that we need to click the button on the document first, and then click the button to select the file:

1、点击文档按钮进行跳转:

我们首先找到文本导航按钮的位置:

[En]

We first find the location of the text navigation button:

我们使用元素选择工具进行选择:

[En]

We use the element selection tool to make the selection:

; 2、点击选择文档的按钮:

方法同上:

[En]

The method is the same as the one above:

然后,当我们完成添加文件时,将出现一个用于翻译的按钮:

[En]

Then, when we have finished adding the file, a button for translation appears:

我们还需要点击翻译按钮来获得翻译的结果。

[En]

We also need to click the translation button to get the result of the translation.

四、使用Python进行实现功能(selenium模块)

我们就给出密码吧。其中有更详细的评论:

[En]

Let’s just give the code. There are more detailed comments in it:

python学习交流Q群:906715085###
from selenium import webdriver

导入 python 的 selenium 模块

def transfer(path):
"""
此函数用于访问Google翻译,首先获取文件的路径,然后将其发送到浏览器。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>This function is used to access Google translation, first getting the path to the file, and then sending it to the browser.</font>*</details>
:param path: 你想要翻译的文件的绝对路径。
:return: 空
"""
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
记录当前的页面
此操作是在点击后找不到我们想要的页面。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>This operation is that we can't find the page we want after clicking.</font>*</details>
ele = browser.find_elements_by_tag_name("button")
获取得到 button 标签并且点击 button
这里采用了通过标签名来获取 button 的方法来进行获取标签,方便后面的点击操作    ele[1].click()
点击 button    all_h = browser.window_handles
获取得到所有的页面    browser.switch_to.window(all_h[0])
选择当前的页面    h2 = browser.current_window_handle    upload = browser.find_element_by_id('i34')
获取得到 "添加文件" 的那个按钮的标签
这个标签的 id 是我们通过网页检查获取得到的啦    upload.send_keys(path)
上传文件
path 是文件的绝对路径,有我们自己选择指定    print(upload.get_attribute('value'))
打印显示文件的路径    browser.find_elements_by_tag_name("button")[39].click()
这是点击文件成功上载的翻译按钮。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>This is the translation button that clicks on the successful upload of the file.</font>*</details>
res = browser.page_source    # 这个是获取得到页面的内容
with open("result.txt", "w+") as f:        # 这里是将翻译的结果写入一个文件
打开文件并将内容写入该文件<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Open a file and write the contents to the file</font>*</details>
print(res[30: len(res) - 20])        # 显示翻译的结果
f.write(res[30: len(res) - 20])        # 将翻译的结果写入文件之中
f.close()        # 操作完成文件以后还是需要进行关闭的操作的啦
browser.close()    # 所有的操作完成以后需要关闭浏览器了啦
if __name__ == '__main__':    # main函数    # 这个是测试
transfer(path=r"C:\Users\1\Desktop\Google翻译\google_transfer0\test.txt")    #
 将文件的位置传递到形参 path 里面

删除评论后的代码如下:

[En]

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__':
ransfer(path=r"C:\Users\1\Desktop\Google翻译\google_transfer0\test.txt")
五、测试功能

我们进行测试:

test.txt文件:

Python+selenium实现谷歌翻译

翻译完成后生成的文件:

[En]

The files generated after the translation is completed:

交互端的打印:

以上是我们的测试,这表明我们已经实现了我们想要的功能。今天的代码似乎不是很多,所以它非常适合动手实践。这里。

[En]

The above is our test, which shows that we have achieved the function we want. Today’s code doesn’t seem to be very much, so it’s very suitable for hands-on practice. Here.

分享结束了。下一章见。

[En]

The sharing is over. See you in the next chapter.

Original: https://www.cnblogs.com/1234567FENG/p/16381861.html
Author: 蚂蚁爱Python
Title: Python+selenium实现谷歌翻译

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/499361/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Python matplotlib绘制图表

    matplotlib是Python中的一个第三方库。主要用于开发2D图表,以渐进式、交互式的方式实现数据可视化,可以更直观的呈现数据,使数据更具说服力。 一、安装matplotli…

    Python 2023年8月31日
    056
  • seaborn简明教程(一)

    1. Seaborn简介 seaborn是基于 matplotlib的数据可视化库。它在 matplotlib的基础上,进行了更高级的API封装,从而使得绘图更加容易,不需要经过大…

    Python 2023年9月5日
    041
  • Linux实例丢失SSH密钥对的解决步骤

    右键点击开始按钮 > Windows PowerShell, 参考下面命令从私钥文件中检索公有密钥(-f参数后面请根据实际情况替换为pem文件的完整路径, 下面例子操作的是浏…

    Python 2023年8月10日
    038
  • RLHF:基于人类反馈(Human Feedback)对语言模型进行强化学习【Reinforcement Learning from Human Feedback】

    笔者读过之后,觉得讲解的还是蛮清晰的,因此提炼了一下核心脉络,希望给对ChatGPT技术原理感兴趣的小伙伴带来帮助。 但其实这种生成模型很难训练。以语言模型为例,大多是采用&#82…

    Python 2023年11月4日
    060
  • Pandas 表连接(concat,merge,append)

    Pandas 表连接(concat,merge,append) ## 1.pd.concat([left,right],axis=1) ## 2.pd.merge(left,rig…

    Python 2023年8月18日
    033
  • Python如何读取Excel表内容

    用python读取excel表中的数据 假如说有如下一张存储了数据的excel表,其中x1-x6是特征,y_label是特征对应的类别标签。我们想要使用python对以下数据进行数…

    Python 2023年8月1日
    043
  • 如何将 Matplotlib 可视化 插入到 Excel 表格中?

    大家好,我是小五🧐 在生活中工作中,我们经常使用Excel用于储存数据,Tableau等BI程序处理数据并进行可视化。我们也经常使用R、Python编程进行高质量的数据可视化,生成…

    Python 2023年8月31日
    042
  • 美化Matplotlib的3个小技巧

    Matplotlib是Python的数据可视化库的基础。它是其他可视化工具(如Seaborn)的基础。 Matplotlib提供了很大的灵活性,因此您可以自定义或调整几乎所有的图表…

    Python 2023年8月31日
    051
  • 【Python】Windows微信清理工具v.3.0.1

    Windows微信清理工具v.3.0.1 今天,我原创的Windows微信清理工具迎来最大更新! v.3.0.0更新内容: 1、使用tkinter重构GUI,界面更简单易用!2、增…

    Python 2023年11月9日
    040
  • python_透视表操作unstack

    Data Wrangling: Join, Combine,¶# 第8章 数据规整:聚合、合并和重塑# 在许多应⽤中,数据可能分散在许多⽂件或数据库中,存储的形# 式也不利于分析。…

    Python 2023年5月25日
    055
  • Pytest测试框架一键动态切换环境思路及方案

    前言 在上一篇文章《Pytest fixture及conftest详解》中,我们介绍了fixture的一些关键特性、用法、作用域、参数等,本篇文章将结合fixture及confte…

    Python 2023年9月11日
    055
  • 爬虫一

    爬虫: 1,爬虫基础 #1、什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,像一张网一样。 #2、互联网建立的目的? 互联网的核心价值…

    Python 2023年6月12日
    053
  • 配置 conda 镜像环境

    环境: conda/4.11.0 CPython/3.8.8 Windows/10镜像源选用阿里云镜像站anaconda镜像:https://mirrors.aliyun.com/…

    Python 2023年9月9日
    049
  • 爬虫框架Scrapy总结笔记

    前言 由于工作需要,学习一下爬虫框架,在网上看了别人的笔记和教学视频,想总结一下便于以后复习用,然后和大家分享一下。 我的总结大概分为四步: Scrapy的整体架构 使用Scrap…

    Python 2023年10月2日
    035
  • 2021-09-30 Backtesting.py 恒生指数

    主要是使用backtesting.py做了个回测,对象是恒生指数,数据源是yahoo finance,策略是基于夏普率的Z-score 配合给定阈值,然后目标是计算9格平均夏普率的…

    Python 2023年8月9日
    030
  • 【python】环境配置|添加环境变量|python+conda+pip+mysql 超详细。

    环境配置?一篇让你玩明白! 什么是环境?说白了就是你的电脑可以识别的路径! 凡是遇到: “无法识别pip等命令” “无法识别conda等命令&#…

    Python 2023年9月8日
    057
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球