6 回答

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

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

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊
將此函數(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貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
看來(lái) Pytube 模塊已更新。
它適用于 pytube 包
即嘗試pip install pytube
卸載 pytube 變體

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

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