Skip to content

Instantly share code, notes, and snippets.

@zx1986 zx1986/Gemfile
Last active Oct 1, 2016

What would you like to do?
Get YouTube URLs from iThome
source ''
gem 'nokogiri'
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'json'
base_url = ''
yt_url = ''
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.views-field-title span.field-content a")
images = seminar_page.css("div.view-content 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
puts result.to_json

This comment has been minimized.

Copy link
Owner Author

commented Oct 1, 2016
還沒能處理好 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.