【问题标题】:How can I get the code to embed a YouTube video from the HTMLusing Python 3.6如何使用 Python 3.6 从 HTML 中获取嵌入 YouTube 视频的代码
【发布时间】:2017-09-29 18:42:30
【问题描述】:

我的目标是为在输入中搜索的视频创建一个文件下面的所有代码都有效,但是我找不到如何搜索 html 代码或将其写入 html 文件)。最终目标是让 YouTube discord 机器人之类的东西在本地工作。我需要提取用于嵌入视频的代码,而无需手动右键单击视频。 我会非常感谢那些提供帮助的人:)

import bs4 as bs

import urllib.request

import os

basic = 'https://www.youtube.com/'

search = ''

x = str(input('Song name: '))

y = ''

i = 0

x = x.split(' ')

y = x[0]

for i in range(1,len(x)):

    y = y + '+' + str(x[i])


searchQ = 'https://www.youtube.com/results?search_query='+y


sauce = urllib.request.urlopen(searchQ).read()
soup = bs.BeautifulSoup(sauce,'lxml')

nav = soup.nav

for url in soup.find_all('a'):
    z = (url.get('href'))
    i += 1
    if i > 10:
        if z[0] == '/' and z[1] == 'w':
            search = basic+z
            print(search)
            break
sauce = urllib.request.urlopen(search).read()
soup = bs.BeautifulSoup(sauce,'lxml')
page = urllib.request.urlopen(search)
html = page.read()
soup = bs.BeautifulSoup(html,'lxml')

os.remove('my vid.html')
f = open('my vid.html','w')
#f.write(str(html))
f.close


path = r'C:\Users\name\Desktop\my vid.html'
os.startfile(path)

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup urllib


    【解决方案1】:

    我使用 selenium 来解决这个问题。而且我还删除了 requests 库的所有用法。

    我的代码所做的是,它基本上进入任何 YouTube 视频并使用 selenium 复制嵌入代码并将代码输出到屏幕。

    然后您可以稍后将嵌入代码以您想要的格式写入任何文件。这取决于你。

    另外请注意,我没有触及与搜索视频或选择视频相关的任何内容。

    代码:

    import clipboard
    import bs4 as bs
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    basic = 'https://www.youtube.com/'
    search = ''
    x = str(input('Song name: '))
    y = ''
    i = 0
    x = x.split(' ')
    y = x[0]
    
    for i in range(1,len(x)):
        y = y + '+' + str(x[i])
    
    searchQ = 'https://www.youtube.com/results?search_query=' + y
    driver = webdriver.Chrome()
    driver.get(searchQ)
    driver.maximize_window()
    soup = bs.BeautifulSoup(driver.page_source, 'lxml')
    nav = soup.nav
    
    for url in soup.find_all('a'):
        z = (url.get('href'))
        i += 1
        if i > 10:
            if z[0] == '/' and z[1] == 'w':
                search = basic+z
                print(search)
                break
    
    driver.get(search)
    soup = bs.BeautifulSoup(driver.page_source,'lxml')
    
    actionChains = ActionChains(driver)
    video = driver.find_element_by_css_selector('#movie_player > div.html5-video-container > video')
    actionChains.context_click(video).perform()
    embed = driver.find_element_by_xpath('//*[@id="null"]/div[3]/div[1]').click()
    text = clipboard.paste()
    print(text)
    

    输入:

    fire squad
    

    输出:

    Song name: fire squad
    https://www.youtube.com//watch?v=-MGB_G_ZjMo
    <iframe width="854" height="480" src="https://www.youtube.com/embed/-MGB_G_ZjMo" frameborder="0" allowfullscreen></iframe>
    

    【讨论】:

    • 非常感谢您的帮助,但是在安装模块并运行代码后,我收到一个以 selenium.common.exceptions.WebDriverException 结尾的巨大错误:消息:'chromedriver' 可执行文件需要在 PATH 中。
    • 嗨 Luc,您需要从 link 下载 chromedriver.exe,然后您只需将可执行文件添加到您的项目目录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2013-04-08
    • 1970-01-01
    • 2012-08-13
    • 2019-04-28
    • 1970-01-01
    相关资源
    最近更新 更多