Skip to content

Instantly share code, notes, and snippets.

@Neko288
Created December 12, 2021 03:18
Show Gist options
  • Save Neko288/18319593d24d6de105c4c7f22b038ac2 to your computer and use it in GitHub Desktop.
Save Neko288/18319593d24d6de105c4c7f22b038ac2 to your computer and use it in GitHub Desktop.
カクヨム(小説投稿サイト)の小説全話を.txtとして保存します。
import requests,re,winsound
from bs4 import BeautifulSoup
url = 'https://kakuyomu.jp/works/1177354054892010417'
res = requests.get(url)
soup = BeautifulSoup(res.content, "lxml")
page_url = soup.find_all('a', class_='widget-toc-episode-episodeTitle')
title = soup.find('title').text
all_page_url = []
for i in page_url:
all_page_url.append('https://kakuyomu.jp'+i.get('href'))
all_text = []
for i in all_page_url:
res_page = requests.get(i)
soup_page = BeautifulSoup(res_page.content, "lxml")
page_text = soup_page.find('div', class_='widget-episode js-episode-body-container')
kari_text = page_text.text
all_text.append(kari_text.replace('\n\u3000', ' '))
with open('C:/savepath'+title+'.txt', mode='w') as f:
f.write(' ')
for i in all_text:
with open('C:/savepath'+title+'.txt', mode='a',encoding='utf-8') as f:
f.write(i)
print('end')
winsound.Beep(294,800)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment