2019-03-31 11:00:21 +00:00
|
|
|
---
|
|
|
|
title: このように書くときれいにスクレイピングできた
|
|
|
|
author: kazu634
|
|
|
|
date: 2009-07-08
|
|
|
|
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:4699;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
|
|
|
categories:
|
|
|
|
- Perl
|
|
|
|
- scraper
|
|
|
|
|
|
|
|
---
|
|
|
|
<div class="section">
|
|
|
|
<p>
|
|
|
|
すこし工夫すると、Web::Scraperできれいにスクレイピングできるようになった。
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
参考にしたのは
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
「<a href="http://d.hatena.ne.jp/dayflower/20080626/1214540593" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/dayflower/20080626/1214540593', 'Web::Scraper をつかってみた – daily dayflower');" target="_blank">Web::Scraper をつかってみた – daily dayflower</a>」
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
「<a href="http://d.hatena.ne.jp/javascripter/20080425/1209094795" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/javascripter/20080425/1209094795', 'XPath入門、実用例 – 素人がプログラミングを勉強するブログ');" target="_blank">XPath入門、実用例 – 素人がプログラミングを勉強するブログ</a>」
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<h4>
|
|
|
|
Perlスクリプト
|
|
|
|
</h4>
|
|
|
|
|
|
|
|
<pre class="syntax-highlight">
|
|
|
|
<span class="synComment"># === Libraries ===</span>
|
|
|
|
<span class="synStatement">use strict</span>;
|
|
|
|
<span class="synStatement">use warnings</span>;
|
|
|
|
<span class="synComment"># === Libraries ===</span>
|
|
|
|
<span class="synStatement">use </span>URI;
|
|
|
|
<span class="synStatement">use </span>Web::Scraper;
|
|
|
|
<span class="synStatement">use </span>YAML;
|
|
|
|
<span class="synStatement">use </span>Encode;
|
|
|
|
<span class="synStatement">use utf8</span>;
|
|
|
|
<span class="synComment"># === Main part ===</span>
|
|
|
|
<span class="synComment"># /html/body/div/div/div/div[4]/div/div[2]/div[2]/table/tbody/tr/td/div/strong</span>
|
|
|
|
<span class="synStatement">my</span> <span class="synIdentifier">$scraper</span> = scraper {
|
|
|
|
process
|
|
|
|
<span class="synConstant">'/html[1]/body[1]/center[2]/center[1]/table[1]/tbody[1]/tr[1]/td[2]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table[@class="size2"]'</span>,
|
|
|
|
<span class="synConstant">'shop'</span> => scraper {
|
|
|
|
process <span class="synConstant">'//tr[1]/td[2]'</span>, <span class="synConstant">'shopname'</span> => <span class="synConstant">'TEXT'</span>;
|
|
|
|
process <span class="synConstant">'//tr[2]/td[2]'</span>, <span class="synConstant">'tel'</span> => <span class="synConstant">'TEXT'</span>;
|
|
|
|
process <span class="synConstant">'//tr[3]/td[2]'</span>, <span class="synConstant">'address'</span> => <span class="synConstant">'TEXT'</span>;
|
|
|
|
process <span class="synConstant">'//tr[4]/td[2]'</span>, <span class="synConstant">'shophour'</span> => <span class="synConstant">'TEXT'</span>;
|
|
|
|
process <span class="synConstant">'//tr[5]/td[2]'</span>, <span class="synConstant">'accomodation'</span> => <span class="synConstant">'TEXT'</span>;
|
|
|
|
process <span class="synConstant">'//tr[6]/td[2]'</span>, <span class="synConstant">'access'</span> => <span class="synConstant">'TEXT'</span>;
|
|
|
|
process <span class="synConstant">'//tr[7]/td[2]'</span>, <span class="synConstant">'memo'</span> => <span class="synConstant">'TEXT'</span>;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
<span class="synStatement">my</span> <span class="synIdentifier">$result</span> =
|
|
|
|
<span class="synIdentifier">$scraper</span>->scrape( URI-><span class="synStatement">new</span>(<span class="synConstant">"http://www.ginza-renoir.co.jp/renoir/051.htm"</span>) );
|
|
|
|
<span class="synStatement">print</span> encode( <span class="synConstant">'utf8'</span>, Dump <span class="synIdentifier">$result</span> );
|
|
|
|
<span class="synComment"># print Dump $result;</span>
|
|
|
|
<span class="synComment"># Reference: http://www.e-kotoba.net/</span>
|
|
|
|
<span class="synComment"># Below is the sample</span>
|
|
|
|
<span class="synComment"># (1) <div class="ekotoba">...</div>の部分をbodyに取得</span>
|
|
|
|
<span class="synComment"># process 'div.ekotoba', 'body' => 'TEXT';</span>
|
|
|
|
<span class="synComment"># (2) XPATHで指定</span>
|
|
|
|
<span class="synComment"># Firebugsなどを用いる。</span>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<h4>
|
|
|
|
実行結果
|
|
|
|
</h4>
|
|
|
|
|
|
|
|
<pre class="syntax-highlight">
|
|
|
|
kazu634@kazu634-desktop% perl scraper.pl ~/work/googlemaps <span class="synStatement">[</span><span class="synConstant">2987</span><span class="synStatement">]</span>
|
|
|
|
---
|
|
|
|
shop:
|
|
|
|
access: JR池袋駅東口出て明治通り左並びの東池袋<span class="synConstant">1</span>丁目交差点ヤマダ電機隣
|
|
|
|
accomodation: 禁煙席:<span class="synConstant">38</span>席喫煙席:<span class="synConstant">32</span>席
|
|
|
|
address: 東京都豊島区東池袋<span class="synConstant">1-40-2</span>池袋旗ビル<span class="synConstant">2</span>階
|
|
|
|
memo: <span class="synStatement">''</span>
|
|
|
|
shophour: 年中無休全日:<span class="synConstant">7</span>:<span class="synConstant">30-23</span>:<span class="synConstant">00</span>
|
|
|
|
shopname: 喫茶室ルノアール 池袋東口店
|
|
|
|
tel: <span class="synConstant">03-3980-6352</span>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<h4>
|
|
|
|
「scraper」に関連する最近のエントリ
|
|
|
|
</h4>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<a href="http://d.hatena.ne.jp/sirocco634/20090705/1246801893" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20090705/1246801893', ' 銀座ルノアールの場所をマッピング – 武蔵の日記');" target="_blank"> 銀座ルノアールの場所をマッピング – 武蔵の日記</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="http://d.hatena.ne.jp/sirocco634/20090623/1245769314" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20090623/1245769314', ' Find XPathが便利 – 武蔵の日記');" target="_blank"> Find XPathが便利 – 武蔵の日記</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="http://d.hatena.ne.jp/sirocco634/20090621/1245573790" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20090621/1245573790', ' 銀座ルノアールの店舗情報を取得 – 武蔵の日記');" target="_blank"> 銀座ルノアールの店舗情報を取得 – 武蔵の日記</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="http://d.hatena.ne.jp/sirocco634/20090517/1242563607" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20090517/1242563607', ' 本日購入した本 – 武蔵の日記');" target="_blank"> 本日購入した本 – 武蔵の日記</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2019-04-02 16:06:15 +00:00
|
|
|
</div>
|