python-3.x - 如何使用 selenium python 从 youtube 获取视频链接

标签 python-3.x selenium web-scraping youtube selenium-chromedriver

我正在尝试制作一个可以在 youtube 上获取视频链接的程序,这是我的代码

video=Song_name
chromeOptions=Options()
chromeOptions.add_experimental_option("prefs{'download.default_directory':down_pth}")
driver=webdriver.Chrome(path+"/chromedriver.exe",options=chromeOptions)
wait=WebDriverWait(driver,3)
presence = EC.presence_of_element_located   
visible = EC.visibility_of_element_located
driver.get("https://www.youtube.com/results?search_query=" + str(video))
wait.until(visible((By.ID, "video-title")))
driver.find_element_by_id("video-title").click()
print(driver.current_url)
url=driver.current_url
print(url)
此代码仅在 youtube 上的视频出现在列表中的第一个位置时才有效,否则如果某些广告出现在视频的(顶部)之前,它将失败。
如何避免广告并直接播放视频?
Here is the image in which ads come on top of the video and I am not able to open video
如何解决这个问题?

最佳答案

我会导航到视频 URL 页面并使用 .SendKeys()发送F6键,它将完整地突出显示 URL。然后我会发送Ctrl + C复制,你应该好好去!

关于python-3.x - 如何使用 selenium python 从 youtube 获取视频链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62800038/

相关文章:

python - 计算任何数字的最大素因数

python - 如何解决 PyTest 中的陈旧元素异常

r - 使用 RVest 创建 HTML 表,然后使用操作和清理到 DF

Python - 将文本结果抓取到列表中

python - 如何在 Outlook (2010) 全局地址列表中搜索多个姓名?

python - 如何获得 pickle 中的物体数量?

python-3.x - 如何使用python调试器和Visual Studio Code调试Behave BDD场景

python - 如何避免每次使用 Selenium 和 Python 时都出现 Instagram 登录页面

java - 更新到 Selenium 3.4 时出现 ClassCastException

python - 为什么 lxml 找不到这个类?