get-pytube / pytube3

A lightweight, dependency-free Python 3 library (and command-line utility) for downloading YouTube Videos.
https://pytube3.readthedocs.io
Other
180 stars 55 forks source link

HTTP Error 429: Too Many Requests #72

Open arslandogar opened 4 years ago

arslandogar commented 4 years ago

This piece of code

from pytube import YouTube yt = YouTube('https://www.youtube.com/watch?v=tPEE9ZwTmy0')

Throws this error:

Traceback (most recent call last): File "test.py", line 2, in yt = YouTube('https://www.youtube.com/watch?v=tPEE9ZwTmy0') File "/home/user/.local/lib/python3.6/site-packages/pytube/main.py", line 91, in init self.prefetch() File "/home/user/.local/lib/python3.6/site-packages/pytube/main.py", line 162, in prefetch self.watch_html = request.get(url=self.watch_url) File "/home/user/.local/lib/python3.6/site-packages/pytube/request.py", line 36, in get return _execute_request(url).read().decode("utf-8") File "/home/user/.local/lib/python3.6/site-packages/pytube/request.py", line 24, in _execute_request return urlopen(request) # nosec File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python3.6/urllib/request.py", line 532, in open response = meth(req, response) File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python3.6/urllib/request.py", line 564, in error result = self._call_chain(args) File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain result = func(args) File "/usr/lib/python3.6/urllib/request.py", line 756, in http_error_302 return self.parent.open(new, timeout=req.timeout) File "/usr/lib/python3.6/urllib/request.py", line 532, in open response = meth(req, response) File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python3.6/urllib/request.py", line 570, in error return self._call_chain(args) File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain result = func(args) File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 429: Too Many Requests

swiftyy-mage commented 4 years ago

I will investigate this, my apologies for the delay.

swiftyy-mage commented 4 years ago

I tested this on python 3.6, 3.7, 3.8 and I got no errors. Have you made any modifications and have you installed it the correct way?

arslandogar commented 4 years ago

No, I have not made any changes. Just following instructions in readme.md. Installed with this command pip3 install pytube3 --upgrade

Then in my code file I have from pytube import YouTube yt = YouTube('https://www.youtube.com/watch?v=tPEE9ZwTmy0')

Using Python 3.6.8 on Ubuntu 18.04

swiftyy-mage commented 4 years ago

Does it raise this error every time? Does it raise this error for every video? This error code seems strange, kind of specific.

arslandogar commented 4 years ago

The error occurs like 95% of the time and yes for every video. And on random rare occasions, it does work alright.

swiftyy-mage commented 4 years ago

I have used a new installation of Python 3.6.8 with Linux Mint 19.3 (which is based on Ubuntu 18.04), and have been totally unable to replicate this issue. I have an idea of how to get it working though, have you tried using a VPN to use this package? If not I would recommend just making a free account of Proton VPN and using their command line tool, then once you are connected try using this package again. If using a VPN still doesn't work, try installing youtube-dl and see if that works. If youtube-dl doesn't work, I think it is your internet, if it does work but our package doesn't please let me know I will try again to help.

Once I had installed Python 3.6.8, I created a venv by running

python3.6 -m venv <path to virtual envirnment>

then activated the test environment with

source <path to virtual environment>/bin/activate

then installed pytube and updated to pytube3

pip3 install pytube
pip3 install pytube3 --upgrade

then I just ran the script and had no errors. I was able to download whatever youtube video I wanted.

Let me know how this goes anyway, I am eager to get this working for you.

arslandogar commented 4 years ago

I tried using VPN and got the same error. youtube-dl also not working. So I guess it's my internet, is there anything that I can do about to make this work?

swiftyy-mage commented 4 years ago

Is there anything you can do? Probably, but this is a little bit out of my knowledge 🤔. Which VPN are you using? And which country is your server in? I think it needs to be another country.

arslandogar commented 4 years ago

I am using Proton VPN, connected to fastest available server which happened to be in Netherlands.

swiftyy-mage commented 4 years ago

I'm really sorry, I don't know how to help at the moment. Maybe try asking on some other website? People who know more about networks and the internet? I will keep thinking about this, if anything comes to mind I will post a reply here.

swiftyy-mage commented 4 years ago

I have been doing some research, when you use youtube-dl have you tried using the flag --force-ipv4 ? People were having a similar issue ad that solved it for them?

bigsandip commented 4 years ago

Youtube-dl and pytube both works on local server but on a live website when you try downloading more then 3-4 videos it shows this error. I have been searching for the solution....iI found this but don't know how to do Check this

arslandogar commented 4 years ago

youtube-dl worked after using the flag --force-ipv4.

arslandogar commented 4 years ago

And now everything is working. I have tried 6 different videos with youtube-dl and it's working fine with and without --force-ipv4. Pytube is also working fine. But I don't know why.

swiftyy-mage commented 4 years ago

@bigsandip When you say local server I don't know what you mean, is that a VPN? And about the solution you linked, I have never gone through the code in youtube-dl so I'm not sure how that program works. By the looks of it, you can save cookies in a config file. There is no option to do that in pytube currently, I will research this and see if I can add something similar in the future if it works for you. Did you try using the flag --force ipv4 in youtube-dl?

swiftyy-mage commented 4 years ago

@arslandogar Well that is good everything is working now! I only know how pytube works, I thought I knew the basics of networks too but it looks like I need to do more research before I understand why this was not working for you.

bigsandip commented 4 years ago

@swiftyy-mage I mean on http://127.0.0.1:8000/ . I haven't tried --force ipv4 but ill do now.

bigsandip commented 4 years ago

@swiftyy-mage How do i use --force ipv4 in this:

ydl_opts = {
                'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio',

                'outtmpl': '~/Downloads/video.mp4',

                'noplaylist': True,
            }
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:

                ydl.download([url])
swiftyy-mage commented 4 years ago

@bigsandip I think it is just a command line flag, you just type it in as an argument when you use youtube-dl. I have never used youtube-dl though. I recommend trying to use a VPN.

e2jk commented 4 years ago

In case this could help, I ran this on my local computer without any VPN, no specific issues here:

Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pytube import YouTube
>>> yt = YouTube('https://www.youtube.com/watch?v=tPEE9ZwTmy0')
>>> print(yt.title)
Shortest Video on Youtube
>>> 
KeyboardInterrupt
>>> 

$ uname -a
Linux daguerre 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1 03:25:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
$ python3 --version
Python 3.6.9
$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"
hafiz-ahtasham-ali commented 4 years ago

Try using this method: https://github.com/kazanture/YouPy

StarkGang commented 4 years ago

Try using this method: https://github.com/kazanture/YouPy

But I am using Heroku 😳😕