the*_*boy 5 python python-3.x pytube python-3.8
尝试运行我的小测试脚本时,Pytube 十分之五会向我发送此错误。
这是脚本:
import pytube
import urllib.request
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=3NCyD3XoJgM')
print('Youtube video title is: ' + yt.title + '! Downloading now!')
Run Code Online (Sandbox Code Playgroud)
这是我得到的:
Traceback (most recent call last):
File "youtube.py", line 6, in <module>
yt = YouTube('https://www.youtube.com/watch?v=3NCyD3XoJgM')
File "C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pytube\__main__.py", line 91, in __init__
self.prefetch()
File "C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pytube\__main__.py", line 183, in prefetch
self.js_url = extract.js_url(self.watch_html)
File "C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pytube\extract.py", line 143, in js_url
base_js = get_ytplayer_config(html)["assets"]["js"]
KeyError: 'assets'
Run Code Online (Sandbox Code Playgroud)
我很困扰。我尝试重新安装 Python 和 pytube,但似乎无法解决此问题。越来越令人困惑的是,该脚本有一半时间有效,而另一半时间无效。
the*_*boy 10
现在用这个固定 100%:
https://github.com/nficano/pytube/pull/767#issuecomment-716184994
如果其他人遇到此错误或问题,请在终端或 cmd 中运行此命令:
python -m pip install git+https://github.com/nficano/pytube
尚未随 pip 安装发布的 pytubeX 更新。GitHub 链接是解释情况的当前开发人员。
小智 5
我遇到了同样的麻烦,但我保证最重要的答案不能解决任何问题,只是隐藏问题直到它再次弹出。我调查了“extract.py”文件的这个范围,发现了一个错误。该范围在视频所在的 Youtube 页面的源代码中搜索“字符串”片段,通过字典搜索,例如:
#Example ---------------
Vars = {
'name':'luis'
'age':'27'
}
print(Vars['name'])
result: 'luis'
#Extract.py Code -------
def js_url(html: str) -> str:
"""Get the base JavaScript url.
Construct the base JavaScript url, which contains
the decipher
"transforms".
:param str html:
The html contents of the watch page.
"""
base_js = get_ytplayer_config(html)["assets"]["js"]
return "https://youtube.com" + base_js
Run Code Online (Sandbox Code Playgroud)
错误:
base_js = get_ytplayer_config(html)["assets"]["js"]
KeyError: 'assets'
Run Code Online (Sandbox Code Playgroud)
给出它是因为这个源代码片段不支持搜索为 dicionario,所以 'KeyError' 键错误,因为 'assets' 不是一个有效的键,并且源代码不是字典。所以我做了这个脚本,我相信它取代了这个原始脚本,但在我的中,特别是出现了其他错误。
def js_url(html: str) -> str:
"""Get the base JavaScript url.
Construct the base JavaScript url, which contains
the decipher
"transforms".
:param str html:
The html contents of the watch page.
"""
base_js = html[html.find('js') + 4:html.find('.js')
+ 4]
return "https://youtube.com" + base_js
Run Code Online (Sandbox Code Playgroud)
上面的脚本搜索函数想要的字符串,而不是字典。
我希望我为更完整的未来解决方案做出了贡献:)
小智 5
将此函数添加到extract.py
def get_ytplayer_js(html: str) -> Any:
"""Get the YouTube player base JavaScript path.
:param str html
The html contents of the watch page.
:rtype: str
:returns:
Path to YouTube's base.js file.
"""
js_url_patterns = [
r"\"jsUrl\":\"([^\"]*)\"",
]
for pattern in js_url_patterns:
regex = re.compile(pattern)
function_match = regex.search(html)
if function_match:
logger.debug("finished regex search, matched: %s", pattern)
yt_player_js = function_match.group(1)
return yt_player_js
raise RegexMatchError(
caller="get_ytplayer_js", pattern="js_url_patterns"
)
Run Code Online (Sandbox Code Playgroud)
并将extract.py中的函数“js_url”更改为:
def js_url(html: str) -> str:
"""Get the base JavaScript url.
Construct the base JavaScript url, which contains the decipher
"transforms".
:param str html:
The html contents of the watch page.
"""
base_js = get_ytplayer_config(html)["assets"]["js"]
return "https://youtube.com" + base_js
Run Code Online (Sandbox Code Playgroud)
到:
def js_url(html: str) -> str:
"""Get the base JavaScript url.
Construct the base JavaScript url, which contains the decipher
"transforms".
:param str html:
The html contents of the watch page.
"""
base_js = get_ytplayer_js(html)
return "https://youtube.com" + base_js
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4768 次 |
| 最近记录: |