LoginSignup
1
2

More than 5 years have passed since last update.

青空文庫のXHTMLファイルからPython3でルビなしプレーンテキストを作る

Last updated at Posted at 2019-02-17

必要なもの

  • Python3(Python 3.7.2で試しました。)
  • lxmlライブラリ

やり方

  1. 青空文庫からXHTMLファイルをダウンロードする。
  2. pip install lxmllxmlライブラリをインストールする。
  3. 下のsanitize.pyを走らせる。

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型に翻弄された話

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2