TEDの英語原稿を取得する
更新日:2023.05.05
作成日:2012.03.20
方針
http://www.ted.com/talks/subtitles/id/# {固有のID}/lang/en を叩くと、英語原稿のjsonが返ってくる。
TEDのビデオの固有のIDを取得して、API叩いて、jsonをparseして、出力すればいけそう。
jsonのパース
gem install json
require '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 nokogiri
require '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
Happiness 私たちが幸せを感じる理由

TECH
2012.04.19
RubyからGMailを使ってメール送信

TECH
2012.04.03
Padrinoを触ってみた

TECH
2012.03.21
TEDから取得したmp3に英語原稿を埋め込む

TECH
2012.03.05
RubyからEvernoteAPIを使う-Web Application編-

TECH
2012.03.04
RubyからEvernoteAPIを使ってノートブックの取得と、ノートの作成をする

TECH
2012.02.01
Sinatra触ってると出てくるRackって何?

TECH
2012.01.30
Lokkaプラグインlokka-twitter_urlを作ってみた