You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py机器人获取Literature Clock内容遇问题,求技术支持

解决方案

1. 修复Selenium驱动初始化错误

Selenium 4.x版本已移除executable_path参数,需改用FirefoxService类指定驱动路径:

  • 导入from selenium.webdriver.firefox.service import Service as FirefoxService
  • 用FirefoxService封装驱动路径,通过service参数传入webdriver.Firefox

2. 解决字段为空问题

页面内容动态加载,隐式等待无法确保元素完全渲染,改用显式等待精准等待目标元素出现;同时优化元素定位逻辑:

  • 导入from selenium.webdriver.support.ui import WebDriverWait和from selenium.webdriver.support import expected_conditions as EC
  • 对每个目标元素设置显式等待,直到元素可见后再获取文本;时间标签em嵌套在引用元素内,可直接从引用元素中定位,提升准确性

修正后的完整代码

import discord
from discord.ext import commands
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

intents = discord.Intents.default()
intents.message_content = True
intents.members = True
client = commands.Bot(command_prefix='/', intents=intents)

@client.event
async def on_ready():
    # 同步斜杠命令(首次运行或修改命令后需要执行)
    await client.tree.sync()
    print(f'Logged in as {client.user}')

@client.tree.command(name="literature-clock", description="Get a book quote for the current time.")
async def literature_clock(ctx):
    firefox_options = FirefoxOptions()
    firefox_options.add_argument("--headless") 
    # 使用FirefoxService指定驱动路径
    service = FirefoxService(executable_path=r"C:\Users\shadowshard\OneDrive\Documents\geckodriver.exe")
    driver = webdriver.Firefox(service=service, options=firefox_options)

    try:
        driver.get("https://literature-clock.jenevoldsen.com/")
        # 显式等待页面核心元素加载完成
        wait = WebDriverWait(driver, 15)
        
        # 等待引用文本元素可见并获取内容
        quote_element = wait.until(EC.visibility_of_element_located((By.ID, "lit_quote")))
        quote_text = quote_element.text.strip()
        
        # 获取时间文本(位于lit_quote内的em标签)
        time_element = quote_element.find_element(By.TAG_NAME, "em")
        time = time_element.text.strip()
        
        # 等待书籍和作者元素可见
        book_element = wait.until(EC.visibility_of_element_located((By.ID, "book")))
        book = book_element.text.strip()
        
        author_element = wait.until(EC.visibility_of_element_located((By.ID, "author")))
        author = author_element.text.strip()
        
        response_text = f"📖 Quote: {quote_text}\n⏰ Time: {time}\n📚 Book: {book}\n✍️ Author: {author}"
    except Exception as e:
        response_text = f"Error: {str(e)}"
    finally:
        driver.quit()

    await ctx.response.send_message(response_text)

# 替换为你的机器人Token
client.run("YOUR_BOT_TOKEN")

额外说明

  • 首次运行需同步斜杠命令,代码中已通过on_ready事件实现
  • 确保geckodriver版本与Firefox浏览器版本匹配,避免兼容性问题

内容的提问来源于stack exchange,提问作者Shadowshard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 09:50:57