2

我正在编写一个从 html 文件中获取数据的 perl 脚本。WWW::Mechanize我可以使用和打印输出文件轻松导航到页面。但是,我需要获取的数据是 iframe 标记,并且具有动态 src 值。

我也想出了一个想法,XML::Parser因为我有网站 XML API。但是,由于我的菜鸟,我不知道如何获取 xml 链接。

所以我的问题是:

1st:如何从 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);

这是框架 src 所在的 out.htm 的一部分输出。

...
<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 链接中。

谢谢你。

4

1 回答 1

2

当您得到显然$output_page只是iframe内容时,将该字符串发送到 HTML 解析器。像我的HTML::SimpleLinkExtor这样的东西可能对你有用。但是,我最近一直在使用Mojo::DOM来处理这些事情。还有“如何使用 Perl 的 Mojo::DOM 从文本中提取 iframe ”。

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' )
于 2013-10-01T14:08:45.417 回答