【发布时间】:2022-01-09 13:57:06
【问题描述】:
我对编程很陌生,我正在使用 Python 开发语音助手。我在 Github 上找到了这段代码,但他没有按应有的方式工作。这是代码:
def Play(speech):
if speech.endswith("on YouTube"):
searchTerm = speech.split()
response = get("https://www.youtube.com/results?search_query=" + quote(" ".join(searchTerm[:-2])))
soup = BeautifulSoup(response.text, "html.parser")
videos = soup.findAll(attrs={"class":"yt-uix-tile-link"})[1:4]
#Was [:3], changed to [1:4] to try to stop ads
#Try to remove google ads if possible (May have fixed, but test this)
names = list()
links = list()
for i in range(len(videos)):
names.insert(i, videos[i]["title"])
links.insert(i, "https://www.youtube.com" + videos[i]["href"])
print("I found 3 videos. " + ". ".join(names), links)
在 get() 方法中作为参数传递的 URL 正常工作,soup 变量也可以正常工作,但“视频”中没有任何内容,因此最后没有打印任何内容,我不知道如何解决这个问题。
请给一些想法:) ?
【问题讨论】:
标签: python beautifulsoup response speech-recognition