新手求救 python大數據特訓班第二版-8 |
|
張
更新時間:2021/11/17 上午 11:19:09 |
|
在本書第8章最後的caption下載字幕檔, from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=RIIU6rRj7Eo') #print(yt.captions) caption = yt.captions['en'] srt = caption.generate_srt_captions() file = open('download/youtube.srt', 'w', encoding='UTF-8') file.write(srt) file.close() print(srt)
按照書上做,但無法下載 KeyError Traceback (most recent call last) <ipython-input-23-52accba3103f> in <module> 3 yt = YouTube('https://www.youtube.com/watch?v=RIIU6rRj7Eo') 4 #print(yt.captions) ----> 5 caption = yt.captions['en'] 6 srt = caption.generate_srt_captions() 7 file = open('d:\\tem55/download/youtube.srt', 'w', encoding='UTF-8')
C:\ProgramData\Anaconda3\lib\site-packages\pytube\query.py in __getitem__(self, i) 410 411 def __getitem__(self, i: str): --> 412 return self.lang_code_index[i] 413 414 def __len__(self) -> int:
KeyError: 'en'
是字型出問題嗎 不好意思麻煩幫幫我 |
|
|
|
文淵閣工作室
更新時間:2021/11/17 下午 07:58:03 |
|
<youtube_caption.py>程式第7列改為「caption = yt.captions['a.en']」。 修改Pytube原始檔<C:\ProgramData\Anaconda3\Lib\site-packages\pytube\captions.py>: 76列改為「for i, child in enumerate(list(root.findall('body/p'))):」 80列改為「duration = float(child.attrib["d"])」 83列改為「start = float(child.attrib["t"])」 77列改為: text = ''.join(child.itertext()).strip() if not text: continue |
|
|
|
張
更新時間:2021/11/18 上午 10:16:20 |
|
不好意思,我有照樣修改了 但還是有錯誤 KeyError Traceback (most recent call last) <ipython-input-2-9359b035f184> in <module> 4 #print(yt.captions) 5 caption = yt.captions['a.en'] ----> 6 srt = caption.generate_srt_captions() 7 file = open('d:\\tem55/download/youtube.srt', 'w', encoding='UTF-8') 8 file.write(srt)
C:\ProgramData\Anaconda3\lib\site-packages\pytube\captions.py in generate_srt_captions(self) 49 recompiles them into the "SubRip Subtitle" format. 50 """ ---> 51 return self.xml_caption_to_srt(self.xml_captions) 52 53 @staticmethod
C:\ProgramData\Anaconda3\lib\site-packages\pytube\captions.py in xml_caption_to_srt(self, xml_captions) 81 try: 82 duration = float(child.attrib["d"]) ---> 83 except KeyError: 84 duration = 0.0 85 start = float(child.attrib["t"])
KeyError: 'start'
還有這行 77列改為: text = ''.join(child.itertext()).strip() 是不是要多加一個" |
|
|
|
文淵閣工作室
更新時間:2021/11/18 下午 09:08:02 |
|
請確認是安裝 pytube (不是 pytube3) 並更新到最新版 11.0.1。 貼上修正後的程式碼: 76 for i, child in enumerate(list(root.findall('body/p'))): 77 text = ''.join(child.itertext()).strip() 78 if not text: 79 continue 80 caption = unescape(text.replace("\n", " ").replace(" ", " "),) 81 try: 82 duration = float(child.attrib["d"]) 83 except KeyError: 84 duration = 0.0 85 start = float(child.attrib["t"]) |
|
|
|
張
更新時間:2021/11/24 上午 09:16:59 |
|
確認完畢後,請問這是存取位置的問題嗎? PermissionError Traceback (most recent call last) <ipython-input-5-d6106e5be162> in <module> 5 caption = yt.captions['a.en'] 6 srt = caption.generate_srt_captions() ----> 7 file = open('d:\\tem55/', 'w', encoding='UTF-8') 8 file.write(srt) 9 file.close()
PermissionError: [Errno 13] Permission denied: 'd:\\tem55/' |
|
|
|
文淵閣工作室
更新時間:2021/11/24 下午 04:04:59 |
|
請修改第7列如下: file = open('d:/tem55/youtube.srt', 'w', encoding='UTF-8') 必須在 D 磁碟先建立 tem55 資料夾,並明確指定儲存的檔名,本例為<youtube.srt> |
|
|
|
蔡
更新時間:2022/9/29 上午 12:30:21 |
|
在本書第8章最後的caption下載字幕檔, from pytube import YouTube Traceback (most recent call last):
File "C:\Users\magiceason\Desktop\Python程式設計\Python大數據分析\CH08 實戰:YouTube影片資源下載\untitled1.py", line 6, in <module> srt = caption.generate_srt_captions()
File "C:\ProgramData\Anaconda3\lib\site-packages\pytube\captions.py", line 51, in generate_srt_captions return self.xml_caption_to_srt(self.xml_captions)
File "C:\ProgramData\Anaconda3\lib\site-packages\pytube\captions.py", line 83, in xml_caption_to_srt start = float(child.attrib["start"])
KeyError: 'start' |
|
|
|
|
|
|