爬虫——有道云翻译

废话不多说直接上代码

固定文本内容

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()
相关推荐
大菠萝学姐18 分钟前
基于Spring Boot和Vue的高校图书馆座位预约系统的设计与实现
java·vue.js·spring boot·后端·python·mysql·vue
Tomorrow'sThinker26 分钟前
[特殊字符] Python 批量生成词云:读取词频 Excel + 自定义背景 + Excel to.png 流程解析
python·excel
rit843249931 分钟前
MATLAB基于voronoi生成三维圆柱形
开发语言·人工智能·matlab
liulilittle38 分钟前
C/C++ inline-hook(x86)高级函数内联钩子
c语言·开发语言·汇编·c++·hook·底层·钩子
Amelio_Ming39 分钟前
C++开源项目—2048.cpp
linux·开发语言·c++
chilavert31843 分钟前
技术演进中的开发沉思-28 MFC系列:关于C++
开发语言·c++·mfc
witton1 小时前
C语言使用Protobuf进行网络通信
c语言·开发语言·游戏·c·模块化·protobuf·protobuf-c
浏览器API调用工程师_Taylor1 小时前
Look my eyes 都2025年了,你还不会将重复的事情自动化?
前端·javascript·爬虫
黄焖鸡能干四碗1 小时前
系统安全设计方案,软件系统安全设计方案
开发语言·数据库·安全·vue·系统安全
野指针121381 小时前
【使用Flask基于PaddleOCR3.0开发一个接口 调用时报错RuntimeError: std::exception】
python·flask