Skip to content

Instantly share code, notes, and snippets.

@jakenotjacob
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakenotjacob/c0abf52d60495625120a to your computer and use it in GitHub Desktop.
Save jakenotjacob/c0abf52d60495625120a to your computer and use it in GitHub Desktop.
Video URL of first Youtube search result.
require 'nokogiri'
require 'open-uri'
puts "Enter your search terms: "
terms = gets.chomp
terms = terms.split(" ").join("+")
doc = Nokogiri::HTML(open("https://www.youtube.com/results?search_query=#{terms}"))
video_thumbnails = doc.at_css('.yt-thumb-default').css('.yt-thumb-clip')
first_video_thumbnail = video_thumbnails.children[1]
img_url = first_video_thumbnail.attributes["src"].value
vid_id = img_url.split("/")[4]
vid_url = "https://www.youtube.com/watch?v=#{vid_id}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment