无法在python中从youtube下载多个视频

无法在python中从youtube下载多个视频,youtube,google-cloud-storage,youtube-data-api,pytube,Youtube,Google Cloud Storage,Youtube Data Api,Pytube,我正在做一个视频分析项目,需要从youtube下载视频并上传到谷歌云存储。我想不出直接上传到地面军事系统的方法,所以我试着在本地机器上下载,然后上传到地面军事系统 我阅读了多篇关于stackoverflow的文章,在这些文章的帮助下,我想出了以下脚本 import urllib.request import json from pytube import YouTube import pickle def get_all_video_in_channel(channel_id): ap

我正在做一个视频分析项目,需要从youtube下载视频并上传到谷歌云存储。我想不出直接上传到地面军事系统的方法,所以我试着在本地机器上下载,然后上传到地面军事系统

我阅读了多篇关于stackoverflow的文章,在这些文章的帮助下,我想出了以下脚本

import urllib.request
import json
from pytube import YouTube
import pickle

def get_all_video_in_channel(channel_id):
    api_key = 'AIzaSyCK9eQlD1ptx0SKMsmL0srmL2ua9_EuwSs'

    base_video_url = 'https://www.youtube.com/watch?v='
    base_search_url = 'https://www.googleapis.com/youtube/v3/search?'

    first_url = base_search_url+'key={}&channelId={}&part=snippet,id&order=date&maxResults=25'.format(api_key, channel_id)

    video_links = []
    url = first_url
    while True:
        inp = urllib.request.urlopen(url)
        resp = json.load(inp)

        for i in resp['items']:
            if i['id']['kind'] == "youtube#video":
                video_links.append(base_video_url + i['id']['videoId'])

        try:
            next_page_token = resp['nextPageToken']
            url = first_url + '&pageToken={}'.format(next_page_token)
        except:
            break
    return video_links


#Load the file containing all the youtube video url
load_url = get_all_video_in_channel(channel_id)

#Access all the youtube url in the list and store them on local machine. Need to figure out if there is a way to directly upload them to gcs
for i in range(0,len(load_url)):
    YouTube('load_url[i]').streams.first().download('C:/Users/Tushar/Documents/Serato_Video_Intelligence/youtube_videos')
我浏览了关于stackoverflow的多篇文章,其中包括 及

在他们的帮助下,我想出了下面的剧本

import urllib.request
import json
from pytube import YouTube
import pickle

def get_all_video_in_channel(channel_id):
    api_key = 'AIzaSyCK9eQlD1ptx0SKMsmL0srmL2ua9_EuwSs'

    base_video_url = 'https://www.youtube.com/watch?v='
    base_search_url = 'https://www.googleapis.com/youtube/v3/search?'

    first_url = base_search_url+'key={}&channelId={}&part=snippet,id&order=date&maxResults=25'.format(api_key, channel_id)

    video_links = []
    url = first_url
    while True:
        inp = urllib.request.urlopen(url)
        resp = json.load(inp)

        for i in resp['items']:
            if i['id']['kind'] == "youtube#video":
                video_links.append(base_video_url + i['id']['videoId'])

        try:
            next_page_token = resp['nextPageToken']
            url = first_url + '&pageToken={}'.format(next_page_token)
        except:
            break
    return video_links


#Load the file containing all the youtube video url
load_url = get_all_video_in_channel(channel_id)

#Access all the youtube url in the list and store them on local machine. Need to figure out if there is a way to directly upload them to gcs
for i in range(0,len(load_url)):
    YouTube('load_url[i]').streams.first().download('C:/Users/Tushar/Documents/Serato_Video_Intelligence/youtube_videos')
它只适用于前两个视频URL,然后由于以下错误而失败

Traceback (most recent call last):
 File "<stdin>", line 2, in <module>
 File "C:\Python37\lib\site-packages\pytube\streams.py", line 217, in download
   bytes_remaining = self.filesize
 File "C:\Python37\lib\site-packages\pytube\streams.py", line 164, in filesize
   headers = request.get(self.url, headers=True)
 File "C:\Python37\lib\site-packages\pytube\request.py", line 21, in get
   response = urlopen(url)
 File "C:\Python37\lib\urllib\request.py", line 222, in urlopen
   return opener.open(url, data, timeout)
 File "C:\Python37\lib\urllib\request.py", line 531, in open
   response = meth(req, response)
 File "C:\Python37\lib\urllib\request.py", line 641, in http_response
   'http', request, response, code, msg, hdrs)
 File "C:\Python37\lib\urllib\request.py", line 569, in error
   return self._call_chain(*args)
 File "C:\Python37\lib\urllib\request.py", line 503, in _call_chain
   result = func(*args)
 File "C:\Python37\lib\urllib\request.py", line 649, in http_error_default
   raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
下载文件“C:\Python37\lib\site packages\pytube\streams.py”,第217行
剩余字节数=self.filesize
文件“C:\Python37\lib\site packages\pytube\streams.py”,第164行,文件大小
headers=request.get(self.url,headers=True)
文件“C:\Python37\lib\site packages\pytube\request.py”,第21行,在get中
响应=urlopen(url)
文件“C:\Python37\lib\urllib\request.py”,第222行,在urlopen中
返回opener.open(url、数据、超时)
打开文件“C:\Python37\lib\urllib\request.py”,第531行
响应=方法(请求,响应)
文件“C:\Python37\lib\urllib\request.py”,第641行,在http\U响应中
“http”、请求、响应、代码、消息、hdrs)
文件“C:\Python37\lib\urllib\request.py”第569行出错
返回自我。调用链(*args)
文件“C:\Python37\lib\urllib\request.py”,第503行,在调用链中
结果=func(*args)
文件“C:\Python37\lib\urllib\request.py”,第649行,默认为http\u error\u
raise HTTPError(请求完整的url、代码、消息、hdrs、fp)
urllib.error.HTTPError:HTTP错误403:禁止
我希望如果有人能帮助我理解这里出了什么问题,如果有人能帮助我解决这个问题。我非常需要这个,一段时间以来一直无法解决这个问题

提前多谢


注意:如果可能的话,有没有办法直接上传到地面军事系统。

似乎您可能会与YouTube的服务条款发生冲突,我建议您查看此文档,并注意第5节的字母B。[1]


[1] 似乎您可能会与YouTube的服务条款发生冲突,我建议您查看此文档,并注意第5节字母B。[1]

[1]