TEDの英語原稿を取得する
方針
http://www.ted.com/talks/subtitles/id/# {固有のID}/lang/en を叩くと、英語原稿のjsonが返ってくる。
TEDのビデオの固有のIDを取得して、API叩いて、jsonをparseして、出力すればいけそう。
jsonのパース
gem install jsonrequire 'rubygems'
require 'open-uri'
require 'json'
open("http://www.ted.com/talks/subtitles/id/1183/lang/en") do |f|
json = JSON.parse(f.read)
json['captions'].each do |j|
puts j['content']
end
endこんな感じで取得できそう。
固有IDを取得するために、nokogiriでHTMLをparseする
brew update
brew install libxml2 libxslt
gem install nokogirirequire 'rubygems'
require 'open-uri'
require 'json'
require 'nokogiri'
url = "http://www.ted.com/talks/brene_brown_listening_to_shame.html"
doc = Nokogiri::HTML(open(url))
ted_id = doc.xpath("//div[@id='share_and_save']").first.attribute("data-id")
open("http://www.ted.com/talks/subtitles/id/#{ted_id}/lang/en") do |f|
json = JSON.parse(f.read)
json['captions'].each do |j|
puts j['content']
end
endスクレイピングの方法がわからなくて四苦八苦。以下を参照して、怪しげながら、固有IDの取得。
できた
参考
Related contents
TECH
2012.04.23
TECH
2012.04.19
TECH
2012.04.03
TECH
2012.03.21
TECH
2012.03.05
TECH
2012.03.04
TECH
2012.02.01
TECH
2012.01.30