【问题标题】:How to get many JSON URLs and parse for dynamic content如何获取许多 JSON URL 并解析动态内容
【发布时间】:2017-02-25 13:07:43
【问题描述】:

我正在尝试创建第一个从 TMDB API 解析数据的应用。

当我尝试加载和解析一个 URL 时,一切正常:

uri = URI.parse("http://api.themoviedb.org/3/tv/#URI.escape(@id)}?api_key=#{@api_key}")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
@results = JSON.load(response.body)

但现在我想解析可用的 JSON:

/tv/{tv_id}/season/{season_number}/episode/{episode_number}

其中:{tv_id}@id = params[:id]

{season_number}{episode_number} 是第一个 URL 产生的季节和剧集。 我尝试了多种方法,但每次都会出现随机错误。

完整代码在Github。 工作应用是TOMDB App on Heroku

我正在尝试在 Serie 的同一页面中显示季节标题和剧集。 例如,当我解析时:

https://api.themoviedb.org/3/tv/2432?api_key=**********

我明白了:

{
"backdrop_path":"/hl9mC6fc2adfeGpI1ijKCfQ0KzI.jpg",
"name":"Taken",
"number_of_episodes":10,
"number_of_seasons":1
}

所以我有一季有 10 集。对于每一集,我都有一个 URL,将剧集编号 {1..5..10} 替换为:

https://api.themoviedb.org/3/tv/8681/season/1/episode/*1*?api_key=********

我想为每一集显示剧集名称,所以一集的 JSON 结果如下:

{
"name":"Beyond the Sky","overview":"......",
"id":183273,
"vote_average":0.0,
"vote_count":0
}

【问题讨论】:

  • 你能手动点击那个 JSON 端点(使用 curl 或 Postman 等)并得到你所期望的吗?
  • 你得到什么“随机错误”?在您看来,它可能看起来是随机的,但错误很重要,而且很少是随机的。请阅读“minimal reproducible example”。
  • Jhon Feltz,是的,我可以得到所有 url 的一个一个
  • The Tin Man 我的意思是每次搜索我都会得到一个错误,但通常是未初始化的常量,那是因为我试图在值为 nil 时获取多个 json url

标签: json ruby sinatra net-http open-uri


【解决方案1】:

您需要对该请求使用 https,请参阅我用 #** 标记的需要更改的部分。注意使用 Net::HTTP.start 而不是 Net::HTTP.new。 顺便说一句,不错的应用程序。

require 'net/http'
require 'net/https' #*add*
require 'json'
require 'open-uri'

url = 'https://api.themoviedb.org/3/tv/1418/season/1/episode/1?api_key=**************************&language=en-US'
uri = URI.parse(url)
# *adapt following line*
http = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
json = JSON.load(response.body)


# {"air_date"=>"2007-09-24", "crew"=>[{"id"=>157437, "credit_id"=>"5256cfee19c2956ff60a280c", "name"=>"James Burrows", "department"=>"Directing", "job"=>"Director", "profile_path"=>"/lTcRumFOm6HkfOyPuUElV4l4n4r.jpg"}, {"id"=>160172, "credit_id"=>"5256cfbc19c2956ff60a0483", "name"=>"Chuck Lorre", "department"=>"Writing", "job"=>"Writer", "profile_path"=>"/btpYlMV71sjQXrV142I9kogEINI.jpg"}, {"id"=>163528, "credit_id"=>"5256cfbd19c2956ff60a04f0", "name"=>"Bill Prady", "department"=>"Writing", "job"=>"Writer", "profile_path"=>"/duXUvo8JtivQR0BHiXHGQwoNYB4.jpg"}, {"id"=>1480308, "credit_id"=>"55881d379251416818001c2b", "name"=>"Janice Zoladz", "department"=>"Costume & Make-Up", "job"=>"Hairstylist", "profile_path"=>nil}, {"id"=>1480289, "credit_id"=>"558819ffc3a36836ea006145", "name"=>"Joe Bella", "department"=>"Production", "job"=>"Co-Producer", "profile_path"=>nil}, {"id"=>1232374, "credit_id"=>"55881a1492514179f6003f6e", "name"=>"Michael Collier", "department"=>"Production", "job"=>"Producer", "profile_path"=>nil}, {"id"=>1480291, "credit_id"=>"55881a269251411efc000f6e", "name"=>"Toti Levine", "department"=>"Production", "job"=>"Associate Producer", "profile_path"=>nil}], "episode_number"=>1, "guest_stars"=>[{"id"=>1118085, "name"=>"Brian Patrick Wade", "credit_id"=>"5256cfc919c2956ff60a0c8f", "character"=>"Kurt", "order"=>0, "profile_path"=>"/6y0Hd1xLC5Nitedg2GQ90DtxxDb.jpg"}, {"id"=>157085, "name"=>"Vernee Watson-Johnson", "credit_id"=>"5256cfd719c2956ff60a1747", "character"=>"", "order"=>1, "profile_path"=>"/dRXXPoeAAbl1PhURCJfKCDoZiUn.jpg"}], "name"=>"Pilot", "overview"=>"Brilliant physicist roommates Leonard and Sheldon meet their new neighbor Penny, who begins showing them that as much as they know about science, they know little about actual living.", "id"=>64766, "production_code"=>nil, "season_number"=>1, "still_path"=>"/rxWlBXZhGWhumbLB8gAHyyW3ITD.jpg", "vote_average"=>7.39285714285714, "vote_count"=>14}

编辑:要获取一个季节的所有剧集,您只需从 1 数到剧集数,然后组装并请求新的 url。 你必须做同样的事情来列举一个系列的所有季节,但我给你留下了一些工作超过 8 :) 成功!

require 'net/http'
require 'net/https'
require 'json'
require 'open-uri'
require 'pp'

# we have to do this more than once, so let's make it DRY
def get_json url
  uri = URI.parse(url)
  http = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) #**
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  JSON.load(response.body)
end

API_KEY  = '****************************'
url = "https://api.themoviedb.org/3/tv/2432?api_key=#{API_KEY}"
season = get_json url

season['number_of_episodes'].to_i.times do |episode|
  episode += 1 # times starts from 0 , so we add 1
  url = "https://api.themoviedb.org/3/tv/8681/season/1/episode/#{episode}?api_key=#{API_KEY}"
  episode = get_json url
  # pp episode
  puts episode['name']
end

gives

# Great White Shark: The True Story of Jaws
# Polar Bear: The Arctic Warrior
# Crocodile: The Smiling Predator
# Leopard: The Agent of Darkness
# Eagle: The Master of the Skies
# Humpback Whale: The Giant of the Oceans
# Wolf: The Legendary Outlaw
# Tiger: The Elusive Princess
# Lions: Spy in the Den
# Grizzly: Face to Face

【讨论】:

  • 让我知道它是否有效,然后请接受答案,我也有兴趣看到最终版本
  • 对不起,彼得先生,您的发言很好!并更正了我的代码,但问题不在于访问安全网址,而是在子网址依赖于第一个网址时访问多个网址
  • 没问题,添加到我的答案中
猜你喜欢
  • 2023-03-29
  • 2017-02-11
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多