Skip to content

Instantly share code, notes, and snippets.

@zx1986 zx1986/Gemfile
Last active Oct 1, 2016

Embed
What would you like to do?
Get YouTube URLs from iThome
source 'https://rubygems.org'
gem 'nokogiri'
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'json'
base_url = 'http://www.ithome.com.tw/'
yt_url = 'https://www.youtube.com/watch?v='
data = open(base_url + '/videos/seminar-index')
html = Nokogiri::HTML(data)
result = {}
seminars = html.css("div.index-item a")
seminars.each do |s|
seminar_title = s.text
seminar_url = base_url + s["href"]
seminar_page = Nokogiri::HTML(open(seminar_url))
result[seminar_title] = []
titles = seminar_page.css("div.view-content div.video-channel-item div.views-field-title span.field-content a")
images = seminar_page.css("div.view-content div.video-channel-item div.views-field-field-videos div.field-content img")
images.each_with_index do |img, idx|
img_url = img["src"].to_s
video_url = yt_url + img_url[/\w+\.png/][/\w+/]
video_title = titles[idx].text
h = {}
h[:url] = video_url
h[:title] = video_title
result[seminar_title] << h
end
puts result.to_json
end
@zx1986

This comment has been minimized.

Copy link
Owner Author

commented Oct 1, 2016

http://stackoverflow.com/questions/11251364/regex-that-allows-dashes-and-numbers-only
還沒能處理好 YouTube 中有 - (dash) 的編碼。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.