LoginSignup
5
4

More than 3 years have passed since last update.

Readabilityで本文っぽい文章を抽出

Posted at

Readabilityのインストール

まずはReadabilityとhtml2textをインストール。

pip install readability-lxml
pip install html2text
  1. Readabilityで本文抽出
  2. html2textでmarkdown形式に変換し読みやすくする

Readabilityを使う

python.py
# ライブラリをimport
from readability.readability import Document
import urllib.request
import html2text

# 実行
url = "https://flatclub.jp/shinobiwriting/"
html = urllib.request.urlopen(url).read()
# 本文っぽい部分を抽出
article = Document(html).summary()
# htmlからmarkdown形式に変換
text = html2text.html2text(article)
# とりあえずコマンドラインに出力
print(text)

抽出完了。

参考

HTMLから本文抽出。 - ねこゆきのメモ
Python – readability-lxmlを利用して本文抽出|Miningoo

5
4
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
5
4