HTMLファイルからデータを取得するperlスクリプトを書いています。WWW::Mechanize
出力ファイルを使用して簡単にページに移動し、印刷できます。ただし、取得する必要があるデータは iframe タグであり、動的な src 値を持っています。
XML::Parser
Web サイトの XML API があるので、それを使用するアイデアも思いつきました。ただし、初心者のため、xmlリンクを取得する方法がわかりません。
だから私の質問は:
1回目:iframeタグからデータを表示・取得する方法
2 番目: Web サイトから 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>
...
必要なデータは、frame タグ内の src リンクにあります。
ありがとうございました。