blog/content/post/2008/09/01/2008-09-01-00001017.md

47 lines
2.8 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: getを使ったHTTPリクエストをperlでやるよ
author: kazu634
date: 2008-09-01
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4253;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- LWP
- Perl
---
<div class="section">
<p>
Amazonでperlを検索するよ:
</p>
<pre class="syntax-highlight">
<span class="synComment"># === Libraries ===</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># LWP module</span>
<span class="synStatement">use </span>LWP <span class="synConstant">5.64</span>;
<span class="synComment"># Character Encoding</span>
<span class="synStatement">use </span>Encode;
<span class="synStatement">use utf8</span>;
<span class="synStatement">binmode</span>( <span class="synIdentifier">STDERR</span>, <span class="synConstant">':raw :encoding(utf8)'</span> );
<span class="synComment"># === main method ===</span>
<span class="synComment"># get then content of the url.</span>
<span class="synStatement">my</span> <span class="synIdentifier">$browser</span> = LWP::UserAgent-&#62;<span class="synStatement">new</span>;
<span class="synComment"># getする先のURIを指定するよ</span>
<span class="synStatement">my</span> <span class="synIdentifier">$url</span> = URI-&#62;<span class="synStatement">new</span>(<span class="synConstant">'http://www.amazon.co.jp/s/ref=nb_ss_gw?'</span>);
<span class="synComment"># Queryの指定</span>
<span class="synIdentifier">$url</span>-&#62;query_form(
<span class="synConstant">'__mk_ja_JP'</span> =&#62; encode(<span class="synConstant">'shiftjis'</span>, <span class="synConstant">'カタカナ'</span>),
<span class="synConstant">'url'</span> =&#62; encode(<span class="synConstant">'shiftjis'</span>, <span class="synConstant">'search-alias=aps'</span>),
<span class="synConstant">'field-keywords'</span> =&#62; <span class="synConstant">'perl'</span>,
<span class="synConstant">'x'</span> =&#62; <span class="synConstant">'0'</span>,
<span class="synConstant">'y'</span> =&#62; <span class="synConstant"></span>
);
<span class="synComment"># 実際にリクエストを送る</span>
<span class="synStatement">my</span> <span class="synIdentifier">$response</span> = <span class="synIdentifier">$browser</span>-&#62;get(<span class="synIdentifier">$url</span>);
<span class="synComment"># デコードして表示</span>
<span class="synStatement">print</span> decode(<span class="synConstant">'shiftjis'</span>, <span class="synIdentifier">$response</span>-&#62;content);
<span class="synComment"># print($url)</span>
</pre>
</div>