6 回答

TA貢獻1864條經(jīng)驗 獲得超2個贊
現(xiàn)在已經(jīng)100%修復(fù)了:
https://github.com/nficano/pytube/pull/767#issuecomment-716184994
如果其他人遇到此錯誤或問題,請在終端或 cmd 中運行此命令: python -m pip install git+https://github.com/nficano/pytube
尚未隨 pip 安裝一起發(fā)布的 pytubeX 更新。GitHub 鏈接是當前開發(fā)人員對情況的解釋。

TA貢獻1847條經(jīng)驗 獲得超11個贊
我也遇到了同樣的麻煩,但我保證最上面的答案不能解決任何問題,只是隱藏問題,直到它再次出現(xiàn)。我調(diào)查了“extract.py”文件的范圍,發(fā)現(xiàn)了一個錯誤。該范圍通過字典搜索在視頻所在的 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
錯誤:
base_js = get_ytplayer_config(html)["assets"]["js"]
KeyError: 'assets'
之所以給出這個源代碼片段,是因為該源代碼片段不支持字典搜索,因此出現(xiàn) 'KeyError' 鍵錯誤,因為 'assets' 不是有效鍵,并且源代碼不是字典。所以我做了這個腳本,我相信它取代了原來的腳本,但在我的腳本中,特別是出現(xiàn)了其他錯誤。
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
上面的腳本搜索函數(shù)想要的字符串形式,而不是字典形式。
我希望我為未來更完整的解決方案做出了貢獻:)

TA貢獻1852條經(jīng)驗 獲得超7個贊
將此函數(shù)添加到 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"
)
并將 extract.py 中的函數(shù)“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
到:
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

TA貢獻2036條經(jīng)驗 獲得超8個贊
我遇到了同樣的問題,更新 pytube
到當前可用的最新版本問題消失了。
pip install pytube==10.0.0
或者
pip install --upgrade pytube

TA貢獻1848條經(jīng)驗 獲得超10個贊
如果您正在使用該軟件包pytube
或pytube3
,我建議您卸載它并安裝pytubeX
。無需更改導(dǎo)入。我發(fā)現(xiàn)它的工作更加可靠。
編輯:從評論中,如果這些都不起作用,請嘗試pytube4
編輯:pytube
現(xiàn)在再次維護!
添加回答
舉報