-3

Please help me fix this, this is my code which I've already tried. I really appreciate your help.

import urllib.request
import re

search_keyword="ill%20wiat"
html = urllib.request.urlopen("https://www.youtube.com/results?search_query=" + search_keyword)
video_ids = re.findall(r"watch?v=(\S{11})", html.read().decode())
print("https://www.youtube.com/watch?v=" + video_ids[0])
Tejesh
  • 13
  • 1
  • check `video_ids` value to ensure it is not empty, or None, you can do so by `print(video_ids)` – Ajay Verma Sep 30 '20 at 07:01
  • video_ids list has empty output (print (len(video_ids)). it doesn't have any element to access. if video_ids: print("https://www.youtube.com/watch?v=" + video_ids[0]) else: print ("Empty") – Skanda Shastry Sep 30 '20 at 07:06
  • 2
    Please provide full traceback. – rizerphe Sep 30 '20 at 07:13
  • Does this answer your question? [IndexError: list index out of range and python](https://stackoverflow.com/questions/1098643/indexerror-list-index-out-of-range-and-python) – Skanda Shastry Sep 30 '20 at 07:16
  • Tejesh: **This question should be updated to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem.** – Mauricio Arias Olave Oct 05 '20 at 14:30

2 Answers2

0

urlib won't give you data use

import requests
html=requests.get('https://www.youtube.com/results?search_query='+search_keyword)
text=html.text

text have all html data so search from text

tard
  • 107
  • 6
0

First of all check page you try to parse. you wrote:

r"watch?v=(\S{11})"

just remember that ? char here will be parsed as REGEX operator and not string you want, so first of all you need to write it like:

/watch[?]v=(\S{11})

so your regex will be parsed properly
Second: good practice to print your list to see what you get and iterate via list using FOR loop instead of directly accessing index [0].
in you case you get this error just because your list of id is empty.

next code is working for me

import urllib.request
import re

search_keyword="ill%20wiat"
url="https://www.youtube.com/results?search_query="+search_keyword
with urllib.request.urlopen(url) as response:
   video_ids = re.findall("/watch[?]v=(\S{11})", response.read().decode())
   for video in video_ids:
      print("https://www.youtube.com/watch?v=" + video)

P.S don't wrap your code with try/except to catch such thrown errors

Alex S
  • 66
  • 5