Skip to content

Instantly share code, notes, and snippets.

@autf
Last active October 17, 2021 10:36
Show Gist options
  • Save autf/45fff25789c054d75fa1bbf0aac98a87 to your computer and use it in GitHub Desktop.
Save autf/45fff25789c054d75fa1bbf0aac98a87 to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'open-uri'
base = 'https://ncode.syosetu.com'
ncode = 'x0000xx'
seen = i = 777
# start from #(seen + 1) chapter
p out = `date -Im`.strip
`mkdir #{out}`
chaps = ''
Nokogiri::HTML(URI.open("#{base}/#{ncode}"))
.css('dd')[seen..]
.each do |dd|
a = dd.at_css 'a'
href = a['href']
i = href[9..-2] # rewrite outer `i`
title = "第#{i}章·#{a.text}\n"
ch = title + Nokogiri::HTML(URI.open(base+href)).at_css('#novel_honbun').text.gsub('(・)', "\u0323")
open("#{out}/#{i}.txt", 'w') { _1 << ch }
chaps << ch
sleep 3
p [title, href]
end
txt = "#{ncode}-#{seen+1}..#{i}.txt"
open(txt, 'w') { _1 << chaps }
`zip #{txt}{.zip,}`
puts "#{i-seen} chaps downloaded!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment