我正在尝试使用Qwebengineview查看youtube视频列表,但浏览器无法自动播放视频,我正在使用PyQt5 5.13.1 Python 3.6
这是示例代码
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
from PyQt5.QtWidgets import QApplication
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
webview = QWebEngineView()
profile = QWebEngineProfile("my_profile", webview)
profile.defaultProfile().setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
webpage = QWebEnginePage(profile, webview)
webview.setPage(webpage)
webview.load(QUrl("https://www.youtube.com/watch?v=VzIVI2fsRb0"))
webview.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
我已经找到了使用QWebEngineSettings的解决方案,这是一个完整的工作示例,以防有人需要
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage, QWebEngineSettings
from PyQt5.QtWidgets import QApplication
import time
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
webview = QWebEngineView()
profile = QWebEngineProfile("my_profile", webview)
profile.defaultProfile().setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
webpage = QWebEnginePage(profile, webview)
webpage.settings().setAttribute(QWebEngineSettings.PlaybackRequiresUserGesture, False)
webview.setPage(webpage)
webview.load(QUrl("https://www.youtube.com/watch?v=aKCNrkERJ3E"))
webview.show()
sys.exit(app.exec_())