2017-06-28 17 views
0

私は、Webページをスクラップし、HTMLタグ内の値を取得しようとしています。最終結果は クラブのように見える方法で値を分離するための方法だろう:X場所:yのURL:zのHTML :: PerlでのHTML :: Treeの解析

はここで、これまで私が持っているもの

use HTML::Tree; 
use LWP::Simple; 

$url = "http://home.gotsoccer.com/clubs.aspx?&clubname=&clubstate=AL&clubcity="; 
$content = get($url); 
$tree = HTML::Tree->new(); 
$tree->parse($content); 
@td = $tree->look_down(_tag => 'td', class => 'ClubRow'); 
foreach $1 (@td) { 
    print $1->as_text(); 
    print "\n"; 
} 

そして、何に印刷され

のようなものであるのです
AYSO UnitedMadison, ALwww.aysounitednorthalabama.org 

これは、私は方法が必要にHTMLが

<td class="ClubRow" width="80%"> 
    <div> 
     <a href="/rankings/club.aspx?ClubID=27086" class="ClubLink">AYSO United</a></div> 
    <div class="SubHeading">Madison, AL</div> 
     <a href="http://www.aysounitednorthalabama.org" target="_blank"><img src="/images/icons/ArrowRightSm.png" class="LinkIcon"><font color="black">www.aysounitednorthalabama.org</font></a> 
</td> 

次のようになりますこれらのフィールドを別々の変数に分割するか、ある種の文字を削除するようにして、Regexでそれを行うことができます。オンラインでの資料はあまりないので、助けに感謝します。

+0

'foreach $ 1(@td){' ...残りの部分については、あなたが望むすべてのビットが容易に識別可能です。だから、それらを抽出する。 –

+0

@SinanÜnür代わりに何をお勧めしますか? –

答えて

3

まず、これは醜態です:

foreach $1 (@td) { 
    print $1->as_text(); 
    print "\n"; 
} 

あなたはそれがかわいいと思うかもしれないが、あなたにも「Iを言い、特に以来ループ変数として$1ようなregex capture variablesを使用するように混乱しています私はRegexでそれを行うことができます。 (emphasis mine

これは、Perlに悪い名前を付ける保守不能なプログラムにつながるナンセンスの一種です。

常にstrictwarningsを使用して、ループにプレーン変数を使用してください。

第2に、各tdの3つの特定の要素に興味があります。1)a[class="ClubLink"]のテキスト。 2)The text of div[class="SubHeading"]; 3)font[color="black"]のテキスト。

だから、だけではなく、td内のテキストを平坦化する情報のそれらの3ビットを抽出:出力

#!/usr/bin/env perl 

use strict; 
use warnings; 

use HTML::Tree; 

my $html = <<HTML; 
<td class="ClubRow" width="80%"> <div> <a 
href="/rankings/club.aspx?ClubID=27086" class="ClubLink">AYSO United</a></div> 
<div class="SubHeading">Madison, AL</div> <a 
href="http://www.aysounitednorthalabama.org" target="_blank"><img 
src="/images/icons/ArrowRightSm.png" class="LinkIcon"><font 
color="black">www.aysounitednorthalabama.org</font></a> </td> 
HTML 

my $tree = HTML::Tree->new_from_content($html); 

my @wanted = (
    [class => 'ClubLink'], 
    [class => 'SubHeading'], 
    [_tag => 'font', color => 'black'], 
); 

my @td = $tree->look_down(_tag => 'td', class => 'ClubRow'); 

for my $td (@td) { 
    my ($club, $loc, $www) = map $td->look_down(@$_)->as_text, @wanted; 
    print join(' - ', $club, $loc, $www), "\n"; 
} 

を:もちろん

$ ./gg.pl 
AYSO United - Madison, AL - www.aysounitednorthalabama.org 

、私はおそらく利点を活用するためにHTML::TreeBuilder::XPathを使用しているだろうXPathクエリの

1

ここにはMojoliciousの例があります。 Sinan didと同じことですが、ウェブページを取得して処理するツールを備えた別のツールボックスを使用しています。それは少し長く見えますが、それはコメントとドキュメントです。;)

私はおそらく私が仕事のために必要な他のすべてを持って、私はモジュールの一つにロードかつてのでMojoliciousは、「付属の電池」であることを好む:

use v5.10; 
use Mojo::UserAgent; 

my $url = "http://home.gotsoccer.com/clubs.aspx?&clubname=&clubstate=AL&clubcity="; 
my $ua = Mojo::UserAgent->new; 

my $tx = $ua->get($url); 

    # You could do some error checking here in case the fetch fails 
$tx->res->dom 
     # there are lots of ClubRow td cells, but we want the one with 
     # the width attribute. Find all of those. See Mojo::DOM::CSS for 
     # docs on CSS selectors. 
    ->find('td[class=ClubRow][width=80%]') 
     # now go through each td and extract several things 
    ->map(sub { 
      # these selectors represent the club location, name, and website 
     state $find = [ qw(
      a[class=ClubLink] 
      div[class=SubHeading] 
      font[color=black] 
      ) ]; 
     my $chunk = $_; 

      # return the location, name, and link as a tuple for later 
      # processing 
     [ 
      map { s/\t+/ /gr } # remove tabs so we can use them as a separator 
      map { $chunk->find($_)->map('text')->[0] } 
      @$find 
     ] 
     }) 
     # do something will all tuples. In this case, output them as tab 
     # separated values (which is why you removed tabs already). You 
     # should be able to easily import this into a spreadsheet application. 
    ->each(sub { say join "\t", @$_ }); 

出力がその迷惑な最​​初の行を持っていますあなたはそれをあなた自身で修正することができます:

*****Other Club***** 
Alabama Soccer Association  www.alsoccer.org 
Alabaster Competitive SC  acsc.teampages.com/ 
Alabaster Parks and Rec 
Alex City YSL  www.alexcitysoccer.com/ 
Auburn Thunder SC  auburnthundersoccer.com/ 
AYSO United Madison, AL www.aysounitednorthalabama.org 
Birmingham Area Adult Soccer League 
Birmingham Bundesliga LLC Birmingham, AL www.birmingham7v7.com 
Birmingham Premier League 
Birmingham United SA Birmingham, AL, AL www.birminghamunited.com/ 
Blount County Youth Soccer Oneonta, AL bcysfury.com 
Briarwood SC Birmingham, AL www.questrecreation.org/briarwood-soccer-club.html... 
Capital City Streaks Montgomery, AL www.capitalcitystreaks.org 
City of Calera Youth Soccer