爬虫——有道云翻译

废话不多说直接上代码

固定文本内容

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()
相关推荐
L-影几秒前
FastAPI全解析(下):除了快,它还能干多少脏活累活?
python·fastapi
qq_372154231 分钟前
如何利用Bootstrap的Flex工具类快速排版
jvm·数据库·python
qq_654366982 分钟前
golang如何实现菜单权限动态加载_golang菜单权限动态加载实现详解
jvm·数据库·python
寒秋花开曾相惜5 分钟前
(学习笔记)4.1 Y86-64指令集体系结构(4.1.4 Y86-64异常&4.1.5 Y86-64程序)
开发语言·jvm·数据结构·笔记·学习
Rick199342 分钟前
Spring AI 如何进行权限控制
人工智能·python·spring
码界筑梦坊42 分钟前
302-基于Python的安卓应用市场数据可视化分析推荐系统
开发语言·python·信息可视化·毕业设计·fastapi
齐鲁大虾1 小时前
新人编程语言选择指南
javascript·c++·python·c#
LiLiYuan.1 小时前
【Java 6种线程状态】
java·开发语言
Absurd5871 小时前
Redis如何限制列表最大长度_利用LTRIM指令截断List保留最新记录
jvm·数据库·python
2401_882273721 小时前
SQL函数面试题解析_函数性能与设计考点
jvm·数据库·python