Perl 使用Html::语法分析器解析Html音频标记
我正在尝试用perl编写一个spider,它将解析域中的所有音频标记,并尝试从找到的每个音频标记下载相应的Perl 使用Html::语法分析器解析Html音频标记,perl,html-parsing,Perl,Html Parsing,我正在尝试用perl编写一个spider,它将解析域中的所有音频标记,并尝试从找到的每个音频标记下载相应的音频/mpeg内容 下面是我的代码片段,它使用HTML::TokeParser解析HTML,以便从a标记中提取链接: my($response, $base, $stream, $pageURL, $tag, $url); $response = 'http://example.com/page-with-some-audio-content'; $base = URI->new(
音频/mpeg
内容
下面是我的代码片段,它使用HTML::TokeParser
解析HTML,以便从a
标记中提取链接:
my($response, $base, $stream, $pageURL, $tag, $url);
$response = 'http://example.com/page-with-some-audio-content';
$base = URI->new( $response->base )->canonical;
$stream = HTML::TokeParser->new( $response->content_ref );
$pageURL = URI->new( $response->request->uri );
while($tag = $stream->get_tag('a')) {
next unless defined($url = $tag->[1]{'href'});
print $url."\n";
}
my($response, $base, $stream, $pageURL, $tag, $url);
$response = 'http://example.com/page-with-some-audio-content';
$base = URI->new( $response->base )->canonical;
$stream = HTML::TokeParser->new( $response->content_ref );
$pageURL = URI->new( $response->request->uri );
while($tag = $stream->get_tag('audio')) {
next unless defined($url = $tag->[1]{'onplaying'});
print $url."\n";
}
上面的代码片段从给定的html页面提取所有链接。这在循环中与URL哈希一起使用,以抓取给定域中的所有页面
下面是另一个片段,与第一个片段几乎完全相同,只是我试图提取音频
标签,而不是a
标签:
my($response, $base, $stream, $pageURL, $tag, $url);
$response = 'http://example.com/page-with-some-audio-content';
$base = URI->new( $response->base )->canonical;
$stream = HTML::TokeParser->new( $response->content_ref );
$pageURL = URI->new( $response->request->uri );
while($tag = $stream->get_tag('a')) {
next unless defined($url = $tag->[1]{'href'});
print $url."\n";
}
my($response, $base, $stream, $pageURL, $tag, $url);
$response = 'http://example.com/page-with-some-audio-content';
$base = URI->new( $response->base )->canonical;
$stream = HTML::TokeParser->new( $response->content_ref );
$pageURL = URI->new( $response->request->uri );
while($tag = $stream->get_tag('audio')) {
next unless defined($url = $tag->[1]{'onplaying'});
print $url."\n";
}
由于某些原因,未检测到音频
标签。有什么我遗漏的吗
阅读文档后,我发现我无法提取嵌套html元素的属性 请考虑以下标记:
<audio onplaying="podPress_html5_count('http://www.example.com/mp3/Some Mp3 File.mp3', this.id)">
<source src="http://www.example.com/mp3/Some%20Mp3%20File.mp3">
</audio>
预期输出应如下所示:
<body>
<audio onplaying="podPress_html5_count('http://www.example.com/mp3/Some Mp3 File.mp3', this.id)">
<source src="http://www.example.com/mp3/Some%20Mp3%20File.mp3">
</audio>
<audio onplaying="podPress_html5_count('http://www.example.com/mp3/Some Mp3 File 2.mp3', this.id)">
<source src="http://www.example.com/mp3/Some%20Mp3%20File%202.mp3">
</audio>
<audio onplaying="podPress_html5_count('http://www.example.com/mp3/Some Mp3 File 3.mp3', this.id)">
<source src="http://www.example.com/mp3/Some%20Mp3%20File%203.mp3">
</audio>
<audio onplaying="podPress_html5_count('http://www.example.com/mp3/Some Mp3 File 4.mp3', this.id)">
<source src="http://www.example.com/mp3/Some%20Mp3%20File%204.mp3">
</audio>
</body>
http://www.example.com/mp3/Some%20Mp3%20File.mp3
http://www.example.com/mp3/Some%20Mp3%20File%202.mp3
http://www.example.com/mp3/Some%20Mp3%20File%203.mp3
http://www.example.com/mp3/Some%20Mp3%20File%204.mp3
因此,我需要解析html文件,以仅提取每个音频
标记的src
属性
我不熟悉HTML::Token,但可以使用from轻松查找和提取具有熟悉CSS语法的链接:
use Mojo::DOM;
my $html = '<body> ... ';
my $dom = Mojo::DOM->new($html);
my @src = map { $_->{src} }
$dom->find('audio[onplaying] source[src]')->each;
使用Mojo::DOM;
我的$html='…';
my$dom=Mojo::dom->new($html);
my@src=map{$\->{src}
$dom->find('audio[onplaying]source[src]')->每个;
如果您需要从网络中获取HTML文件或音频文件,您也可以将其与结合使用。我担心该模块不支持HTML5。@choroba nooooh!!!!!那么有什么选择呢??X(因此
@src
数组应该包含所有音频标签的src
属性?@atefth:没错。你的例子对我很有用。