首页 > 解决方案 > webbrowser.open() 在输入后面添加 /

问题描述

我正在尝试使用语音识别搜索某些内容,但该命令被解释为 URL,它将在命令之后添加 /。斜线使单词成为 URL。我不想要一个 URL,而只是搜索栏上的一个普通单词。

例如:我:“搜索动物”

结果 - 动物/(无法访问站点)

有没有办法解决这个问题?

代码如下:

elif 'google' in command or 'search' in command:
        talk("Googling.")
        command = command.replace('google','') 
        command = command.replace('search','') 
        chrome_path = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
        webbrowser.register('edge', None , webbrowser.BackgroundBrowser(chrome_path))
        webbrowser.get('edge').open_new_tab(command)

标签: python-3.xpython-webbrowser

解决方案


webbrowser.open_new_tab()需要一个 URL。

添加要查找查询数据的搜索引擎(命令)。


webbrowser.get('edge').open_new_tab('https://google.com/search?q={}'.format(command))

webbrowser 模块启动您的浏览器并在其中打开指定的网页。


推荐阅读