爬虫——有道云翻译

废话不多说直接上代码

固定文本内容

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()
相关推荐
codists8 分钟前
《AI-Assisted Programming》读后感
python
爱欲无极27 分钟前
基于Flask的微博话题多标签情感分析系统设计
后端·python·flask
F_D_Z39 分钟前
conda issue
python·github·conda·issue
大阳1231 小时前
数据结构2.(双向链表,循环链表及内核链表)
c语言·开发语言·数据结构·学习·算法·链表·嵌入式
Wangsk1331 小时前
用 Python 批量处理 Excel:从重复值清洗到数据可视化
python·信息可视化·excel·pandas
ChipCamp1 小时前
Chisel芯片开发入门系列 -- 18. CPU芯片开发和解释8(流水线架构的代码级理解)
开发语言·青少年编程·fpga开发·scala·dsp开发·risc-v·chisel
越来越无动于衷1 小时前
智慧社区(八)——社区人脸识别出入管理系统设计与实现
java·开发语言·spring boot·python·mysql
正义的大古2 小时前
OpenLayers 详细开发指南 - 第八部分 - GeoJSON 转换与处理工具
开发语言·前端·javascript
沐知全栈开发2 小时前
Eclipse 代码模板
开发语言
一顿操作猛如虎,啥也不是!2 小时前
c# 在 23:00 - 23:59 之间执行一次的写法
开发语言·c#