笔趣阁小说下载
2018-03-14 本文已影响16人
野小火
package downloadxiaoshuo
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.FileWriter
import java.io.StringReader
import java.io.StringWriter
val client = OkHttpClient()
fun genReq(nextSubUrl: String): Request {
return Request.Builder()
.url("http://www.biqukan.com/24_24845/$nextSubUrl")
.build()
}
fun main(args: Array<String>) {
var nextSubUrl = "8661600.html"
var article = 1
val writer = FileWriter("万剑道尊.txt")
// val buffer = StringWriter()
//
var count = 3
while (true) {
val string = client.newCall(genReq(nextSubUrl)).execute().body()?.string()
// println(string)
val sb = StringBuffer()
StringReader(string).readLines().forEach {
if (it.startsWith("var next_page =")) {
val start = it.indexOf("\"") + 1
val end = it.lastIndexOf("\"")
nextSubUrl = it.substring(start, end).replace("/24_24845", "")
print("第${article}篇:$nextSubUrl ")
article++
}
if (it.startsWith("<h1>")) {
val title = it.replace("<h1>", "")
.replace("</h1>", "")
print("标题:$title ")
sb.append(title).append("\n")
}
if (it.startsWith("<div id=\"content\" class=\"showtxt\">")) {
val text = it.replace("<div id=\"content\" class=\"showtxt\">", "")
.replace("</div>", "")
.replace("<br />", "\n")
.replace(" ", " ")
println("篇幅:${text.length}")
sb.append(text).append("\n").append("\n").append("\n").append("\n").append("\n").append("\n")
writer.write(sb.toString())
// writer.flush()
return@forEach
}
}
if (nextSubUrl.isBlank()) {
writer.flush()
return
}
}
}