解析时我没有收到 HTML 标签
I'm not getting HTML tag while parsing
我要解析的HTML代码片段是这样的:
<ul class="authors">
<li class="author" itemprop="author" itemscope="itemscope" itemtype="http://schema.org/Person">
<a href="/search?facet-creator=%22Charles+L.+Fefferman%22" itemprop="name">Charles L. Fefferman</a>,
</li>
<li class="author" itemprop="author" itemscope="itemscope" itemtype="http://schema.org/Person">
<a href="/search?facet-creator=%22Jos%C3%A9+L.+Rodrigo%22" itemprop="name">José L. Rodrigo</a>
</li>
我想提取整个 <a>
元素,但是当我试图用 WWW::Mechanize::TreeBuilder
解析它时,我得到的唯一内容是作者的名字。所以:
我期待的内容:
<a href="/search?facet-creator=%22Charles+L.+Fefferman%22" itemprop="name">Charles L. Fefferman</a>,
<a href="/search?facet-creator=%22Jos%C3%A9+L.+Rodrigo%22" itemprop="name">José L. Rodrigo</a>
我收到的内容:
Charles L. Fefferman,
José L. Rodrigo
下面是负责解析的代码:
my $mech = WWW::Mechanize->new();
WWW::Mechanize::TreeBuilder->meta->apply($mech);
$mech->get($addressdio);
my @authors = $mech->look_down('class', 'author');
print "Authors: <br />";
foreach ( @authors ) {
say $_->as_text(), "<br />";
}
我认为这可能与 as_text()
有关,而且当 CGI 获得 HTML 时,它不会将其作为文本。
我处理了它,但方式完全不同 - 使用 HTML::TagParser:
my $html = HTML::TagParser->new("overwrite.xml");
my @li = $html->getElementsByAttribute('class','author');
foreach(@li){
my $a = $_->firstChild();
my $link = $a->getAttribute('href');
say $_->innerText;
say $link;
}
我要解析的HTML代码片段是这样的:
<ul class="authors">
<li class="author" itemprop="author" itemscope="itemscope" itemtype="http://schema.org/Person">
<a href="/search?facet-creator=%22Charles+L.+Fefferman%22" itemprop="name">Charles L. Fefferman</a>,
</li>
<li class="author" itemprop="author" itemscope="itemscope" itemtype="http://schema.org/Person">
<a href="/search?facet-creator=%22Jos%C3%A9+L.+Rodrigo%22" itemprop="name">José L. Rodrigo</a>
</li>
我想提取整个 <a>
元素,但是当我试图用 WWW::Mechanize::TreeBuilder
解析它时,我得到的唯一内容是作者的名字。所以:
我期待的内容:
<a href="/search?facet-creator=%22Charles+L.+Fefferman%22" itemprop="name">Charles L. Fefferman</a>,
<a href="/search?facet-creator=%22Jos%C3%A9+L.+Rodrigo%22" itemprop="name">José L. Rodrigo</a>
我收到的内容:
Charles L. Fefferman,
José L. Rodrigo
下面是负责解析的代码:
my $mech = WWW::Mechanize->new();
WWW::Mechanize::TreeBuilder->meta->apply($mech);
$mech->get($addressdio);
my @authors = $mech->look_down('class', 'author');
print "Authors: <br />";
foreach ( @authors ) {
say $_->as_text(), "<br />";
}
我认为这可能与 as_text()
有关,而且当 CGI 获得 HTML 时,它不会将其作为文本。
我处理了它,但方式完全不同 - 使用 HTML::TagParser:
my $html = HTML::TagParser->new("overwrite.xml");
my @li = $html->getElementsByAttribute('class','author');
foreach(@li){
my $a = $_->firstChild();
my $link = $a->getAttribute('href');
say $_->innerText;
say $link;
}