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

Python个人助手:实现维基百科页面正确跳转的问题求助

维基百科页面跳转错误修复

你的问题出在两个核心点:字符串变量未正确注入URL,以及使用了错误的维基百科域名。具体修改方案如下:

  • 修复字符串格式化逻辑:你当前代码里的URL字符串中,{statement}只是普通文本,不会被实际变量值替换。需要用Python的f-string(推荐写法)或.format()方法完成变量注入。
  • 修正维基百科域名:正确的维基百科官方域名是wikipedia.org,而非wikipedia.com。

修改后的完整代码片段:

# Function that fetches data from wikipedia
if 'wikipedia' in statement:
    speak('Searching Wikipedia...')
    statement = statement.replace("wikipedia", "").strip()  # 去除前后多余空格
    # 用f-string格式化URL,同时替换空格为下划线适配维基百科URL规则
    webbrowser.open_new_tab(f"https://en.wikipedia.org/wiki/{statement.replace(' ', '_')}")
    results = wikipedia.summary(statement, sentences=3)
    speak("According to Wikipedia")
    speak_and_print(results)

额外优化说明:

  • 新增.strip()方法清除statement前后的空白字符,避免URL出现无效的开头/结尾空格。
  • 将statement中的空格替换为下划线_,这是维基百科URL的标准格式(也可保留空格,浏览器会自动转译为%20,但下划线更符合维基百科的常规URL样式)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 20:40:31