2012-08-08 54 views
1

我正在使用下面的代碼來獲取網頁內容。它工作正常,但我想要獲得特定的線路。任何幫助是極大的讚賞。如何只從Perl中的網頁內容中獲取特定行

use strict; 
use warnings; 
use LWP::Simple; 

my $content = get('http://www.w3schools.com/'); 
print $content; 
my @arr; 
my $flag = 0; 
push (@arr, $content); 
#print @arr; 

my $find = "HTML 4.01"; 
for (@arr) 
{ 
    if ($_ =~ /$find/) 
    { 
    print "$_\n"; 
    print "passed\n"; 
    $flag = 1; 
    } 
} 

if ($flag == 1) 
{ 
    print "Testcase passed"; 
} 
else 
{ 
    die "Testcases fails"; 
} 
+0

my $ flag = $ content =〜/ $ find/i; – cdtits 2012-08-08 07:24:34

回答

1

get將整個內容作爲單個值提取。如果要按行處理,請在line endings上首先處理split

use strict; 
use warnings; 
use LWP::Simple qw(get); 

for my $line (split qr/\R/, get('http://www.w3schools.com/')) { 
    print $line if $line =~ /\QHTML 4.01/; 
} 
+0

非常感謝您的幫助:-) – user1583773 2012-08-09 07:05:07

+0

user1583773,在堆棧溢出之一表示感謝[▲投票](http://stackoverflow.com/privileges/vote-up)和[✔接受](http:// meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)的答案。 – daxim 2012-08-09 09:45:21

+0

任何人都可以解釋什麼分裂qr/\ R /在技術上做?什麼是qr? – Memoc 2018-02-23 03:35:41