PyTube3 播放列表返回空列表 [英] PyTube3 Playlist returns empty list
问题描述
似乎在过去 2 或 3 周的某个时候,Playlist
类似乎对我停止工作了.我尝试了以下改编自 他们的 GitHub 的代码片段:
It seems that sometime in the past 2 or 3 weeks, the Playlist
class seems to have stopped working for me. I've tried the following code snippet adapted from their GitHub:
from pytube import Playlist
playlist = Playlist("https://www.youtube.com/playlist?list=PLynhp4cZEpTbRs_PYISQ8v_uwO0_mDg_X")
print(len(playlist.video_urls))
for url in playlist.video_urls:
print(url)
我尝试了多个公共播放列表,但它们都生成了一个空的 list
对象.这段代码大约在 3 周前有效.另外,我正在运行 Python 3.7.6 和最新版本的 PyTube3 (9.6.4).
I've tried multiple public playlists but they all produced an empty list
object. This code was working about 3 weeks ago. Also, I am running Python 3.7.6 and the latest version of PyTube3 (9.6.4).
是不是我做错了什么?
推荐答案
我查看了一下源代码,它似乎会加载 html,然后对 /watch/v=.. 执行正则表达式搜索..
网址.使用的正则表达式是 href=\"(/watch\?v=[\w-]*)
但它找不到任何匹配项,因为 YouTube 必须更新了他们的 html.他们现在以 JSON 对象的形式发送监视 URL.所以我们应该寻找那个.
I looked into the source a bit and it seemed that it would load the html and then perform a regex search for /watch/v=...
URLs. The regex expression used was href=\"(/watch\?v=[\w-]*)
but it couldn't find any matches since YouTube must have updated their html. They now send the watch URLs in a JSON object. So we should look for that instead.
这里有一些有用的东西:
Here is something that works:
from pytube import Playlist
import re
playlist = Playlist("https://www.youtube.com/playlist?list=PLynhp4cZEpTbRs_PYISQ8v_uwO0_mDg_X")
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
print(len(playlist.video_urls))
for url in playlist.video_urls:
print(url)
希望这在下一个补丁之前有用.
Hope this is useful until the next patch.
这篇关于PyTube3 播放列表返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!