加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 我在解析时没有获得HTML标记

发布时间:2020-12-16 06:15:57 所属栏目:大数据 来源:网络整理
导读:我要解析的 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. Fefferma
我要解析的 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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读