実現したいこと
perl + html::TreeBuilder を使用した、Webサイトのスクレイピング。
取得したいサイトの情報/ソースには class や id などの目印がない状態。
取得したい値は<td>〜</td>の間にセットされていてる値を取得したい。
前提
perl + html::TreeBuilder を使用して、Webサイトのスクレイピングをおこなっていますが、取得したいサイトの情報/ソースには class や id などの目印がない状態で、tdタグ内に1行でつめこまれています。
取得したい値は<td>〜</td>の間にセットされていてる値を取得したい。
Webサイトのソースコード
<table border="0" width="100%" cellspacing="0" cellpadding="2"> <tr> <td align="center" width="25%" valign="top"><a class="image" href="https://foo.com/item_id/1001”><img src=“https://foo.com/up/1001.jpg" border="0" alt=“商品A”></a><a href="https://foo.com/item_id/1001”>商品A</a><a href="https://foo.com/maker/9001”>X株式会社</a>1,200円</td> <td align="center" width="25%" valign="top"><a class="image" href="https://foo.com/item_id/1002”><img src=“https://foo.com/up/1002.jpg" border="0" alt=“商品B”></a><a href="https://foo.com/item_id/1002”>商品B</a><a href="https://foo.com/maker/9001”>X株式会社</a>1,000円</td> <td align="center" width="25%" valign="top"><a class="image" href="https://foo.com/item_id/1003”><img src=“https://foo.com/up/1003.jpg" border="0" alt=“商品C”></a><a href="https://foo.com/item_id/1003”>商品C</a><a href="https://foo.com/maker/9002”>Y株式会社</a>2,200円</td> <td align="center" width="25%" valign="top"><a class="image" href="https://foo.com/item_id/1004”><img src=“https://foo.com/up/1004.jpg" border="0" alt=“商品D”></a><a href="https://foo.com/item_id/1004”>商品D</a><a href="https://foo.com/maker/9002”>Y株式会社</a>1,800円</td> </tr> </table>
サイトの情報を次のイメージのの内容で取得したいです。
◆抽出したいイメージ(itemなどの項目名は説明のために記入、先頭の h は削除)
item : 商品A
Item_url : ttps://foo.com/item_id/1001
Image_url : ttps://foo.com/up/1001.jp
Maker_url : ttps://foo.com/maker/9001
Maker_name : X株式会社
Price : 1,200円
item : 商品B
Item_url : ttps://foo.com/item_id/1002
Image_url : ttps://foo.com/up/1002.jp
Maker_url : ttps://foo.com/maker/9001
Maker_name : X株式会社
Price : 1,000円
:
:
既に作成し利用しているコードを参考に新しいコードを作成してみましたが、希望通りの値を取得することができませんでした。既に作成していて動かしているコードは、取得したい値には class や id の目印となるものが付いていて問題なく取得ができています。
今回のようなサイトは初めてのためうまく値が取得できていません。
作成したソースコード(処理部分のみ抜粋)
$tree->parse_file("$file"); $tree->eof(); $tree->as_XML('&<>'), "\n"; foreach $td ($tree->find("td")) { #Aタグ抽出 if ($td->look_down('_tag', 'a')) { $product_info_wrk = ($td->look_down('_tag', 'a')->attr('href')); $product_text_wrk = ($td->look_down('_tag', 'a')->as_text); print $product_info_wrk, "\n"; print $product_text_wrk, "\n"; undef $product_info_wrk; undef $product_text_wrk; } if ($td->look_down('_tag', 'img')) { $img_wrk = ($td->look_down('_tag', 'img')->attr('src')); print $img_wrk, "\n"; undef $img_wrk; } if ($td->look_down('_tag', 'img')) { $img_alt_wrk = ($td->look_down('_tag', 'img')->attr('alt')); print $img_alt_wrk, "\n"; undef $img_alt_wrk; } }
コードを実行すると a タグは1回だけ、a タグで囲まれているテキストは取得ができずと、目的を達成することができませんん。どの様にすれば実現可能かお助けください。
◆実行結果
ttps://foo.com/item_id/1001
<空白>
ttps://foo.com/up/1001.jpg
商品A
ttps://foo.com/item_id/1002
<空白>
ttps://foo.com/up/1002.jpg
商品B
:
:
補足情報(FW/ツールのバージョンなど)
実行環境は次の通りです。
OS : linux
Perl : v5.26.3
html::TreeBuilder : 5.07
0 コメント