我在一个IRC漫游器的pastebin上找到了这个脚本,可以为您找到youtube视频。我根本没有碰过它(禁止 channel 设置),它可以很好地工作,但是它不会获取已搜索视频的URL。此代码不是我的!我想让它正常工作,因为它将非常有用!
#!/usr/bin/env ruby
require 'rubygems'
require 'cinch'
require 'nokogiri'
require 'open-uri'
require 'cgi'
bot = Cinch::Bot.new do
configure do |c|
c.server = "irc.freenode.net"
c.nick = "YouTubeBot"
c.channels = ["#test"]
end
helpers do
#Grabs the first result and returns the TITLE,LINK,DESCRIPTION
def youtube(query)
doc = Nokogiri::HTML(open("http://www.youtube.com/results?q=#{CGI.escape(query)}"))
result = doc.css('div#search-results div.result-item-main-content')[0]
title = result.at('h3').text
link = "www.youtube.com"+"#{result.at('a')[:href]}"
desc = result.at('p.description').text
rescue
"No results found"
else
CGI.unescape_html "#{title} - #{desc} - #{link}"
end
end
on :channel, /^!youtube (.+)/ do |m, query|
m.reply youtube(query)
end
on :channel, "polkabot quit" do |m|
m.channel.part("bye")
end
end
bot.start
目前,如果我使用命令
!youtube asdf
我得到这个返回:
19:25 < YouTubeBot> asdfmovie - Worldwide Store www.cafepress.com ... asdfmovie cakebomb tomska epikkufeiru asdf movie ... tomska ... [Baby Giggling] Man: Got your nose! [Baby ... - www.youtube.com#
如您所见,URL只是www.youtube.com#,而不是视频的URL。
非常感谢!
最佳答案
这是一个xpath问题。看起来第三个'a'具有所需的href,因此请尝试:
link = "www.youtube.com#{result.css('a')[2][:href]}"
关于ruby - Ruby:脚本无法正确从YouTube抓取URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7758697/