https://developer.yahoo.co.jp/webapi/jlp/keyphrase/v2/extract.html
ヤフーのAPIを使いたいと思っています。RubyもAPIもJsonもPOSTも初心者です。いろいろ試したり、検索しても出てこなかったのでご教授下さい。
Ruby
1# ヤフーのAPIでキーフレーズ抽出したいよー 2# https://developer.yahoo.co.jp/webapi/jlp/keyphrase/v2/extract.html 3require 'json' 4require 'uri' 5require 'net/http' 6 7APPID = "ないしょ" 8 9def post(query) 10 header = { 11 "Content-Type": "application/json", 12 "User-Agent": "Yahoo AppID: {#{APPID}}" 13 } 14 param = { 15 "id": "1234-1", 16 "jsonrpc": "2.0", 17 "method": "jlp.keyphraseservice.extract", 18 "params": { 19 "q": query 20 } 21 } 22 23 param = param.to_json 24 25 # uri = URI('https://jlp.yahooapis.jp/KeyphraseService/V2/extract') 26 uri = URI.parse("https://jlp.yahooapis.jp/KeyphraseService/V2/extract") 27 28 http = Net::HTTP.new(uri.host,uri.port) 29 response = http.post(uri.path,param,header) 30 31 response.code 32 response.body 33end 34 35puts post("東京ミッドタウンから国立新美術館まで歩いて5分で着きます。")
エラーコード
/Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:515:in `post': undefined method `hostname' for "/KeyphraseService/V2/extract":String (NoMethodError) from test1.rb:29:in `post' from test1.rb:35:in `<main>' USER@USERnoAir Keyphrase % ruby test1.rb /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/protocol.rb:227:in `rbuf_fill': end of file reached (EOFError) from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/protocol.rb:193:in `readuntil' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/protocol.rb:203:in `readline' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http/response.rb:42:in `read_status_line' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http/response.rb:31:in `read_new' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1557:in `block in transport_request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1548:in `catch' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1548:in `transport_request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1521:in `request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1514:in `block in request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:960:in `start' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1512:in `request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1535:in `send_entity' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1323:in `post' from test1.rb:29:in `post' from test1.rb:35:in `<main>'
また、headerを「header.to_json」として送ると、以下のエラーが出ました。
/Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http/generic_request.rb:36:in `initialize': undefined method `keys' for "{\"Content-Type\":\"application/json\",\"User-Agent\":\"Yahoo AppID: {dj00aiZpPVVnQ1BVVlRjaG5GeiZzPWNvbnN1bWVyc2VjcmV0Jng9NGE-}\"}":String (NoMethodError) from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http/request.rb:15:in `initialize' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1535:in `new' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1535:in `send_entity' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1323:in `post' from test1.rb:29:in `post' from test1.rb:35:in `<main>'
<<追記>>
最初の「require 'net/http'」を「require 'net/https'」に改めて実行すると以下のエラー
USER@USERnoAir Keyphrase % ruby test1.rb /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/protocol.rb:227:in `rbuf_fill': end of file reached (EOFError) from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/protocol.rb:193:in `readuntil' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/protocol.rb:203:in `readline' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http/response.rb:42:in `read_status_line' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http/response.rb:31:in `read_new' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1557:in `block in transport_request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1548:in `catch' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1548:in `transport_request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1521:in `request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1514:in `block in request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:960:in `start' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1512:in `request' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1535:in `send_entity' from /Users/USER/.rbenv/versions/3.0.2/lib/ruby/3.0.0/net/http.rb:1323:in `post' from test1.rb:29:in `post' from test1.rb:35:in `<main>'
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [arm64-darwin20]
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/11/23 04:34