2013-10-01 28 views
1

我在寫一個perl腳本,它從html文件中獲取數據。我可以使用WWW::Mechanize輕鬆導航到頁面並打印輸出文件。然而,我需要獲得的數據是一個iframe標籤,並具有動態的src值。使用Perl從html文件加載iframe標記的數據

我也想出了一個想法,使用XML::Parser,因爲我有網站的XML API。但是,由於我的noobness,我不知道如何獲得XML鏈接。

所以我的問題是:

第1頁:如何顯示或iframe標籤獲取數據

第二:如何獲得從一個網站XML鏈接。

這是我的代碼

#!/usr/bin/perl 
use strict; 
use warnings; 

use Getopt::Std; 
use XML::Simple; 
use WWW::Mechanize; 
use HTTP::Cookies; 
use LWP::Debug qw(+); 


my $username = $opt_u; 
my $password = $opt_p; 

my $outfile = "out.html"; 

my $url = "https://t-square.gatech.edu/portal"; 
my $mech = WWW::Mechanize->new(); 
$mech->cookie_jar(HTTP::Cookies->new()); 
$mech->get($url); 

$mech->follow_link(text => "Login", n => 1); 
$mech->submit_form(
    form_id=> 'fm1', 
    fields => { username => $username, 
       password => $password 
       }, 
    button => 'submit', 
); 
$mech->follow_link(text => "CS-2200-A,GR SUM13", n => 1); 
my $response = $mech->follow_link(text => "Assignments", n => 1); 
$response = $mech->get('https://t-square.gatech.edu/portal/tool/3a34f619-99d1-4548-be57-  9ee977fd8127?panel=Main'); 
my $content = $response->decoded_content(); 
my $parser = new XML::Simple; 
my $data = $parser->XMLin($content); 
print Dumper($data); 
my $output_page = $mech->content(); 
open(OUTFILE, ">$outfile"); 
print OUTFILE "$output_page"; 
close(OUTFILE); 

這裏是一塊從我out.htm輸出其中FRAME SRC位於的。

... 
<iframe name="Main3a34f619x99d1x4548xbe57x9ee977fd8127" 
    id="Main3a34f619x99d1x4548xbe57x9ee977fd8127" 
    title="Assignments " 
    class ="portletMainIframe" 
    height="475" 
    width="100%" 
    frameborder="0" 
    marginwidth="0" 
    marginheight="0" 
    scrolling="auto" 
    src="https://t-square.gatech.edu/portal/tool/3a34f619-99d1-4548-be57-9ee977fd8127?panel=Main">** 
</iframe> 
... 

我需要的數據是在幀標籤內的src鏈接。

謝謝。

+0

我們需要一些例子 – Suic

+0

什麼是「動態的SRC值」? – Quentin

+0

如果有一個API,那麼你應該*絕對*使用它來刮取HTML。 「獲取XML鏈接」是什麼意思? – Quentin

回答

2

當您得到$output_page(顯然只是iframe的內容)時,請將該字符串發送給HTML解析器。像我的HTML::SimpleLinkExtor可能會爲你工作。不過,最近我一直在使用Mojo::DOM這些東西。還有「How can I extract iframes from text with Perl's Mojo::DOM」。

use v5.10; 
use Mojo::DOM; 

my $html = ...; 

say "Src is ", Mojo::DOM->new($html)->find('iframe')->[0]->{src}; 

不過,既然你已經使用WWW::Mechanize,你應該能夠使用find_all_links

$mech->find_all_links(tag => 'iframe') 
+0

實際上,輸出更長,並且還有其他代碼。 – novo

+0

解析器不會在意。 –