[經驗總結]Perl提取網頁資訊

來源:互聯網
上載者:User

#!/usr/bin/perl -w# Gist: https://gist.github.com/2928006use LWP::Simple;my $url=$ARGV[0];my $filename =$ARGV[1];  my $content = get($url)     or die "Couldn't get $url";
#$content =~ s#^.*?(<div.*?</div>).*$##m;
if ($content =~ m#.*(<div id="enText" style="display:block">.*?</div>).*#sg) {    $text = $1;
    # 開啟模板檔案     open(TEMPLATE, "template.html")            or die " Couldn't open template.html for writing: $! " ;
    # 讀模數板檔案     $/="</html>"; # 讀到</html>結束    my $reads = <TEMPLATE>;
    # 替換聽力文本     $reads =~ s/==TEXT_CONTENT==/$text/gix;    #print $reads;
    # 輸出html檔案     open (OUT , " > $filename " )            or die " Couldn't open $filename for writing: $! ";    print OUT $reads;
    # 下載聽力     my $baseUrl = $url;    $baseUrl =~ s/(.*)(//.*/.html)/$1/g;     my $reslink = $content;    $reslink =~ s/.*<a href="(.*?)" title="進入下載資料頁面">下載聽力<//a>.*/$1/sg;    $reslink = $baseUrl . "/" . $reslink;
    print "/nreslink:",$reslink,"/n";    my $respage = get($reslink)         or die "Couldn't get $reslink";
    my $mp3link = $respage;    print $mp3link;#$mp3link =~ s#.*<a href="(.*?)" target="_blank"><img src="/images/downloadurl1/.jpg"></a>.*#$1#sg;#if ($mp3link =~ m/.*<a href="(.*?)" target="_blank"><img src="//images//downloadurl1/.jpg"><//a>.*/sg) {
    # 沒登入,下載連結擷取不到,怎麼辦?    if ($mp3link =~ m/downloadurl1/sg) {        print "匹配/n";    }    else {        print "不匹配/n";    }    #<a href="(.*?)" target="_blank"><img src="/images/downloadurl1.jpg"></a>
    print "/ndownload:".$reslink."/n";}else {    print "不匹配/n";}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.