【问题标题】:loading data from iframe tag in an html file using perl使用 perl 从 html 文件中的 iframe 标记加载数据
【发布时间】:2013-10-01 06:15:17
【问题描述】:

我正在编写一个从 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);

这是我的 out.htm 的一部分输出,框架 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 链接中。

谢谢。

【问题讨论】:

  • 我们需要一些例子
  • 什么是“动态 src 值”?
  • 如果有 API,那么您应该绝对使用它而不是抓取 HTML。 “获取 xml 链接”是什么意思?
  • @Quentin:我的意思是确定。

标签: javascript html xml perl iframe


【解决方案1】:

当您获得$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' )

【讨论】:

  • 其实outout更长,还有其他代码。
  • 解析器不会关心的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 1970-01-01
相关资源
最近更新 更多