溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

node爬取新型冠狀病毒的疫情實(shí)時(shí)動(dòng)態(tài)

發(fā)布時(shí)間:2020-09-26 09:33:43 來(lái)源:腳本之家 閱讀:138 作者:喜歡ctrl的cxk 欄目:web開(kāi)發(fā)

寫(xiě)在前面:

新型冠狀病毒有多么可怕,我想大家都已經(jīng)知道了。湖北爆發(fā)了新型冠狀病毒,湖南前幾天爆發(fā)了禽流感,四川發(fā)生地震,中國(guó)加油!昨天晚上我突發(fā)奇想地打算把疫情實(shí)時(shí)動(dòng)態(tài)展示在自建站上,于是說(shuō)干就干(先附上昨晚用puppeteer截的圖片)。 

node爬取新型冠狀病毒的疫情實(shí)時(shí)動(dòng)態(tài)

安裝node_modules:

所需的node_modules:①puppeteer;②cheerio;③fs;④cron。

需要注意的是安裝puppeteer的時(shí)候很容易安裝失敗,這里有倆個(gè)解決方法,都是用淘寶源(馬云爸爸不是白叫的😄)。

一、先將npm換成淘寶源再安裝:

npm config set registry http://registry.npm.taobao.org/
npm install -g cheerio
npm i -g puppeteer
npm i -g fs
npm i -g cron

二、用cnpm進(jìn)行安裝:

npm install cnpm -g --registry=https://registry.npm.taobao.org/
cnpm install -g cheerio
cnpm i -g puppeteer
cnpm i -g fs
cnpm i -g cron

具體操作:

用puppeteer爬?。?/p>

puppeteer本質(zhì)上是一個(gè)chrome瀏覽器,網(wǎng)頁(yè)很難分清這是人類(lèi)用戶(hù)還是爬蟲(chóng),我們可以用它來(lái)加載動(dòng)態(tài)網(wǎng)頁(yè)。

先來(lái)一個(gè)簡(jiǎn)單的例子,用puppeteer截圖:

const puppeteer = require('puppeteer');
 
(async () => {
 const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});  //啟動(dòng)瀏覽器實(shí)例
  /* puppeteer.launch()的可選參數(shù)如下:
  headless: 是否打開(kāi)瀏覽器,默認(rèn)為true
  ignoreHTTPSErrors: 是否忽略https錯(cuò)誤,默認(rèn)為true
  executablePath: 配置要調(diào)用瀏覽器的可執(zhí)行路徑,默認(rèn)是同Puppeteer一起安裝的Chromeium
  slowMo:指定的毫秒延緩Puppeteer的操作
  args:設(shè)置瀏覽器的相關(guān)參數(shù),比如是否啟動(dòng)沙箱模式“--no-sandbox”,是否更換代理“--proxy-server”,
  */
 const page = await browser.newPage();  //新建頁(yè)面
 await page.goto('https://ncov.dxy.cn/');  //訪(fǎng)問(wèn)目標(biāo)網(wǎng)頁(yè):丁香醫(yī)生
 await page.screenshot({  //進(jìn)行截圖
  path: 'p1.png',
  type: 'png',
  // quality: 100, 只對(duì)jpg有效
  // 指定區(qū)域截圖,clip和fullPage兩者只能設(shè)置一個(gè)
  // fullPage: true,
  clip: {
   x: 0,
   y: 0,
   width: 1000,
   height: 1000
  }
 });
 browser.close();  //關(guān)閉瀏覽器
})();

用puppeteer獲取網(wǎng)頁(yè)源代碼:

const puppeteer = require('puppeteer');
 
(async () => {
 const browser = await puppeteer.launch();
 const page = await browser.newPage();
 await page.goto('https://ncov.dxy.cn/');
 //page.frames() 獲取當(dāng)前頁(yè)面所有的 iframe,然后根據(jù) iframe 的名字精確獲取某個(gè)想要的 iframe
 const frame = await page.mainFrame();
 const bodyHandle = await frame.$('html');  //獲取所有的html
 //frame.evaluate()在瀏覽器中執(zhí)行函數(shù),相當(dāng)于在控制臺(tái)中執(zhí)行函數(shù),返回一個(gè)Promise
 const html = await frame.evaluate(body=>body.innerHTML,bodyHandle);
 await bodyHandle.dispose();
 browser.close();
 console.log(html);
})();

用cheerio解析html:

// 使用cheerio模塊裝載我們得到的頁(yè)面源代碼,返回的是一個(gè)類(lèi)似于jquery中的$對(duì)象
// 使用這個(gè)$對(duì)象就像操作jquery對(duì)象一般去操作我們獲取得到的頁(yè)面的源代碼
var $ = cheerio.load(html);
var $menu_box = $(".statistics___1cFUQ");
console.log($menu_box.html());

用fs寫(xiě)入到文件中:

/* fs.wirteFile有三個(gè)參數(shù)
  * 1,第一個(gè)參數(shù)是要寫(xiě)入的文件路徑
  * 2,第二個(gè)參數(shù)是要寫(xiě)入得內(nèi)容
  * 3,第三個(gè)參數(shù)是可選參數(shù),表示要寫(xiě)入的文件編碼格式,一般就不寫(xiě),默認(rèn)就行
  * 4,第四個(gè)參數(shù)是個(gè)回調(diào)函數(shù) 只有一個(gè)參數(shù)error,來(lái)判斷是否寫(xiě)入成功
  */
fs.writeFile("./coronavirus.php",$menu_box.html(),error=>{
  if(error) return console.log("寫(xiě)入文件失敗,原因是:"+error.message);
  console.log('寫(xiě)入成功');
});

引入到網(wǎng)站中:

我是直接把它放在頭部,局部代碼如下:

<div id="header-bg">
  <style type="text/css">
    .title___2d1_B img {
      width: 18px;
      height: 18px;
      cursor:pointer;
    }
    #novel_coronavirus {
      text-align: center;
      position:relative;
      top:50px;
      background-color:rgba(255,255,255,0.7);
    }
    #novel_coronavirus li {
      margin: 10px;
      padding:2px;
      border:1px slide #000;
    }
    #novel_coronavirus ul li { 
      list-style:none;
      display: inline-block;
    }
    .count___3GCdh p{
      font-size:12px;
    }
    .count___3GCdh span{
      font-size:20px;
    }
  </style>
  <div id="novel_coronavirus" > 
    <strong><p >新型冠狀病毒疫情實(shí)時(shí)動(dòng)態(tài)</p></strong>
    <?php require("./test/coronavirus.php");?>
  </div>
</div>

服務(wù)器上運(yùn)行的完整代碼:

CronJob的定時(shí)參數(shù)是 秒 分鐘 小時(shí) 天 月份 星期。這里我設(shè)置成了每分鐘爬取一次。(我是用mstsc遠(yuǎn)程連接后運(yùn)行node coronavirus.js的,這樣關(guān)閉遠(yuǎn)程桌面連接后,服務(wù)器依然會(huì)每分鐘爬取一次丁香醫(yī)生上的新型冠狀病毒的全國(guó)疫情實(shí)時(shí)動(dòng)態(tài)。 

const cheerio = require('cheerio');
const puppeteer = require('puppeteer');
const fs = require('fs');
var cronJob = require('cron').CronJob;
new cronJob('0 */1 * * * *',function(){
 update();
},null,true);  //每分鐘執(zhí)行一次
//爬取全國(guó)新型肺炎疫情實(shí)時(shí)動(dòng)態(tài)并寫(xiě)入到指定的.php文件
function update() {
 (async () => {
  const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
  const page = await browser.newPage();
  await page.goto('https://ncov.dxy.cn/');
  const frame = await page.mainFrame();
  const bodyHandle = await frame.$('html');
  const html = await frame.evaluate(body=>body.innerHTML,bodyHandle);
  await bodyHandle.dispose();
  browser.close();
  var $ = cheerio.load(html);
  var $menu_box = $(".statistics___1cFUQ");
  fs.writeFile("coronavirus.php",$menu_box.html(),error=>{
   if(error) {
    console.log("寫(xiě)入文件失敗,原因是:"+error.message);
   } else { 
    console.log('更新成功');
   }
  });
 })();
}

查看我的網(wǎng)站 

node爬取新型冠狀病毒的疫情實(shí)時(shí)動(dòng)態(tài)

node爬取新型冠狀病毒的疫情實(shí)時(shí)動(dòng)態(tài)

總結(jié)

以上所述是小編給大家介紹的node爬取新型冠狀病毒的疫情實(shí)時(shí)動(dòng)態(tài),希望對(duì)大家有所幫助!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI