Web::Scraperを使用して指定したところから情報をとることはわかるのですが
たとえば、tebleのthのテキスト部分が○○の場合
その下のtdの部分からテキストの部分を取り出す
みたいな指定をしたいのですが
Web::Scraperで可能でしょうか?
可能でしたら指定の仕方を
ダメでしたら、他のやり方を教えていただければと思います。
ケースバイケースなのでHTMLを提示したほうが質問者回答者双方楽になります。
適当(精一杯)ですが、、
1回目のscrapeはtableのhtml(キャッシュしておいて2回アクセスするのを避ける)とthの集合を取得。
thの中から任意の文字列にマッチする要素の順番を取得。
さっきのhtmlをscrape対象にして、thと同じ順番のtdを取得。
use strict; use warnings; use Web::Scraper; use Data::Dumper; my $html_content = <<"EOT"; <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <table align="center" border="4" width="250"> <tr bgcolor="#cccccc"> <th> <br> </th> <th>列-A</th> <th>列-B</th> <th>列-C</th> </tr> <tr align="center"> <td>行-1</td> <td>A1</td> <td>B1</td> <td>C1</td> </tr> <tr align="center"> <td>行-2</td> <td>A2</td> <td>B2</td> <td>C2</td> </tr> <tr align="center"> <td>行-3</td> <td>A3</td> <td>B3</td> <td>C3</td> </tr> </table> </body> </html> EOT my $res = scraper { process '/html/body/table', 'table[]' => 'HTML'; process '/html/body/table//th', 'th[]' => 'TEXT'; }->scrape($html_content); #URLに変える my $th_position; for (my $i=0; $i < $res->{th}; $i++ ) { if ($res->{th}[$i] eq "列-B") { $th_position = $i; last; } } my $table_html = $res->{table}[0]; my $res2 = scraper { process '//td[' . ($th_position + 1) . ']', 'td[]' => 'TEXT' }->scrape($table_html); print Dumper $res2;
結果
$VAR1 = { 'td' => [ 'B1', 'B2', 'B3' ] };
XPathは軸をどうするかによって書き方が全然違ってきます。
2012/04/15 03:51:42効果的な軸を決めるには(Firebugを使って)観察です。
次に質問する際は初めからHTMLを提示していただけるとありがたいです。早く解決に結びつきます。
ありがとうございます。
2012/04/15 22:02:52今後はそうしたいと思います。