私はLinuxの初心者です。HTMLページをテキストファイルに変換する方法を教えてください。テキストファイルは、Webページから画像とリンクを削除します。htmlからテキストへの変換ツールではなくbashコマンドのみを使用したい。例として、最初のページのグーグル検索結果を「コンピューター」に変換したいと思います。
ありがとうございました
私はLinuxの初心者です。HTMLページをテキストファイルに変換する方法を教えてください。テキストファイルは、Webページから画像とリンクを削除します。htmlからテキストへの変換ツールではなくbashコマンドのみを使用したい。例として、最初のページのグーグル検索結果を「コンピューター」に変換したいと思います。
ありがとうございました
最も簡単な方法は、このようなダンプを使用することです(つまり、表示可能なHTMLのテキストバージョンです)。
リモートファイル:
lynx --dump www.google.com > file.txt
links -dump www.google.com
ローカルファイル:
lynx --dump ./1.html > file.txt
links -dump ./1.htm
文字セットをutf8に変換する場合(を参照):
lynx -dump -display_charset UTF-8 ./1.htm
links -dump -codepage UTF-8 ./1.htm
コマンドラインにhtml2text.pyがあります。
使用法:html2text.py [(filename|url) [encoding]]
Options:
--version show program's version number and exit
-h, --help show this help message and exit
--ignore-links don't include any formatting for links
--ignore-images don't include any formatting for images
-g, --google-doc convert an html-exported Google Document
-d, --dash-unordered-list
use a dash rather than a star for unordered list items
-b BODY_WIDTH, --body-width=BODY_WIDTH
number of characters per output line, 0 for no wrap
-i LIST_INDENT, --google-list-indent=LIST_INDENT
number of pixels Google indents nested lists
-s, --hide-strikethrough
hide strike-through text. only relevent when -g is
specified as well
OSXでは、textutilというコマンドラインツールを使用して、htmlファイルをtxt形式にバッチ変換できます。
textutil -convert txt *.html
ubuntu/debianhtml2text
では良い選択です。http://linux.die.net/man/1/html2text
nodejsを取得して、モジュールhtml-to-textをグローバルにインストールできます。
npm install -g html-to-text
次に、次のように使用します。
html-to-text < stuff.html > stuff.txt
sedを使用する
sed -e 's/<[^>]*>//g' foo.html
リンクはこれを行うための最も一般的なツールだと思います。マンリンクを確認し、プレーンテキストなどを検索します。-ダンプは私の推測です、それも検索してください。このソフトウェアには、ほとんどのディストリビューションが付属しています。
私はpython-boilerpipeを使用しましたが、これまでのところ非常にうまく機能しています...
ローカルのhtmおよびhtmlファイルのバッチモード、lynx
必須
#!/bin/sh
# h2t, convert all htm and html files of a directory to text
for file in `ls *.htm`
do
new=`basename $file htm`
lynx -dump $file > ${new}txt
done
#####
for file in `ls *.html`
do
new=`basename $file html`
lynx -dump $file > ${new}txt
done
HTMLページをテキストファイルに再帰的に変換するBashスクリプト。httpd-manualに適用されます。grep -Rhi'LoadModule ssl' / usr / share / httpd / manual_dump-A10が便利に機能するようにします。
#!/bin/sh
# Adapted from ewwink, recursive html to txt dump
# Made to kind of recursively (4 levels) dump the /usr/share/httpd manual to a dump httpd manual directory into a txt dump including dir
# put this script in /usr/share/httpd for it to work (after installing httpd-manual rpm)
for file in ./manual/*{,/*,/*/*,/*/*/*}.html
do
new=`basename $file .html`
mkdir -p ./manual_dump/${new}
lynx --dump $file > ./manual_dump/${new}.txt
done
pandocツールはHTMLを(他のいくつかのマークアップ形式の中で)プレーンテキストに変換できます。ここでの回答では、他のいくつかのツールと比較して、テキストの形式を好みます。ブラウザのように、すべてをパックするのではなく、多くの空白を使用します。一緒にhtml2text
そうします。いくつかのツールを比較してから、1つを選択することをお勧めします。UbuntuまたはDebianにインストールするには:
sudo apt install pandoc
ファイルからの読み取りと書き込みのオプションがありますが、変換元と変換先の形式を指定するだけでよいパイプモードで使用するのが最も簡単です。
curl URL | pandoc -f html -t plain > output.txt