Regex 除去完整的锚标记-Perl之外的所有内容

Regex 除去完整的锚标记-Perl之外的所有内容,regex,bash,perl,curl,grep,Regex,Bash,Perl,Curl,Grep,我需要解析一个HTML文件并删除除锚定标记之外的所有内容。例如: <html> <body> <p>boom</p> <a href="/blah" rel="no-follow">Example</a> </body> </html> 我只需要保持: <a href="/blah" rel="no-follow">Example</

我需要解析一个HTML文件并删除除锚定标记之外的所有内容。例如:

<html>
    <body>
        <p>boom</p>
        <a href="/blah" rel="no-follow">Example</a>
    </body>
</html>
我只需要保持:

<a href="/blah" rel="no-follow">Example</a>
我正在使用cURL检索html和我发现的一小段代码,这些代码除去了标记的锚文本之外的所有内容。这就是我正在使用的:

curl http://www.google.com 2>&1 | perl -pe 's/\<.*?\>//g'
有没有一种简单的命令行方法可以做到这一点?我的最终目标是将其放入bash脚本并执行它。我很难理解正则表达式和perl。

使用命令行工具:

产出:

<a class="gb1" href="http://www.google.com/imghp?hl=en&amp;tab=wi">Images</a>
<a class="gb1" href="http://maps.google.com/maps?hl=en&amp;tab=wl">Maps</a>
<a class="gb1" href="https://play.google.com/?hl=en&amp;tab=w8">Play</a>
<a class="gb1" href="http://www.youtube.com/?tab=w1">YouTube</a>
<a class="gb1" href="http://news.google.com/nwshp?hl=en&amp;tab=wn">News</a>
<a class="gb1" href="https://mail.google.com/mail/?tab=wm">Gmail</a>
<a class="gb1" href="https://drive.google.com/?tab=wo">Drive</a>
<a class="gb1" href="http://www.google.com/intl/en/options/" style="text-decoration:none"><u>More</u> »</a>
<a class="gb4" href="http://www.google.com/history/optout?hl=en">Web History</a>
<a class="gb4" href="/preferences?hl=en">Settings</a>
<a class="gb4" href="https://accounts.google.com/ServiceLogin?hl=en&amp;continue=http://www.google.com/" id="gb_70" target="_top">Sign in</a>
<a href="/chrome/index.html?hl=en&amp;brand=CHNG&amp;utm_source=en-hpp&amp;utm_medium=hpp&amp;utm_campaign=en" onclick="google.promos&amp;&amp;google.promos.toast&amp;&amp; google.promos.toast.cl()">Install Google Chrome</a>
<a href="/advanced_search?hl=en&amp;authuser=0">Advanced search</a>
<a href="/language_tools?hl=en&amp;authuser=0">Language tools</a>
<a href="http://www.google.com/chrome/devices/index.html" onclick="google.promos&amp;&amp;google.promos.link&amp;&amp; google.promos.link.cl()">Chromebook: For students</a>
<a href="/intl/en/ads/">Advertising Programs</a>
<a href="/services/">Business Solutions</a>
<a href="https://plus.google.com/116899029375914044550" rel="publisher">+Google</a>
<a href="/intl/en/about.html">About Google</a>
<a href="/intl/en/policies/">Privacy &amp; Terms</a>

有关8分钟的介绍性视频,请查看:

如果您希望对HTML进行更精细的控制,则可以使用CPAN上提供的模块

use strict;
use warnings;
use HTML::TagParser;

my $html = HTML::TagParser->new( '<html>
    <body>
        <p>boom</p>
        <a href="/blah" rel="no-follow">Example</a>
    </body>
</html>' );

my @list = $html->getElementsByTagName( "a" );

for my $elem ( @list ) {
    my $name = $elem->tagName;
    my $attr = $elem->attributes;
    my $text = $elem->innerText;
    print "<$name";
    for my $key ( sort keys %$attr ) {
        print " $key=\"$attr->{$key}\"";
    }
    print $text eq "" ? " />" : ">$text</$name>" , "\n";
}
输出:
使用Mojolicious,如上文@Miller所述,但更准确地选择Ingy döt Net值得一提:

perl -MpQuery -E 'pQuery("http://www.ubu.com/sound/barthes.html")
 ->find("a")->each(sub{say pQuery($_)->toHtml})'
只是链接:

perl -MpQuery -E 'pQuery("http://www.ubu.com/sound/barthes.html")
  ->find("a")->each(sub{say $_->{href}})'

虽然-与mojo不同-没有命令行工具,也就是说,还没有-它本身不是那种工具,并且还在构建中,但它是一个模块,可以放在您的观察列表上。

不太清楚为什么会出现这种情况我怀疑DV是因为用正则表达式解析HTML被误导了,相反,应该使用实际的HTML解析器。不过别担心,mojo命令行工具还允许用户使用更详细的搜索。下面将提取与类链接中的所有google ssl HREF:mojo gethttp://www.google.com 'a[class][href^=https]'attr href
perl -Mojo -E 'say $_ for x(b("my.html")->slurp)->find("a[rel]")->each'
perl -Mojo -E 'say $_ for g("http://example.com")->dom->find("a[rel]")->each'
#or
perl -Mojo -E 'g("http://example.com")->dom->find("a[rel]")->each(sub{say $_})'
perl -MpQuery -E 'pQuery("http://www.ubu.com/sound/barthes.html")
 ->find("a")->each(sub{say pQuery($_)->toHtml})'
perl -MpQuery -E 'pQuery("http://www.ubu.com/sound/barthes.html")
  ->find("a")->each(sub{say $_->{href}})'