平常我在facebook, Instagram, Youtube都有下載, 但我的經驗就在外面使用Youtube的載點,很容易載到附帶的網頁自動植入瀏覽器廣告甚至不知名程式,最近無意間找到這個python工具pytube3就試著用用看:
C:\Users\USER\Desktop>pip3 install pytube3
以下是Download 訊息
Collecting pytube3
Downloading https://files.pythonhosted.org/packages/de/86/198092763646eac7abd2063192ab44ea44ad8fd6d6f3ad8586b38afcd52a/pytube3-9.6.4-py3-none-any.whl
Collecting typing-extensions
Downloading https://files.pythonhosted.org/packages/60/7a/e881b5abb54db0e6e671ab088d079c57ce54e8a01a3ca443f561ccadb37e/typing_extensions-3.7.4.3-py3-none-any.whl
Installing collected packages: typing-extensions, pytube3
Successfully installed pytube3-9.6.4 typing-extensions-3.7.4.3
安裝完成囉
先是興匆匆的照https://pypi.org/project/pytube3/
run這支code:Pytube.py
from pytube import YouTube
YouTube('https://youtu.be/9bZkp7q19f0').streams.get_highest_resolution().download()
結果發生以下錯誤:
KeyError Traceback (most recent call last)
.
.
.
KeyError: 'url'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
.
.
.
KeyError: 'cipher'
後來有找到相對應除錯解決方案,如:https://stackoverflow.com/questions/61960657/getting-keyerror-url-with-pytube
I have just fixed this issue. Follow these steps.
Go to pytube directory in site-packages. If you're not sure where full location is use: pip show pytube3. You'll see location of site-packages.
In site-packages/pytube folder opened from above location, you'll see a file called extract.py. Open that file in your IDE or text-editor.
You'll see a function called apply_descrambler . Inside that function, in line 301 probably, you'll see something like parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
Replace cipher in formats[i]["cipher"] with signatureCipher. Thus that line becomes, parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
pytube3 should work fine now.
意思就是指,安裝完pytube3後,例如在 c:\python37\lib\site-packages下,找到extract.py這個檔案找到這行大概在第301行左右:
parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
置換成這樣後
parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
程式就可正常運作。