Knowledge is power ————Francis Bacon
第一步,查询相关问题
第二步,找到问题id、请求地址、请求参数
第三步,解析响应结果
第四步,Talk is cheap.Show you my code
import java.util.concurrent.Executors
import groovy.json.JsonSlurper
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import org.jsoup.Jsoup
class KanMeizhi {
static main(args) {
def httpClient=new OkHttpClient()
def questionId=40554112
def threadPool=Executors.newFixedThreadPool(6)
def dir = new File("H:/images")
if(!dir.exists()){
dir.mkdirs()
}
def downloadImageClosure={imgSrc->
def fileName=imgSrc.substring(imgSrc.lastIndexOf('/')+1)+".jpg"
def file=new File(dir, fileName)
println "$fileName is being downloading ..."
new FileOutputStream(file).withStream {outputStream->
def inputStream=new URL(imgSrc).openStream()
outputStream.write(inputStream.getBytes())
}
println "$fileName has been downloaded O(∩_∩)O!"
}
def parseImageSrcClosure={document->
Jsoup.parse(document).getElementsByTag("img").findAll {img->
img.attr("src").startsWith("http")
}.collect {imgSrc->
def src=imgSrc.attr("src")
if(src.lastIndexOf("_")>0){
return src.substring(0, src.lastIndexOf("_"))
}
return src
}.unique().each { src->
threadPool.execute(new Runnable(){
void run() {
downloadImageClosure(src)
}
})
}
}
def offset=0
def pageSize=20
def total=0
while(true){
total++
def parms="{\"url_token\":$questionId,\"pagesize\":$pageSize,\"offset\":$offset}"
def formBody = new FormBody.Builder().add("_xsrf", "")
.add("method", "next").add("params", parms).build();
def request = new Request.Builder()
.url("http://www.zhihu.com/node/QuestionAnswerListV2")
.post(formBody).build()
def response = httpClient.newCall(request).execute()
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response)
} else{
def str=response.body().string()
def json= new JsonSlurper().parseText(str)
def msg=json?.msg
if(msg !=null&& msg.size()>0){
parseImageSrcClosure(msg.toString())
offset+=pageSize
}else{
break
}
}
}
threadPool.shutdown()
println "total page:$total"
}
}