乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      NodeJS概述2-事件插件-簡易爬蟲

       路人甲Java 2020-08-19

      事件 events 模塊
      原生事件寫法

        /*     * 1. 事件分類      * DOM0級 事件  - on + eventType      * DOM2級 事件  - 事件監(jiān)聽    * 2. 事件構(gòu)成部分有哪些?    dom.onclick = function () {}      * 事件源      * 事件類型  click change ...       * 事件處理程序    * 3. 事件綁定形式有哪些?      *  dom.onclick = function () {}      * 事件監(jiān)聽   dom.addEventListener('click',function(){},false)      * 元素綁定 <div onclick = "load()"></div>  */

      Node.js 事件驅(qū)動(dòng)

      1. 問題: Node.js中有DOM嗎?

        • 沒有
        • 結(jié)論: 原生js DOM 事件都不能用
      2. 創(chuàng)建了一個(gè)叫做 events 內(nèi)置模塊來解決這個(gè)問題

      const events= require('events');//events.EventEmitter//構(gòu)造函數(shù)console.log(events.EventEmitter.prototype)//原型鏈/*  EventEmitter {    _events: undefined,    _eventsCount: 0,    _maxListeners: undefined,    setMaxListeners: [Function: setMaxListeners],    getMaxListeners: [Function: getMaxListeners],    emit: [Function: emit],    addListener: [Function: addListener],    on: [Function: addListener],    prependListener: [Function: prependListener],    once: [Function: once],    prependOnceListener: [Function: prependOnceListener],    removeListener: [Function: removeListener],    off: [Function: removeListener],    removeAllListeners: [Function: removeAllListeners],    listeners: [Function: listeners],    rawListeners: [Function: rawListeners],    listenerCount: [Function: listenerCount],    eventNames: [Function: eventNames]  }*/const archetype=events.EventEmitter.prototype;// archetype.on(事件,事件處理函數(shù))  作用發(fā)布// archetype.emit(事件名,實(shí)際參數(shù))     作用訂閱archetype.on('handler',(val)=>{console.log('事件觸發(fā)',val);})archetype.emit('handler',111)

      Readline模塊逐行讀取文本內(nèi)容
      readline 模塊提供了一個(gè)接口,用于一次一行地讀取可讀流(例如 process.stdin)中的數(shù)據(jù)。

      const readline = require('readline');const rl = readline.createInterface({  input: process.stdin,  output: process.stdout});rl.question('你如何看待 Node.js 中文網(wǎng)?', (answer) => {  // TODO:將答案記錄在數(shù)據(jù)庫中。  console.log(`感謝您的寶貴意見:${answer}`);  rl.close();});
      const readline = require('readline');const fs = require('fs');const rl = readline.createInterface({  input: fs.createReadStream('sample.txt')});rl.on('line', (line) => {  console.log('Line from file:', line);});

      簡易爬蟲

      /*   * 爬蟲    * 1. 進(jìn)行數(shù)據(jù)請求,獲取網(wǎng)頁內(nèi)容       http    * 2. 進(jìn)行數(shù)據(jù)分析、數(shù)據(jù)清洗     * 3. 發(fā)送給我們自己的網(wǎng)頁*/
      const http=require('http')//獲取 JSON 的示例:http.get('http://jsonplaceholder./albums', (res) => {     /* res就是我得到的返回值 */  const { statusCode } = res;//狀態(tài)碼  const contentType = res.headers['content-type'];//得到的文件類型// 錯(cuò)誤代碼處理  let error;  if (statusCode !== 200) {    error = new Error('請求失敗\n' +                      `狀態(tài)碼: ${statusCode}`);  } else if (!/^application\/json/.test(contentType)) {    error = new Error('無效的 content-type.\n' +                      `期望的是 application/json 但接收到的是 ${contentType}`);  }  if (error) {    console.error(error.message);    // 消費(fèi)響應(yīng)數(shù)據(jù)來釋放內(nèi)存。    res.resume();//重新發(fā)起數(shù)據(jù)    return;  }  res.setEncoding('utf8');//中文編碼  let rawData = '';//真實(shí)數(shù)據(jù)  res.on('data', (chunk) => { rawData += chunk; });// 通過data事件將數(shù)據(jù)分片,然后逐片添加到rawData身上,好處就是當(dāng)我們執(zhí)行每一個(gè)分片的小任務(wù)時(shí),至少給其他任務(wù)提供了可執(zhí)行的機(jī)會(huì)  res.on('end', () => {//結(jié)束    try {// 高級編程 錯(cuò)誤捕獲      const parsedData = JSON.parse(rawData);      console.log(parsedData);    } catch (e) {      console.error(e.message);    }  });}).on('error', (e) => {  console.error(`出現(xiàn)錯(cuò)誤: ${e.message}`);});
      const http=require('http');const cheerio=require('cheerio')const options={    hostname: 'jx.',  port: 80,  path: '/teacher.php/Class/classDetail/param/rqiWlsefmajGmqJhXXWhl3ZiZ2Zn',  method: 'GET',  headers: {    Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',    'Accept-Encoding': 'gzip, deflate',    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',    'Cache-Control': 'no-cache',    Connection: 'keep-alive',    Cookie: 'PHPSESSID=ST-117984-IVZSfYMlr9YXvRfFRm-A1TimOeA-izm5ejd5j1npj2pjc7i3v4z',    Host: 'jx.',    Pragma: 'no-cache',    Referer: 'http://jx./teacher.php/Class/index',    'Upgrade-Insecure-Requests': 1,    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',    'Content-Type': 'application/x-www-form-urlencoded',    'Content-Length': 0  }}http.get(options, (res) => {  const { statusCode } = res;  const contentType = res.headers['content-type'];  res.setEncoding('utf8');  let rawData = '';  res.on('data', (chunk) => { rawData += chunk; });  res.on('end', () => {    try {const $=cheerio.load(rawData);$('.student a').each(function(item,index){    console.log($(this).text());    })          } catch (e) {      console.error(e.message);    }  });}).on('error', (e) => {  console.error(`出現(xiàn)錯(cuò)誤: ${e.message}`);});

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多