YouTube请求在dev服务器上运行良好,但从VM prod发送时被拒绝

2024-04-26 17:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码(来自views.py)用于从带有pytube库的视频下载YouTube mp3,在development server上运行良好:

import os

from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings

from pytube import YouTube

# Create your views here.

def index(request):
    url = 'https://www.youtube.com/watch?v=v2JsxXxf1MM'
    audio = get_audio(url)
    download(audio)
    return HttpResponse('Downloaded!!!')

def get_audio(url):
        yt =YouTube(url)   
        all_streams = yt.streams.all()
        audio_mp4_stream_itag= [stream.itag \
                                for stream in all_streams \
                                if stream.mime_type == 'audio/mp4'][0]
        audio = yt.streams.get_by_itag(audio_mp4_stream_itag)
        return audio

def download(audio):
        dst = os.path.join(settings.BASE_DIR, 'tester', 'audio')
        audio.download(dst, 'downloaded_audio')

然而,当我在我的数字海洋水滴(ubuntuvm)上运行这个项目时,YouTube阻止了这个请求。错误是:

Traceback (most recent call last): File "/root/testenv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner response = get_response(request) File "/root/testenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "/root/testenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/root/requests_test/requests_test/tester/views.py", line 14, in index audio = get_audio(url) File "/root/requests_test/requests_test/tester/views.py", line 22, in get_audio yt =YouTube(url, on_progress_callback=log) File "/root/testenv/lib/python3.5/site-packages/pytube/main.py", line 88, in init self.prefetch_init() File "/root/testenv/lib/python3.5/site-packages/pytube/main.py", line 96, in prefetch_init self.prefetch() File "/root/testenv/lib/python3.5/site-packages/pytube/main.py", line 160, in prefetch self.watch_html = request.get(url=self.watch_url) File "/root/testenv/lib/python3.5/site-packages/pytube/request.py", line 21, in get response = urlopen(url) File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python3.5/urllib/request.py", line 472, in open response = meth(req, response) File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python3.5/urllib/request.py", line 504, in error result = self._call_chain(*args) File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain result = func(*args) File "/usr/lib/python3.5/urllib/request.py", line 696, in http_error_302 return self.parent.open(new, timeout=req.timeout) File "/usr/lib/python3.5/urllib/request.py", line 472, in open response = meth(req, response) File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python3.5/urllib/request.py", line 510, in error return self._call_chain(*args) File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain result = func(*args) File "/usr/lib/python3.5/urllib/request.py", line 590, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 429: Too Many Requests

回答是:

This page appears when Google automatically detects requests coming from your computer network which appear to be in violation of the Terms of Service. The block will expire shortly after those requests stop. In the meantime, solving the above CAPTCHA will let you continue to use our services.

This traffic may have been sent by malicious software, a browser plug-in, or a script that sends automated requests. If you share your network connection, ask your administrator for help — a different computer using the same IP address may be responsible. Learn more

Sometimes you may be asked to solve the CAPTCHA if you are using advanced terms that robots are known to use, or sending requests very quickly.

但我并不是用我的droplet的IP请求轰炸YouTube——这只是一个请求,也是第一个请求发生的错误。你知道吗

我怎样才能解决这个问题?将“User-Agent”头添加到请求中是否有帮助(以及如何将其提供给pytube)?你知道吗


Tags: inpyselfurlgetresponserequestlib