【问题标题】:Get youtube video link in python在 python 中获取 youtube 视频链接
【发布时间】:2022-01-21 09:17:48
【问题描述】:

我需要确保我在控制台中输入了一个单词,并且应该为我显示视频的链接,例如,如果我指的是控制台中的单词 Linkin Park,它应该给我一个链接到此请求的第一个视频。在我的例子中,第一个链接是 Numb [Official Music Video] - Linkin Park

我该怎么做?

【问题讨论】:

标签: python search hyperlink youtube


【解决方案1】:

这将解决您的问题。

def search_video(self,query):
    search_query = query.split()

    url = "http://www.youtube.com/results?search_query="

    for word in search_query:
        url += word + "+"

    time.sleep(1)
    webbrowser.open_new(url[:-1])  
    #-1 is used here, because in the URL we have an added + at the end.

更新:您可以使用Selenium

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.implicitly_wait(5)

search_query = input().split()
print(search_query)

for word in search_query:
    final_query += word + "+"
    
driver.get('https://www.youtube.com/results?search_query={}'.format(final_query))
select = driver.find_element(By.CSS_SELECTOR, 'div#contents ytd-item-section-renderer>div#contents a#thumbnail')
link += [select.get_attribute('href')]
print(link)

【讨论】:

  • 谢谢,但这有点不同,它给了我一个视频列表的链接,我需要这个列表中第一个视频的链接。
  • 检查更新的代码@Ikrom
猜你喜欢
  • 2022-12-12
  • 1970-01-01
  • 2016-05-17
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多