尝试使用Mojo :: DOM解析,但无法正确获取标签



我正在使用$ ua从我的$ url =" http://finance.yahoo.com/quote/msft?p=mmsft中获取一些html;

我可以从URL中获取HTML内容。然后,我使用Mojo :: Dom进行子解析,这是正确的步骤,对吗?我想从Mojo $ url get()HTML内容中进一步剥离HREF ...这就是我所拥有的:

my $ua = Mojo::UserAgent->new( max_redirects => 5, timeout => $timeout );
my $dom = Mojo::DOM->new;
my $content = $ua->get($url)->res->dom->at('div#quoteNewsStream-0-Stream')->content;
my $content2 = $content->$dom->find('a href#');

只需使用Mojo :: Useragent返回的Mojo :: DOM:

#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
use Mojo::UserAgent;
my $url = "http://finance.yahoo.com/quote/MSFT?p=MSFT";
my $dom = Mojo::UserAgent->new->get($url)->res->dom;
my $stream = $dom->at('div#quoteNewsStream-0-Stream');
for my $href ( $stream->find('a')->each ) {
    say $href->{href};
}

输出:

/news/jeff-bezos-trump-tech-summit-was-very-productive-224326329.html
/news/jeff-bezos-trump-tech-summit-was-very-productive-224326329.html
/news/donald-trump-tech-summit-at-trump-tower-202517070.html
/video/microsoft-surface-sales-surge-disappointment-181934121.html
/news/jeff-bezos-trump-tech-summit-was-very-productive-224326329.html
/news/microsoft-surface-sales-surge-on-disappointment-with-macbook-pro-163819168.html
/news/microsoft-surface-sales-surge-on-disappointment-with-macbook-pro-163819168.html
/m/7f581deb-0089-341a-b637-e1e979e9e210/ss_5-point-checklist-for.html

有关使用这些工具的8分钟教程,请查看Mojocast第5集

相关内容

  • 没有找到相关文章

最新更新