必要なもの
- Python3(Python 3.7.2で試しました。)
- lxmlライブラリ
やり方
- 青空文庫からXHTMLファイルをダウンロードする。
-
pip install lxml
でlxml
ライブラリをインストールする。 - 下の
sanitize.py
を走らせる。
sanitize.pyのソース(クリックで展開)
import lxml.html
from lxml.html.clean import Cleaner
# ダウンロードしたXHTMLファイルのファイル名を書きます。
# ちなみに1567_14913.htmlは《走れメロス》です。
FILE_NAME = '1567_14913.html'
f = open(FILE_NAME, encoding='shift_jis')
data = f.read().encode('shift_jis')
f.close()
cleaner = Cleaner(page_structure=False, remove_tags=('ruby', 'br'), kill_tags=('rt', 'rp'))
cln_html = cleaner.clean_html(data).decode('utf-8')
plain_text = lxml.html.fromstring(cln_html).find_class('main_text')[0].text_content()
print(plain_text)
うまくいかない場合
下の記事も参考に、lxml.html.clean.Cleanerのremove_tags、kill_tagsを色々試してみて下さい。
Pythonのlxmlライブラリでencoding="Shift_JIS"のxhtmlを処理しようとしてstr型とbytes型に翻弄された話