爬虫——有道云翻译

废话不多说直接上代码

固定文本内容

python 复制代码
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()

图形输入内容:

python 复制代码
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()
相关推荐
刘琦沛在进步3 分钟前
【C / C++】引用和函数重载的介绍
c语言·开发语言·c++
alvin_200510 分钟前
python之OpenGL应用(二)Hello Triangle
python·opengl
机器视觉的发动机15 分钟前
AI算力中心的能耗挑战与未来破局之路
开发语言·人工智能·自动化·视觉检测·机器视觉
铁蛋AI编程实战18 分钟前
通义千问 3.5 Turbo GGUF 量化版本地部署教程:4G 显存即可运行,数据永不泄露
java·人工智能·python
HyperAI超神经23 分钟前
在线教程|DeepSeek-OCR 2公式/表格解析同步改善,以低视觉token成本实现近4%的性能跃迁
开发语言·人工智能·深度学习·神经网络·机器学习·ocr·创业创新
jiang_changsheng30 分钟前
RTX 2080 Ti魔改22GB显卡的最优解ComfyUI教程
python·comfyui
R_.L33 分钟前
【QT】常用控件(按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
开发语言·qt
Zach_yuan42 分钟前
自定义协议:实现网络计算器
linux·服务器·开发语言·网络
云姜.1 小时前
java多态
java·开发语言·c++
CoderCodingNo1 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法