簡體   English   中英

如何使用Perl提取HTML文件的特定部分

[英]How can I use Perl to extract a particular part of an HTML file

我是Perl的新手,我正在嘗試讀取HTML文件的<div class="one">之間的特定內容。

HTML檔案:

<div class="one">

    <div id="two">Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
    </div>

    <pre>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
    </pre>

</div>

Perl代碼:

my $file = "content.html";

if (-e $file) {
    open(IN, $file);
    while (<IN>) {
        chomp($line = $_);

        #print "$line\n";
    }
}

@contents = <IN>;

#check to if content in html file is in the right location,
#if content is in correct location (div class="one")
#print content in div two and three if exist

for (my $i = 0 ; $i <= $#contents ; $i++) {
    if (!$contents[$i] =~ m/^\s*<div/ && $contents[$i] =~ m/class\s*=\s*"one"/) {
        print "content in wrong location";
    }
    else {
        if ($contents[$i] =~ m/^\s*<div/) {
            print "$_";
        }
        else ($contents[$i] =~ m/^\s*<pre/) {
            print "$_";
        }
    }
}

使用HTML :: TreeBuilder取得了一些成功,它擅長處理損壞的HTML。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM