使用grep獲取html標籤之間的內容

mohsinali1317

我有一個 html 文件,我試圖從中獲取數據。該網站是這個https://www.tv2.no/nyheter我正在嘗試從網站上獲取所有新聞文章。

我這樣做 wget -O news.html https://www.tv2.no/nyheter

這為我創建了一個本地文件。

然後我試圖獲取所有具有類文章的文章--nyheter。我嘗試運行此命令

tr '\n' ' ' < news.html | grep -E "^<article class="article-nyheter">.*$"

但我沒有得到任何結果。html結構是這樣的

<body>
<div>
    <article class="article column large-4 small-12">
        hello
    </article>
</div>

<article class="article column large-4 small-12 article--nyheter">

    <a class="article__link" href="/nyheter/14336304/">


        <figure class="image image__responsive" style="padding-bottom:51.312%;">

            <img class="image__img lazyload" itemprop="image" title="" alt=""
                src="data:image/gif;base64,R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7"
                data-src="https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=177"
                data-srcset="https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=688&height=354&compression=92 2x,https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=516&height=265.5&compression=92 1.5x,https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=177&compression=92 1x">


        </figure>


        <div class="article__content">


            <h2 class="article__title t27 tm26">IEA: Mulig å nå 2-gradersmålet om løftene fra Glasgow holdes</h2>



        </div>
    </a>



</article>

<article class="article column large-4 small-12 article--nyheter">

    <a class="article__link" href="/nyheter/14336420/">


        <figure class="image image__responsive" style="padding-bottom:115.452%;">

            <img class="image__img lazyload" itemprop="image" title="" alt=""
                src="data:image/gif;base64,R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7"
                data-src="https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=398"
                data-srcset="https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=688&height=796&compression=92 2x,https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=516&height=597&compression=92 1.5x,https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=398&compression=92 1x">


        </figure>


        <div class="article__content">


            <h2 class="article__title t26 tm20">Italienske jegere stoppet på vei ut av landet med 2.027 nedfryste
                troster</h2>



        </div>
    </a>



</article>

示例輸出,因為以下兩篇文章都包含類名文章--nyheter

<article class="article column large-4 small-12 article--nyheter">

    <a class="article__link" href="/nyheter/14336420/">


        <figure class="image image__responsive" style="padding-bottom:115.452%;">

            <img class="image__img lazyload" itemprop="image" title="" alt=""
                src="data:image/gif;base64,R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7"
                data-src="https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=398"
                data-srcset="https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=688&height=796&compression=92 2x,https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=516&height=597&compression=92 1.5x,https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=398&compression=92 1x">


        </figure>


        <div class="article__content">


            <h2 class="article__title t26 tm20">Italienske jegere stoppet på vei ut av landet med 2.027 nedfryste
                troster</h2>



        </div>
    </a>



</article>


<article class="article column large-4 small-12 article--nyheter">

    <a class="article__link" href="/nyheter/14336304/">


        <figure class="image image__responsive" style="padding-bottom:51.312%;">

            <img class="image__img lazyload" itemprop="image" title="" alt=""
                src="data:image/gif;base64,R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7"
                data-src="https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=177"
                data-srcset="https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=688&height=354&compression=92 2x,https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=516&height=265.5&compression=92 1.5x,https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=177&compression=92 1x">


        </figure>


        <div class="article__content">


            <h2 class="article__title t27 tm26">IEA: Mulig å nå 2-gradersmålet om løftene fra Glasgow holdes</h2>



        </div>
    </a>



</article>

為此,我必須使用 grep、sed、curl、awk。不能使用任何其他解析器。

所以我的預期輸出是獲得具有特定類的所有文章標籤。我想要那些文章標籤中的所有內容。

標記融合

假設:

  • 為什麼不使用以 HTML 為中心的工具來解析所需的部分是有正當理由的
  • 輸入的格式與問題相同,否則建議的sed解決方案可能無法正常工作
  • 提取<article> ... </article>其中對article class條目包含字符串article--nyheter
  • OP 的預期輸出article--nyheter以相反的順序列出了兩個部分;現在我將假設這是某種拼寫錯誤並且沒有要求對這兩個部分進行排序

sed使用範圍來提取所需數據的一種想法:

sed -n '/<article class.*article--nyheter/,/<\/article>/p' news.html

這會產生:

<article class="article column large-4 small-12 article--nyheter">

    <a class="article__link" href="/nyheter/14336304/">


        <figure class="image image__responsive" style="padding-bottom:51.312%;">

            <img class="image__img lazyload" itemprop="image" title="" alt=""
                src="data:image/gif;base64,R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7"
                data-src="https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=177"
                data-srcset="https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=688&height=354&compression=92 2x,https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=516&height=265.5&compression=92 1.5x,https://www.cdn.tv2.no/images/14336482.jpg?imageId=14336482&panox=0&panoy=0&panow=100&panoh=50.993377483444&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=177&compression=92 1x">


        </figure>


        <div class="article__content">


            <h2 class="article__title t27 tm26">IEA: Mulig å nå 2-gradersmålet om løftene fra Glasgow holdes</h2>



        </div>
    </a>



</article>
<article class="article column large-4 small-12 article--nyheter">

    <a class="article__link" href="/nyheter/14336420/">


        <figure class="image image__responsive" style="padding-bottom:115.452%;">

            <img class="image__img lazyload" itemprop="image" title="" alt=""
                src="data:image/gif;base64,R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7"
                data-src="https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=398"
                data-srcset="https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=688&height=796&compression=92 2x,https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=516&height=597&compression=92 1.5x,https://www.cdn.tv2.no/images/14336464.jpg?imageId=14336464&panox=0&panoy=0&panow=100&panoh=100&heighty=0&heightx=0&heightw=100&heighth=100&width=344&height=398&compression=92 1x">


        </figure>


        <div class="article__content">


            <h2 class="article__title t26 tm20">Italienske jegere stoppet på vei ut av landet med 2.027 nedfryste
                troster</h2>



        </div>
    </a>



</article>

如果輸入數據的格式不符合問題中的格式(例如,缺少回車/換行符),則此sed解決方案可能不起作用;需要構建一個更“健壯”的解析器(例如,通過awk)......

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

BeautifulSoup [python] 在特定點之後不從 <td> 標籤讀取內容

在android中獲取帶有Web視圖的內容html頁面

在 <p> 標籤內獲取 <a > 標籤內的文本

正則表達式在多行文本中獲取@ 和@ 之間的所有內容

有沒有辦法根據標題中的內容獲取 DataFrame 列索引號?

如何使用 Nokogiri 獲取沒有任何文本內容的完整 HTML

獲取 lambda 的內容

為什麼我的 html 代碼元素標籤內的內容沒有自己包裝?

奇怪的 HTML <style> 標籤,沒有內容但有樣式規則

如何刪除 HTML 標籤以及 PHP 字符串中的 HTML 內容?

如何在python中獲取td標籤內鏈接的某些部分

如何使單個標籤的內容之間相等?

在 python 中處理 <script> 標籤中的內容

獲取html頁面的python請求缺少文章元素內容

如何通過漂亮的湯 python 在標籤內獲取 html 文本

使用 Javascript Puppeteer 獲取標籤的內部文本

如何獲取文本不在 html 標籤之間,文本在 html 之間

如何提取兩個元素之間的 html 內容(Python、BeautifulSoup)

當使用 < 編碼代碼時,正則表達式匹配除 HTML 標籤之外的任何內容 和 >

使用 Apps 腳本獲取 Confluence 頁面的內容並解析其內容

如何在美麗的湯中獲取 div 的內部 html 及其子標籤?

使用 Javascript 獲取當前標籤頁 url

如何從標籤內的屬性獲取值

查找和替換html標籤之間的內容

如何獲取標頭內p標籤下的值的Xpath

獲取 HTML 內容並將其顯示為純文本

正則表達式只保留標籤之間的內容,但選擇所有內容

BeautifulSoup 獲取一行標籤之間的文本

如何從javascript中的字符串中刪除帶有內容的HTML標籤(不是特定標籤)