リンク内容の続き
というより今までの質問の総合的な続き
やりたいことは上記のリンクと同じで
リンク内容このサイトの検索レスポンスを取得したい。
前回のコード
ruby
1require 'net/http' 2domain = "www.amazon.co.jp"#ドメイン 3port = 80 #ポート番号 4 5Net::HTTP.start(domain,port){|http| 6 response = http.post('/s/ref=nb_sb_noss',"field-keywords=ruby") 7 puts response.body 8}
このコードは回答でhttpsサーバーに対してhttpリクエストを投げているから
レスポンスが期待していたものが送られてこなかった です。
今回はhttpsにも対応できるようにしたはずなのですが
エラーこそならないのですがなにも出力されません。
ruby
1require 'net/https' 2uri = URI.parse("https://www.amazon.co.jp/s/ref=nb_sb_noss?__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&url=search-alias%3Dhobby&field-keywords=") 3http = Net::HTTP.new(uri.host, uri.port)←叩きたいサーバーのドメインとポートを指定してHTTPクライアントのインスタンス作成。 4req = Net::HTTP::Post.new(uri.path)←HTTPリクエストのインスタンスをパスを与えて指定 5req.set_form_data({'field-keywords=' => 'sample'})←検索キーワードにsampleを指定 6http.use_ssl = true ←SSLを利用して接続する場合に真を返します。by リファレンス 7http.verify_mode = OpenSSL::SSL::VERIFY_NONE 8res = http.request(req)←サーバーに引数のリクエストのインスタンスを送信(帰ってきたレスポンスをresに代入) 9puts res.body←レスポンス表示
わからないので解決方法を教えてくださいお願いします。
また、コードの説明で誤っているものを見つけたら教えてください。
追記
コードに
ruby
1http.set_debug_output $stderr
cmd
1opening connection to www.amazon.co.jp:443... 2opened 3starting SSL for www.amazon.co.jp:443... 4SSL established 5<- "POST /s/ref=nb_sb_noss HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: Ruby\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: close\r\nHost: www.amazon.co.jp\r\nContent-Length: 24\r\n\r\n" 6<- "field-keywords%3D=sample" 7-> "HTTP/1.1 301 Moved Permanently\r\n" 8-> "Transfer-Encoding: chunked\r\n" 9-> "Connection: close\r\n" 10-> "Server: Server\r\n" 11-> "Date: Thu, 18 Jul 2019 10:52:27 GMT\r\n" 12-> "Strict-Transport-Security: max-age=47474747; includeSubDomains; preload\r\n" 13-> "Vary: Accept-Encoding,User-Agent,X-Amzn-CDN-Cache,X-Amzn-AX-Treatment\r\n" 14-> "Content-Language: ja-JP\r\n" 15-> "Location: /\r\n" 16-> "Content-Encoding: gzip\r\n" 17-> "X-XSS-Protection: 1;\r\n" 18-> "X-Content-Type-Options: nosniff\r\n" 19-> "X-Frame-Options: SAMEORIGIN\r\n" 20-> "x-amz-rid: QSRPAX02WBR568F9P92Y\r\n" 21-> "X-Cache: Miss from cloudfront\r\n" 22-> "Via: 1.1 b59fc5b326cd7a1c5137742614e4ef0a.cloudfront.net (CloudFront)\r\n" 23-> "X-Amz-Cf-Pop: NRT12-C1\r\n" 24-> "X-Amz-Cf-Id: Re0t7lWxe41IlRgpS26oriO67okMLSA2Cq7S9NBQT29HCwyk8jrxdQ==\r\n" 25-> "\r\n" 26-> "14\r\n" 27reading 20 bytes... 28-> "\x1F\x8B\b\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00" 29read 20 bytes 30reading 2 bytes... 31-> "\r\n" 32read 2 bytes 33-> "0\r\n" 34-> "\r\n" 35Conn close
を追加したところ
との結果がでましたがこれは。。。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/18 11:57
2019/07/18 12:03
2019/07/18 12:10
2019/07/18 12:11