如何在不使用 python 或 R 中打开浏览器的情况下截取 youtube 页面? [英] how to take screenshot of youtube page without opening the browser in python or R?

查看:59
本文介绍了如何在不使用 python 或 R 中打开浏览器的情况下截取 youtube 页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想截取属于 youtube 的几个链接.我想截取将播放视频的网页的屏幕截图,然后截取屏幕截图或在不播放的情况下截取屏幕截图,但这一切都必须使用 python 或 R 进行,但它们不应打开浏览器.这一切都应该发生在后端.

I want to take screenshot of several links belonging to youtube. I want to take screenshot of the webpage where it will play the video and then take screenshot or take the screenshot without playing but all this has to happen using python or R but they shouldn't open the browser. it should all happen in the backend.

对此的任何帮助都会很棒.

Any help on this will be great.

提前致谢.

我尝试打开链接并使用 R 和 python 截取屏幕截图,它确实在不打开浏览器的情况下截取了屏幕截图.但是视频截图是黑色的,有一个我不想要的错误.

I have tried opening the link and taking the screenshot using both R and python and it does take the screenshot without opening browser. But the video screenshot is black with an error which I don't want.

R 中的代码图书馆(网络截图)webshot("https://www.youtube.com/watch?v=Nym5stAJAt8","test.png")

code in R library(webshot) webshot("https://www.youtube.com/watch?v=Nym5stAJAt8","test.png")

python 中的代码从硒导入网络驱动程序驱动程序 = webdriver.PhantomJS()driver.set_window_size(1120, 550)driver.get("https://www.youtube.com/watch?v=Nym5stAJAt8")driver.save_screenshot("screenshot.png")

code in python from selenium import webdriver driver=webdriver.PhantomJS() driver.set_window_size(1120, 550) driver.get("https://www.youtube.com/watch?v=Nym5stAJAt8") driver.save_screenshot("screenshot.png")

我得到的输出我想要的输出

推荐答案

您可以使用 chrome 的无头模式.在调用 chrome 驱动程序之前,创建选项的选项对象,然后作为参数发送给驱动程序.然后你可以在后端运行这个代码,即使是一些没有任何屏幕的环境,包括 docker 容器.但要小心设置窗口大小.这会影响您的屏幕截图结果.

You can use headless mode of chrome. Before call driver of chrome, create option object of option then send to driver as argument. Then you can run this code in backend even some environment without any screen include docker container. But be careful to set window size. This will affect your screenshot result.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options() #
chrome_options.add_argument('--headless')
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get_screenshot_as_file("capture.png")

这篇关于如何在不使用 python 或 R 中打开浏览器的情况下截取 youtube 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆