Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
How to get the first video from a youtube search in javascript

Soo i've already seen some ways for doing it with youtube api but for some reason it didn't work. And i had a project that i was working on that the does exactly what i want in python So I tried to "translate it" to javascript but i didn't an equivilant for urllib.request.urlopen that works for js
here is the part of the python code that i want to "translate"

formated_txt = keyword.replace(" ", "+")
html = urllib.request.urlopen("https://www.youtube.com/results?search_query=official+audio+" + formated_txt)#<<<searching for the equivalent for this line
print(html)
video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())
link = "https://www.youtube.com/watch?v=" + video_ids[0]

and this is where i'm at in js

function search() {
    var searchbox = document.getElementById("search");
    searchbox.select();
    searchbox.setSelectionRange(0, 99999);
    var text = searchbox.value.toLowerCase();
    var formated_text = text.split(" ").join("+");
    var link = 'https://www.youtube.com/results?search_query='+formated_text
}

i found that there is urllib module for js but it's not working or i did something wrong

function search() {
    var searchbox = document.getElementById("search");
    searchbox.select();
    searchbox.setSelectionRange(0, 99999);
    var text = searchbox.value.toLowerCase();
    var formated_text = text.split(" ").join("+");
    var link = 'https://www.youtube.com/results?search_query='+formated_text

    const { data, res } = await request(link, {
        method: 'GET',
        data: {}
      });
    // result: { data: Buffer, res: Response }
    console.log('status: %s, body size: %d, headers: %j', res.statusCode, data.length, res.headers);
}
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!