2

I want to scrape a youtube channel with pytube Channel but I get an index error because I got zero URLs. Anything else is working well just video_urls is not working.

my code:

c = Channel("https://www.youtube.com/c/BuildEmpire")
print(f"This is a test number of video urls:  {len(c.video_urls)}")
for url in c.video_urls[:20]:
  video = YouTube(url)
  description2 = video.description
  try:
    videos_credits = slicer(description2,"Video Credits:")
    videos_credits = reversed_slicer(videos_credits,"Thumbnail:")
    urls = re.findall(r'(https?://[^\s]+)', videos_credits)
  except Exception:
    print("Sorry but I choose bad youtube video I will try it again")

error what I get is:

PS C:\Users\Lukas\Dokumenty\python_scripts\Billionare livestyle> & "c:/Users/Lukas/Dokumenty/python_scripts/Billionare livestyle/env/youtube/Scripts/python.exe" "c:/Users/Lukas/Dokumenty/python_scripts/Billionare livestyle/bilionare_life.py"
[]
0
Traceback (most recent call last):
  File "C:\Users\Lukas\Dokumenty\python_scripts\Billionare livestyle\env\youtube\lib\site-packages\pytube\helpers.py", line 57, in __getitem__
    next_item = next(self.gen)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Lukas\Dokumenty\python_scripts\Billionare livestyle\bilionare_life.py", line 198, in <module>
    for url in c.video_urls[:20]:
  File "C:\Users\Lukas\Dokumenty\python_scripts\Billionare livestyle\env\youtube\lib\site-packages\pytube\helpers.py", line 60, in __getitem__
    raise IndexError
IndexError
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
ALex Break
  • 59
  • 10

1 Answers1

4

The reason why you don't retrieve any video is that it's just an issue on pytube end, as YouTube modified recently its user-interface (on which pytube relies), and this issue is currently being resolved.

However as temporary a workaround installing pytube as following solves the problem:

pip install git+https://github.com/pishiko/pytube.git@42a7d8322dd7749a9e950baf6860d115bbeaedfc
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
  • It also won't allow me to create lists when the channel has an '@' in the url. I think these might be verified channels. Any thoughts? – Hanley Soilsmith Nov 18 '22 at 22:56
  • 1
    [@HanleySoilsmith](https://stackoverflow.com/users/6815505/hanley-soilsmith) Indeed I get `pytube.exceptions.RegexMatchError: channel_name: could not find match for patterns` when trying using a YouTube channel handle. [`pytube`](https://github.com/pytube/pytube) hasn't been updated since Sep 18, so in theory it can't support handles (`@` in the URL), as they have been rolled out very recently. Resolve the channel id from the channel handle, by using [this StackOverflow answer](https://stackoverflow.com/a/74324720), before proceeding. – Benjamin Loison Nov 18 '22 at 23:27
  • New YouTube updates caused other things to break in `v12.1`. For the newest fixed version (`v15.0` as of commenting), do `pip install git+https://github.com/24makee/pytube.git@c709202d4f2c0d36d9484314d44fd26744225b7d` – Squarish Aug 26 '23 at 18:32