爬虫——有道云翻译

废话不多说直接上代码

固定文本内容

import time

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
# driver = webdriver.Chrome()
driver.get("https://fanyi.baidu.com/")

driver.find_element(By.CLASS_NAME, "f9WVaz_4").click()

text_to_input = """Sure! Here’s the classic fairy tale “Cinderella” in English:

Once upon a time, there was a kind and beautiful girl named Cinderella. Her father married a wicked stepmother, who brought along two selfish and arrogant stepsisters.

From that moment on, Cinderella became the household servant, doing countless chores every day, while her stepmother and stepsisters treated her cruelly.

One day, the king announced a grand ball, inviting all the young maidens in the kingdom. Cinderella longed to attend, but her stepmother forbade her.

Cinderella cried in disappointment, but then a fairy godmother appeared and helped her transform into a beautiful ball gown and provided her with a pair of glass slippers.

Dressed in her magnificent attire, Cinderella went to the ball. There, the prince was captivated by her beauty and kindness.

Unfortunately, time waits for no one, and when the clock struck midnight, Cinderella hurriedly left, leaving behind one of her glass slippers.

The prince searched the kingdom, and only Cinderella’s foot could fit into the glass slipper. Eventually, Cinderella put on the glass slipper and became the prince’s princess, living happily ever after.

From then on, Cinderella’s kindness and courage were remembered by all who knew her.
"""
# batch_size = 100  # 每个批次的文本数量
# for i in range(0, len(text_to_input), batch_size):
#     batch_text = text_to_input[i:i + batch_size]
#     try:
#         div_with_slate_node = driver.find_element(By.CSS_SELECTOR, "div[data-slate-node='element']")
#         div_with_slate_node.send_keys(batch_text)
#
#         # 使用显式等待等待文本输入完成
#         WebDriverWait(driver, 20).until(
#             EC.text_to_be_present_in_element((By.CSS_SELECTOR, "div[data-slate-node='element']"), batch_text))
#     except NoSuchElementException as e:
#         print("未找到指定的 div 元素:", e)

div_with_slate_node = driver.find_element(By.CSS_SELECTOR, "div[data-slate-node='element']")
div_with_slate_node.send_keys(text_to_input)

# WebDriverWait(driver, 13).until(
#             EC.text_to_be_present_in_element((By.CSS_SELECTOR, "div[data-slate-node='element']"), batch_text))

# WebDriverWait(driver, 5).until(
#     EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-slate-node='element']")))

# time.sleep(20)

timeout = 20

try:
    # 使用 driver.find_element 定位指定的元素
    div_element = WebDriverWait(driver, timeout).until(
        EC.presence_of_element_located((By.CLASS_NAME, "KzfYYzYR"))
    )

    span_elements = div_element.find_elements(By.CSS_SELECTOR, ".nbyIWSn1 span")

    # 提取每个span元素的文本内容
    for span_element in span_elements:
        text_content = span_element.text
        print(text_content)

except TimeoutException:
    print("超时:在{}秒内未能成功获取数据".format(timeout))

# 关闭浏览器
# driver.quit()

图形输入内容:

import random
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
import tkinter as tk

from selenium import webdriver
from selenium.webdriver.common.by import By

import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import tkinter.messagebox as mb

class WinGUIFy(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        self.tk_label_lwt9kgap = self.__tk_label_lwt9kgap(self)
        self.tk_input_lwt9lqdw = self.__tk_input_lwt9lqdw(self)
        self.tk_input_lwt9n2k3 = self.__tk_input_lwt9n2k3(self)
        self.tk_label_lwt9o4s7 = self.__tk_label_lwt9o4s7(self)
        self.tk_label_lwt9oj39 = self.__tk_label_lwt9oj39(self)
        self.tk_label_lwt9ta6h = self.__tk_label_lwt9ta6h(self)

    def File_like(self, even):
        try:
            chrome_options = Options()
            chrome_options.add_argument('--headless')  # 无界面模式
            chrome_options.add_argument('--disable-gpu')  # 禁用GPU加速

            driver = webdriver.Chrome(options=chrome_options)
            # driver = webdriver.Chrome()
            driver.get("https://fanyi.youdao.com/index.html#/")
            time.sleep(2)

            try:
                driver.find_element(By.CLASS_NAME,"close").click()
            except Exception as e:
                print("继续..")

            # 定位到包含文本为“翻译”的 span 元素的父元素 div
            button_element = driver.find_element(By.XPATH, "//div[@class='tab-item active color_text_1'][span='翻译']")

            # 点击按钮
            button_element.click()

            # 获取到文本框的值
            content = self.text_box.get("1.0", "end-1c")

            input_element = driver.find_element(By.ID, "js_fanyi_input")
            input_element.send_keys(content)

            time.sleep(4)
            text_content = driver.execute_script(
                "return document.getElementById('js_fanyi_output_resultOutput').textContent;")
            print(text_content)

            self.text_name.config(state="normal")
            self.text_name.delete('1.0', tk.END)
            self.text_name.insert(tk.END, text_content)
            self.text_name.config(state="disabled")

            # 关闭浏览器
            driver.quit()
        except Exception as e:
            # 弹框提示网络不流畅
            mb.showerror("Error", "网络不流畅,请尝试更换网络!")

    def __win(self):
        self.title("Tkinter布局助手")
        # 设置窗口大小、居中
        width = 900
        height = 700
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)

        self.resizable(width=False, height=False)

    def scrollbar_autohide(self, vbar, hbar, widget):
        """自动隐藏滚动条"""

        def show():
            if vbar: vbar.lift(widget)
            if hbar: hbar.lift(widget)

        def hide():
            if vbar: vbar.lower(widget)
            if hbar: hbar.lower(widget)

        hide()
        widget.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Leave>", lambda e: hide())
        if hbar: hbar.bind("<Enter>", lambda e: show())
        if hbar: hbar.bind("<Leave>", lambda e: hide())
        widget.bind("<Leave>", lambda e: hide())

    def v_scrollbar(self, vbar, widget, x, y, w, h, pw, ph):
        widget.configure(yscrollcommand=vbar.set)
        vbar.config(command=widget.yview)
        vbar.place(relx=(w + x) / pw, rely=y / ph, relheight=h / ph, anchor='ne')

    def h_scrollbar(self, hbar, widget, x, y, w, h, pw, ph):
        widget.configure(xscrollcommand=hbar.set)
        hbar.config(command=widget.xview)
        hbar.place(relx=x / pw, rely=(y + h) / ph, relwidth=w / pw, anchor='sw')

    def create_bar(self, master, widget, is_vbar, is_hbar, x, y, w, h, pw, ph):
        vbar, hbar = None, None
        if is_vbar:
            vbar = Scrollbar(master)
            self.v_scrollbar(vbar, widget, x, y, w, h, pw, ph)
        if is_hbar:
            hbar = Scrollbar(master, orient="horizontal")
            self.h_scrollbar(hbar, widget, x, y, w, h, pw, ph)
        self.scrollbar_autohide(vbar, hbar, widget)

    def __tk_label_lwt9kgap(self, parent):
        label = Label(parent, text="翻译中心", anchor="center", )
        label.place(x=319, y=29, width=277, height=30)
        return label

    # 输入
    def __tk_input_lwt9lqdw(self, parent):
        self.text_box = tk.Text(parent, )
        self.text_box.place(x=21, y=110, width=385, height=456)
        self.text_box.bind("<Return>", self.File_like)
        return self.text_box

    def __tk_input_lwt9n2k3(self, parent):
        self.text_name = tk.Text(parent, state="disabled")
        self.text_name.place(x=480, y=107, width=407, height=459)
        return self.text_name

    def __tk_label_lwt9o4s7(self, parent):
        label = Label(parent, text="内容区", anchor="center", )
        label.place(x=159, y=69, width=121, height=30)
        return label

    def __tk_label_lwt9oj39(self, parent):
        label = Label(parent, text="翻译区", anchor="center", )
        label.place(x=656, y=65, width=80, height=30)
        return label

    def __tk_label_lwt9ta6h(self, parent):
        label = Label(parent, text="回\n车\n执\n行\n", anchor="center", )
        label.place(x=417, y=105, width=50, height=235)
        return label


class Win(WinGUIFy):
    def __init__(self, controller):
        self.ctl = controller
        super().__init__()
        self.__event_bind()
        self.__style_config()
        self.ctl.init(self)

    def __event_bind(self):
        pass

    def __style_config(self):
        pass


if __name__ == "__main__":
    win = WinGUIFy()
    win.mainloop()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/686817.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Python接口自动化测试:Json 数据处理实战

&#x1f345; 视频学习&#xff1a;文末有免费的配套视频可观看 &#x1f345; 点击文末小卡片 &#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 上一篇说了关于json数据处理&#xff0c;是为了断言方便&#xff0c;这篇就带各位小伙伴实战一下…

SpringBootWeb 篇-深入了解 AOP 面向切面编程与 AOP 记录操作日志案例

&#x1f525;博客主页&#xff1a; 【小扳_-CSDN博客】 ❤感谢大家点赞&#x1f44d;收藏⭐评论✍ 文章目录 1.0 AOP 概述 1.1 构造简单 AOP 类 2.0 AOP 核心概念 2.1 AOP 执行流程 3.0 AOP 通知类型 4.0 AOP 通知顺序 4.1 默认按照切面类的类名字母排序 4.2 用 Order(数字) 注…

b端系统类管理平台设计前端开发案例

b端系统类管理平台设计前端开发案例

(学习笔记)数据基建-数据质量

数据基建-数据质量 数据质量数据质量保障措施如何推动上下游开展数据质量活动数据质量保障如何量化产出数据质量思考全链路数据质量保障项目 数据质量 概念&#xff1a;数据质量&#xff0c;意如其名&#xff0c;就是数据的准确性&#xff0c;他是数据仓库的基石&#xff0c;控…

【案例分享】印前制版工单系统:“鹿山科技”助力“铭匠数据”重塑业务流程

内容概要 本文介绍了鹿山信息科技通过明道云HAP平台的数字化解决方案提升了铭匠数据在印前制版行业的效率。周口铭匠数据科技有限公司位于河南省周口市沈丘县&#xff0c;是一家专注于印前制版设计服务的公司&#xff0c;成立于2023年。企业在销售业务、版材制作生产和美工设计…

CATIA入门操作案例——草图绘制案例,导入草图图片,尺寸约束直径/半径切换,草图分析闭合检查,草图固定

目录 引出草图绘制&#xff0c;导入图片方便绘制新建product&#xff0c;进入sketch tracer模块技巧&#xff1a;尺寸直径 / 半径切换技巧&#xff1a;右键&#xff0c;自动搜索 草图分析&#xff1a;检查闭合警告&#xff1a;Change it to material mode to see the Paintings…

60V大功率半桥GaN半桥驱动器替代LMG1210

1. 产品特性&#xff08;替代LMG1210&#xff09; ➢ 工作频率高达 10MHz ➢ 20ns 典型传播延迟 ➢ 5ns 高侧/低侧匹配 ➢ 两种输入控制模式 ➢ 具有可调死区时间的单个 PWM 输入、 独立输入模式 ➢ 1.5A 峰值拉电流和 3A 峰值灌电流 ➢ 内置 5V LDO ➢ 欠压保护 ➢ 过…

小程序简单版录音机

先来看看效果 结构 先来看看页面结构 <!-- wxml --><view class"wx-container"><view id"title">录音机</view><view id"time">{{hours}}:{{minute}}:{{second}}</view><view class"btngroup"…

【人工智能】第七部分:ChatGPT的未来展望

人不走空 &#x1f308;个人主页&#xff1a;人不走空 &#x1f496;系列专栏&#xff1a;算法专题 ⏰诗词歌赋&#xff1a;斯是陋室&#xff0c;惟吾德馨 目录 &#x1f308;个人主页&#xff1a;人不走空 &#x1f496;系列专栏&#xff1a;算法专题 ⏰诗词歌…

kafka安装流程

安装kafka前需要安装zookeeper zookeeper安装教程 1.新建一个logs文件夹 2.修改配置文件 3.修改listeners参数 4.以管理员身份启动kafka服务 .\bin\windows\kafka-server-start.bat .\config\server.properties 如果报 输入行太长。 命令语法不正确。 解决方案如下&#x…

基于工业互联网打造敏捷供应链的实现方式:创新路径与实践应用

引言 工业互联网和敏捷供应链是当今制造业发展中的两个重要概念。工业互联网以数字化、网络化和智能化为核心&#xff0c;致力于将传统工业生产与互联网技术相融合&#xff0c;从而实现生产过程的高效、智能和灵活。而敏捷供应链则强调快速响应市场需求、灵活调整生产和供应计划…

调用华为云实现人证核身证件版(二要素)

目录 1.作者介绍2.华为云人证核身2.1什么是人证核身2.2应用场景2.3限制要求 3.流程介绍3.1调用API实现3.2调用SDK实现 1.作者介绍 高凡平&#xff0c;男&#xff0c;西安工程大学电子信息学院&#xff0c;2023级研究生 研究方向&#xff1a;数码印花缺陷检测 电子邮件&#xf…

基于扩散动力学模型的乳腺癌在不完整DCE-MRI中的分割

文章目录 Diffusion Kinetic Model for Breast Cancer Segmentation in Incomplete DCE-MRI摘要方法实验结果 Diffusion Kinetic Model for Breast Cancer Segmentation in Incomplete DCE-MRI 摘要 针对现有方法需要完整时间序列数据(尤其是增强后图像)的问题,DKM仅利用预增…

wordpress里面嵌入哔哩哔哩视频的方法

我们正常如果从blibli获取视频分享链接然后在wordpress里面视频URL插入&#xff0c;发现是播放不了的 而视频嵌入代码直接粘贴呢窗口又非常的小 非常的难受&#xff0c;就需要更改一下代码。你可以在在allowfullscreen"true"的后面&#xff0c;留1个空格&#xff…

掌控数据流:深入解析 Java Stream 编程

Java 8 引入了一种新的抽象称为流&#xff08;Stream&#xff09;&#xff0c;它可以让你以一种声明的方式处理数据。Java 8 Stream API 可以极大提高 Java 程序员的生产力&#xff0c;使代码更简洁&#xff0c;更易读&#xff0c;并利用多核架构进行外部迭代。这里将详细介绍 …

Python Flask实现蓝图Blueprint配置和模块渲染

Python基础学习&#xff1a; Pyhton 语法基础Python 变量Python控制流Python 函数与类Python Exception处理Python 文件操作Python 日期与时间Python Socket的使用Python 模块Python 魔法方法与属性 Flask基础学习&#xff1a; Python中如何选择Web开发框架&#xff1f;Pyth…

SQLserver通过CLR调用TCP接口

一、SQLserver启用CLR 查看是否开启CRL&#xff0c;如果run_value1&#xff0c;则表示开启 EXEC sp_configure clr enabled; GO RECONFIGURE; GO如果未启用&#xff0c;则执行如下命令启用CLR sp_configure clr enabled, 1; GO RECONFIGURE; GO二、创建 CLR 程序集 创建新项…

【算法】深入浅出爬山算法:原理、实现与应用

人不走空 &#x1f308;个人主页&#xff1a;人不走空 &#x1f496;系列专栏&#xff1a;算法专题 ⏰诗词歌赋&#xff1a;斯是陋室&#xff0c;惟吾德馨 目录 &#x1f308;个人主页&#xff1a;人不走空 &#x1f496;系列专栏&#xff1a;算法专题 ⏰诗词歌…

简单聊下办公白环境

在当今信息化时代&#xff0c;办公环境对于工作效率和员工满意度有着至关重要的影响。而白名单作为一种网络安全策略&#xff0c;其是否适合办公环境&#xff0c;成为了许多企业和组织需要思考的问题。本文将从白名单的定义、特点及其在办公环境中的应用等方面&#xff0c;探讨…

Excel 生成所在月份的每一天列表

Excel 的 A2 格是日期 A1Fecha201/03/24 需要生成该日期所在月份的每一天的列表 A1WholeMonth201/03/24302/03/24403/03/24504/03/24605/03/24706/03/24807/03/24908/03/241009/03/241110/03/241211/03/241312/03/241413/03/241514/03/241615/03/241716/03/241817/03/241918…