特定のタグを使用してHTMLファイルから複数行の文字列を抽出する

特定のタグを使用してHTMLファイルから複数行の文字列を抽出する

<span class="style530">tagで始まり、tagで終わる文字列を抽出する必要があります</span>

sedコマンドを使用しましたが、目的の結果が得られませんでした。サンプルコードは次のとおりです。

<strong>
-
<span class="style530">
AA - 
This
is my
First
Heading</span></strong><br>
<span class="style530">
<strong>
*Some
text,*
<strong>
*text*</strong>, 
*text*
<strong>
*text*</strong>: 
<br>
<span class="style530">
<strong>
- This 
is my
Second Heading</strong></span><br>
<span class="style530">
<strong>
*Some
text,*
<strong>
*text*</strong>, 
*Here
is some
text.*
<strong>*text*</strong>: 
*Here is 
some
text*.<br>
<br>
<strong>
-
<span class="style530">
- This is
my Third
Heading</span></strong><br>

出力は次のようになります。

 AA - This is my First Heading
 - This is my Second Heading
 - This is my Third Heading

ありがとうございます!

ベストアンサー1

正規表現は実際にHTMLを完全に解析しません。

というコマンドラインツールがあります。シデルXPathまたはCSSセレクタを使用して、目的の部分を抽出できます。

次のようなことはあなたが言うことをするでしょう:

./xidel test.html --extract '//span[@class="style530"]' --output-format bash

ただし、閉じられていない出力があるため、必要以上の出力が返されることに注意してください。<span class="style530">

おすすめ記事