1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

なろう小説全編をテキストで保存するpython

Posted at

暇だったのでなろう小説全編をテキストで保存するnarou_downloader.pyというコードを書きました。

  • 小説の目次のURLを指定すると、全編をダウンロードするbashを出力する
$ python narou_downloader.py https://ncode.syosetu.com/n7850ds
echo 'https://ncode.syosetu.com/n7850ds/1/' '第1話 石像に祈る少女' >&2
python narou_downloader.py https://ncode.syosetu.com/n7850ds/1/
sleep 1
echo 'https://ncode.syosetu.com/n7850ds/2/' '第2話 塔の迷宮' >&2
python narou_downloader.py https://ncode.syosetu.com/n7850ds/2/
sleep 1
echo 'https://ncode.syosetu.com/n7850ds/3/' '第3話 奇妙な面接' >&2
python narou_downloader.py https://ncode.syosetu.com/n7850ds/3/
sleep 1
echo 'https://ncode.syosetu.com/n7850ds/4/' '第4話 猛獣との戦い' >&2
python narou_downloader.py https://ncode.syosetu.com/n7850ds/4/
sleep 1
echo 'https://ncode.syosetu.com/n7850ds/5/' '第5話 ルームメイト' >&2
python narou_downloader.py https://ncode.syosetu.com/n7850ds/5/
sleep 1
echo 'https://ncode.syosetu.com/n7850ds/6/' '第6話 見習い魔導師の街' >&2
python narou_downloader.py https://ncode.syosetu.com/n7850ds/6/
sleep 1
(中略)
  • 小説の一話のURLを指定すると、一話の文章を出力する。タイトル部分はマークダウンの小見出しで表している
$ python narou_downloader.py https://ncode.syosetu.com/n7850ds/141
## 第141話 悪の誘い

フローラの裁判が行われた。

彼女の裁判はアルフルド中で話題の的となり、貧民から富裕な者まで住民という住民が法廷に詰め掛けて彼女にどのような判決が下るのか確かめようとした。
(中略)
  • パイプでつなげると全編をダウンロードする。標準エラーから進捗を確認できる
$ python narou_downloader.py https://ncode.syosetu.com/n7850ds | bash > NOVEL.md
https://ncode.syosetu.com/n7850ds/1/ 第1話 石像に祈る少女
https://ncode.syosetu.com/n7850ds/2/ 第2話 塔の迷宮
https://ncode.syosetu.com/n7850ds/3/ 第3話 奇妙な面接
https://ncode.syosetu.com/n7850ds/4/ 第4話 猛獣との戦い
https://ncode.syosetu.com/n7850ds/5/ 第5話 ルームメイト
https://ncode.syosetu.com/n7850ds/6/ 第6話 見習い魔導師の街
https://ncode.syosetu.com/n7850ds/7/ 第7話 魔導師協会
https://ncode.syosetu.com/n7850ds/8/ 第8話 魔法の工場
(中略)
  • 全編の小説を確認
$ cat NOVEL.md
## 第1話 石像に祈る少女

魔導師の街、グィンガルドは今日も大勢の人で賑わっていた。

港へと伸びる大通りには様々な職業や身分の人々が行き交い、その広い道路を溢れんばかりに埋め尽くしている。
(中略)
  • narou_downloader.py
    • python v3.12.3で確認
    • 標準ライブラリだけで動作
    • 100話以上ある小説については目次が1ページに収まらず2ページ目以降をダウンロードできない
      • 2ページ目以降の目次urlを指定しシェルスクリプトに追記すればいける

Reference

1
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?