0
@bot.command()
async def buscayt(ctx, *, search):
    query_string = parse.urlencode({'search_query': search})
    html_content = request.urlopen('http://www.youtube.com/results?' + query_string)
    search_results = re.findall(href = ("/watch/?v=(.{11})", html_content.read().decode())
    print(search_results)

query_string is used so I can search for what I want.

html_content is used so it opens up yt and what searches what I have written.

search_results should return the video results as codes but that's where I get an error.

This code is for my discord bot, but when a use the command it appears a error as href as undefined, so I really don't know how to solve it.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

It is saying href is undefined because you are treating it like a variable with the expression href =. If you want to match the literal string href= then you need to include that within the quotation marks in your findall regex:

re.findall(r'href="/watch\?v=(.{11})"', html_content.read().decode())

(Note you had an extra / after watch that is not part of a standard YouTube video URL.)

However, even then you will not get any results because YouTube searches populate dynamically using Javascript. That means you won't see the actual HTML links in the code you get with urlopen. (Try it - go to a YouTube search, right click on the page, click "View page source," and search for some links.)

This is a common issue, and a frequent solution is to use Selenium as described in this answer. In this case, though, you are in luck because the YouTube page does include a series of Javascript objects that contain the URLs you're looking for. So just change your search to:

re.findall(r"/watch\?v=(.{11})", html_content.read().decode())

And you should be good to go. Note you do need the backslash before the question mark to escape it properly in the regex, and the r makes it a raw string.

jdaz
  • 5,964
  • 2
  • 22
  • 34
  • Hi, thank you so much, it work, but now when i use the command to get the codes, it appears me only [], without anything @jdaz – HolaAdiosXD02 Jul 06 '20 at 12:37
  • Edited, try now with the above. I didn't realize you had an extra slash in your YouTube URL. There is no forward slash after "watch" – jdaz Jul 07 '20 at 01:31