我正在编写一个从 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 链接中。
谢谢你。