是否可以在python中的语音识别模块上设置计时器

是否可以在python中的语音识别模块上设置计时器,python,selenium,speech-recognition,Python,Selenium,Speech Recognition,因此,我使用selenium和语音识别模块在python中为youtube创建了一个语音搜索脚本,它工作得很好,除了一件事,它听我的声音太长或等待我的输入太长,那么有没有办法在输入上设置计时器?这是我的密码: import speech_recognition as sr from selenium import webdriver r=sr.Recognizer() drive=webdriver.Chrome() def gotosite(): drive.get("

因此,我使用selenium和语音识别模块在python中为youtube创建了一个语音搜索脚本,它工作得很好,除了一件事,它听我的声音太长或等待我的输入太长,那么有没有办法在输入上设置计时器?这是我的密码:

import speech_recognition as sr
from selenium import webdriver

r=sr.Recognizer()
drive=webdriver.Chrome()



def gotosite():
    drive.get("https://youtube.com")
    with sr.Microphone() as source:
        print("speak:")
        audio = r.listen(source)
        text = r.recognize_google(audio)
        print("You said: {}".format(text))
    searchbox = drive.find_element_by_xpath(
        "/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div/div[1]/input")
    searchbox.send_keys(text)
    searchbutton = drive.find_element_by_xpath(
        "/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/button")
    searchbutton.click()

gotosite()
使用
audio=r.listen(源代码,超时=3)
代替
audio=r.listen(源代码)