如何在匹配后提取特定 HTML 标签的内容?
How to extract contents of specific HTML tag following a match?
我想知道如何提取 HTML、
中超链接的内容
例如:
<article id="post36">
<div>
<h3><a href="/blog/2019/4-14-canaries-in-the-coal-mine.html">Canaries in the Coal Mine</a></h3>
<p class="author">Posted by <a href="/blog/authors/moderator.html" rel="author">Moderator</a></p>
<p><time><span>Sunday, April 14th, 2019</span> — 8:17AM</time></p>
</div>
其他 post 看起来像这样(没有外部页面):
<article id="post33">
<div>
<h3><a href="#post33">Landlines Win Again</a></h3>
<p class="author">Posted by <a href="/blog/authors/moderator.html" rel="author">Moderator</a></p>
<p><time><span>Friday, December 21st, 2018</span> — 7:14AM</time></p>
在外部脚本中,我收到了特定 post 的 ID。在这种情况下,post 36 在下面。我有一个页面包含文章标签中的所有 post 元数据,如下所示。
我尝试使用 catting 网页(我有一个本地副本)并将其通过管道传输到 sed -n 's|[^<]*<article\([^<]*\)</article>[^<]*|\n|gp'
那种作品。它只有returns所有的文章id,像这样:
<article id="post6">
<article id="post5">
<article id="post4">
<article id="post3">
<article id="post2">
<article id="post1">
我的结论是它只适用于当前行。当我尝试实际使用 ID 时,我什么也没得到:sed -n 's|[^<]*<article id="post36">\([^<]*\)</article>[^<]*|\n|gp'
我的问题是如何利用内置的 Unix 工具(sed、grep、awk 等)来提取超链接?在这种情况下,我需要的是 /blog/2019/4-14-canaries-in-the-coal-mine.html
是的,我参考了很多 SO post,比如 this one and this one,其中大部分不鼓励这种事情(我尝试了本机解决方案,但 none 有效)。两件事:
- HTML 格式很好。代码中永远不会有任何额外的空格、回车 returns 或任何其他内容。这些块将始终看起来像那样。这是一个非常具体的应用程序。
- 除非实际上不可能在没有某种附加或外部程序的情况下做到这一点,否则我想坚持使用基本的 Unix 工具。
您可以用 sed addresses 单选感兴趣的行。在这种情况下,正则表达式匹配 <a href
sed -nre '/h3.*href.*(#post[0-9]+|\/blog\/)/ s/.*<a href="([^"]+)".*//p' test.html
/blog/2019/4-14-canaries-in-the-coal-mine.html
#post33
要按文章 ID 匹配,请在 sed
命令前添加此内容
grep -A3 'article id="post36"' test.html | sed -nre '/h3.*href.*(#post[0-9]+|\/blog\/)/ s/.*<a href="([^"]+)".*//p'
我想知道如何提取 HTML、
中超链接的内容例如:
<article id="post36">
<div>
<h3><a href="/blog/2019/4-14-canaries-in-the-coal-mine.html">Canaries in the Coal Mine</a></h3>
<p class="author">Posted by <a href="/blog/authors/moderator.html" rel="author">Moderator</a></p>
<p><time><span>Sunday, April 14th, 2019</span> — 8:17AM</time></p>
</div>
其他 post 看起来像这样(没有外部页面):
<article id="post33">
<div>
<h3><a href="#post33">Landlines Win Again</a></h3>
<p class="author">Posted by <a href="/blog/authors/moderator.html" rel="author">Moderator</a></p>
<p><time><span>Friday, December 21st, 2018</span> — 7:14AM</time></p>
在外部脚本中,我收到了特定 post 的 ID。在这种情况下,post 36 在下面。我有一个页面包含文章标签中的所有 post 元数据,如下所示。
我尝试使用 catting 网页(我有一个本地副本)并将其通过管道传输到 sed -n 's|[^<]*<article\([^<]*\)</article>[^<]*|\n|gp'
那种作品。它只有returns所有的文章id,像这样:
<article id="post6">
<article id="post5">
<article id="post4">
<article id="post3">
<article id="post2">
<article id="post1">
我的结论是它只适用于当前行。当我尝试实际使用 ID 时,我什么也没得到:sed -n 's|[^<]*<article id="post36">\([^<]*\)</article>[^<]*|\n|gp'
我的问题是如何利用内置的 Unix 工具(sed、grep、awk 等)来提取超链接?在这种情况下,我需要的是 /blog/2019/4-14-canaries-in-the-coal-mine.html
是的,我参考了很多 SO post,比如 this one and this one,其中大部分不鼓励这种事情(我尝试了本机解决方案,但 none 有效)。两件事:
- HTML 格式很好。代码中永远不会有任何额外的空格、回车 returns 或任何其他内容。这些块将始终看起来像那样。这是一个非常具体的应用程序。
- 除非实际上不可能在没有某种附加或外部程序的情况下做到这一点,否则我想坚持使用基本的 Unix 工具。
您可以用 sed addresses 单选感兴趣的行。在这种情况下,正则表达式匹配 <a href
sed -nre '/h3.*href.*(#post[0-9]+|\/blog\/)/ s/.*<a href="([^"]+)".*//p' test.html
/blog/2019/4-14-canaries-in-the-coal-mine.html
#post33
要按文章 ID 匹配,请在 sed
命令前添加此内容
grep -A3 'article id="post36"' test.html | sed -nre '/h3.*href.*(#post[0-9]+|\/blog\/)/ s/.*<a href="([^"]+)".*//p'