preface

Recently all kinds of answer games fire, office play is not a few people, but also a variety of auxiliary. The Internet to find a time, mostly Python, PHP to write, think of their own based on node also write a try.

knowledge

Js Well, so easy

Train of thought

V1.0 get questions -> baidu take a look ^_^ -> return to processed answers -> select one

Get the title

AnyProxy documentation can be used once read through. Here I use it as an NPM module. Applets are HTTPS and require a certificate. * Some mobile phone installation certificates need to be installed in system security and network connection advanced Settings. After a wave of operations, open localhost:8002 and see the picture below. Congratulations, a little bit of success.

/question/bat/findQuiz

// Print the interface data
 *beforeSendResponse(requestDetail, responseDetail) {
    if(requestDetail.url.indexOf('https://question-zh.hortor.net/question/bat/find') = =0) {let body = JSON.parse(responseDetail.response.body.toString())
      console.log(body)
      returnresponseDetail; }},Copy the code

The baidu

* If you want to search for puppeteer, use noscript, try it, or even puppeteer. * If you want to search for Puppeteer, use noscript. However, it may not be successful to install tens of megabytes of Chromium, without ladders. But we are based on learning, can do their own move. A simple search link on your browser, https://www.baidu.com/s?ie=UTF-8&wd=mars, looks something like this, Mars is the search term, go to the console, and look at the data in the request header.

// So the option of our request is roughly as follows
let reqOptions = {
    hostname: 'www.baidu.com'.method: 'GET'.headers: 
        { 
            Connection: 'keep-alive'.'User-Agent': 'Mozilla/5.0, etc. '.'Accept-Encoding': 'gzip, deflate, br'.Cookie: 'A very long thing'}};Copy the code

To use async/await next, encapsulate a sendReq method. There is also a brief introduction to Buffer. The result of the request is that the Buffer data needs to be processed

function sendReq(reqOptions,quiz){
  const path = `/s? wd=The ${encodeURI(quiz)}` // We need encodereqOptions = {... reqOptions,path}return new Promise(resolve= >{
    callback = (res) = > {
      let bufferList=[];
      res.on('data',(d)=>{
          bufferList.push(d)
      });
      res.on('end', () = > {let buffer = Buffer.concat(bufferList);
        zlib.gunzip(buffer, (err, decoded)=> {
          resolve(decoded.toString());
          console.log(decoded.toString())
        })
      });
    }
    letreq = https.request(reqOptions,callback); req.end(); })}Copy the code

Run down sendReq(reqOptions, Mars) and see what follows. Congratulations on another bit of success.

The processing results

Take the Quzi you got in step 1 and put it in sendReq part 2 and use the Cheerio. Popular point is to get the topic and then go to Baidu, and then statistics under the topic option in the search results of the number of occurrences.

. let content =yield sendReq(reqOptions,body.data.quiz)
const $ = cheerio.load(content, { decodeEntities: false });
const resList = $('.result');
options.forEach((answer,i) = > {
    let time = 0;
    resList.each((index, result) = >{
      if($(result).text().indexOf(answer)>=0){
        time++
      }
    })
    options[i] = `${answer}[${time}] `
});
body.data.options = options
body=new Buffer(JSON.stringify(body))   // Restore the data type
responseDetail.response.body = body
returnresponseDetail; .Copy the code

The final result

Generally choose the most, baidu also does not know however when, as to negate the topic of the class you need artificial intelligence processing to choose the least, do not do further processing here

conclusion

This is the end of this simple assist. You’ll find there’s a lot of room for improvement

  • Concurrent search, Baidu is not good, Google search bing together to statistical results
  • Automatically submit the answer. Interface 2 in the figure above is the interface for submitting the answer. We can directly initiate the answer request after receiving the result, and the full score is not too easy.
  • Local question bank, establish local question bank, check local question bank first after getting the question, have record direct answer, no record search engine again.
  • , etc.

Other questions of the auxiliary basic can be achieved according to this idea as an answer application can also do more processing from the above links, to prevent the emergence of auxiliary. Ps recently seems to be a cash match, anyway I dare not play. Maybe the opposite is alpha dog PS was blocked after the data transfer to zhihu answer king, long time did not pay attention to, recently have github questions update, change a topic interface address https://question into https://question-zh project address welcome star!! Welcome to star!!!!!! Welcome to star!!!!!!