知乎看妹纸攻略

Knowledge is power ————Francis Bacon

第一步,查询相关问题

第一步:搜出相关问题

第二步,找到问题id、请求地址、请求参数

第二步,找到问题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"
    }
}

第五步,No pictures . No Truth

图片下载中
部分图片
全部图片

参考文献

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容