最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用node搭建自動(dòng)發(fā)圖文微博機(jī)器人的方法

 更新時(shí)間:2019年03月22日 10:44:05   作者:zhou_web  
這篇文章主要介紹了使用node搭建自動(dòng)發(fā)圖文微博機(jī)器人的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

本文僅供學(xué)習(xí)交流,請(qǐng)勿用于商業(yè)用途,并遵守新浪微博相關(guān)規(guī)定。

代碼目錄

此微博機(jī)器人的實(shí)現(xiàn)功能如下:

  • 模擬登陸新浪微博,獲取cookie;
  • 自動(dòng)上傳圖片至微博圖床;
  • 自動(dòng)發(fā)送內(nèi)容不同的圖文微博;
  • 通過(guò)定時(shí)任務(wù),實(shí)現(xiàn)周期性發(fā)微博任務(wù)。

效果圖


圖文內(nèi)容我固定了,可自行使用第三方api獲取要發(fā)送的內(nèi)容或爬取第三方內(nèi)容發(fā)送。(偷個(gè)懶...

要實(shí)現(xiàn)發(fā)送圖文微博可以分為三個(gè)步驟

  • 登錄微博。
  • 圖片上傳至微博圖床獲取PID。
  • 發(fā)送微博。

登錄

登錄可以使用Puppeteer node庫(kù),很輕松的實(shí)現(xiàn)登錄獲取微博cookie,這里不多介紹,可以自行搜索Puppeteer學(xué)習(xí)。

Puppeteer是谷歌官方出品的一個(gè)通過(guò)DevTools協(xié)議控制headless Chrome的Node庫(kù)??梢酝ㄟ^(guò)Puppeteer的提供的api直接控制Chrome模擬大部分用戶操作來(lái)進(jìn)行UI Test或者作為爬蟲(chóng)訪問(wèn)頁(yè)面來(lái)收集數(shù)據(jù)。
async function login(username, password) {
  const browser = await puppeteer.launch({
    // headless: false,
    slowMo: 250,
    executablePath: ''
  });
  const page = (await browser.pages())[0];
  await page.setViewport({
    width: 1280,
    height: 800
  });

  await page.goto("https://weibo.com/");
  await page.waitForNavigation();
  await page.type("#loginname", username);
  await page.type("#pl_login_form > div > div:nth-child(3) > div.info_list.password > div > input", password);
  await page.click("#pl_login_form > div > div:nth-child(3) > div:nth-child(6)");
  await page.waitForNavigation().then(result => {
    return new Promise((resolve) => {
      page.cookies().then(async cookie => {
        fs.createWriteStream("cookie.txt").write(JSON.stringify(cookie), "UTF8");//存儲(chǔ)cookie
        await browser.close();//關(guān)閉打開(kāi)的瀏覽器
        resolve(cookie);
      });
    })
  }).catch(e => {
    page.screenshot({
      path: 'code.png',
      type: 'png',
      x: 800,
      y: 200,
      width: 100,
      height: 100
    });
    return new Promise((resolve, reject) => {
      readSyncByRl("請(qǐng)輸入驗(yàn)證碼").then(async (code) => {
        await page.type("#pl_login_form > div > div:nth-child(3) > div.info_list.verify.clearfix > div > input", code);
        await page.click("#pl_login_form > div > div:nth-child(3) > div:nth-child(6)");
        await page.waitForNavigation();
        page.cookies().then(async cookie => {
          fs.createWriteStream("cookie.txt").write(JSON.stringify(cookie), "UTF8");
          await browser.close();
          resolve(cookie);
        });

      })
    })
  })
}

圖片上傳至微博圖床

上傳到微博圖床可以看這里 http://weibo.com/minipublish 抓包看上傳的接口過(guò)程,可以看到上傳的是base64圖片信息。所以上傳前把圖片轉(zhuǎn)換成base64編碼,而本地圖片的編碼和互聯(lián)網(wǎng)鏈接圖片的編碼又不一樣,這里使用的是互聯(lián)網(wǎng)鏈接的圖片,node本地圖片轉(zhuǎn)換成base64編碼更簡(jiǎn)單些。上傳成功后返回微博圖床圖片的pid。記住這個(gè)pid,發(fā)微博用的就是這個(gè)pid。

發(fā)送微博

有了微博cookie和圖片pid后就可以發(fā)微博了,多張圖片時(shí)pid之間以|隔開(kāi)的。

async function weibopost(text, pic_ids = '', cookie) { //發(fā)送微博內(nèi)容(支持帶圖片)
  return new Promise(async (resolve, reject) => {
    if (cookie === '') {
      reject('Error: Cookie not set!');
    }
    let post_data = querystring.stringify({
      'location': 'v6_content_home',
      'text': text,
      'appkey': '',
      'style_type': '1',
      'pic_id': pic_ids,
      'tid': '',
      'pdetail': '',
      'mid': '',
      'isReEdit': 'false',
      'rank': '0',
      'rankid': '',
      'module': 'stissue',
      'pub_source': 'main_',
      'pub_type': 'dialog',
      'isPri': '0',
      '_t': '0'
    });

    let post_options = {
      'Accept': '*/*',
      'Accept-Encoding': 'gzip, deflate, br',
      'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
      'Connection': 'keep-alive',
      'Content-Length': Buffer.byteLength(post_data),
      'Content-Type': 'application/x-www-form-urlencoded',
      'Cookie': cookie,
      'Host': 'weibo.com',
      'Origin': 'https://weibo.com',
      'Referer': 'https://weibo.com',
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36',
      'X-Requested-With': 'XMLHttpRequest'
    };


    let {
      data
    } = await axios.post('https://weibo.com/aj/mblog/add?ajwvr=6&__rnd=' + new Date().getTime(), post_data, {
      withCredentials: true,
      headers: post_options
    })
    if (data.code == 100000) {
      console.log('\n' + text + '-----Sent!' + '---' + new Date().toLocaleString());
      resolve(data);
    } else {
      console.log('post error');
      reject('post error');
    }

  });
}

最后就是定時(shí)任務(wù)了,定時(shí)任務(wù)可以使用node-schedule node庫(kù),這里不多介紹,可以自行搜索學(xué)習(xí)。這里使用的是每隔10分鐘發(fā)送一次。

function loginTo() {
  login(config.username, config.password).then(async () => {
    let rule = null;
    rule = new schedule.RecurrenceRule();
    rule.minute = [01, 11, 21, 31, 41, 51];
    try {
      let cookie = await getCookie();
      getContent(cookie);
    } catch (error) {
      console.log(error);
    }

    j = schedule.scheduleJob(rule, async () => { //定時(shí)任務(wù)
      try {
        let cookie = await getCookie();
        getContent(cookie);
      } catch (error) {
        console.log(error);
      }

    });
  })
}

代碼地址: github地址

參考

https://github.com/itibbers/weibo-post

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • node.js使用express框架進(jìn)行文件上傳詳解

    node.js使用express框架進(jìn)行文件上傳詳解

    在本篇內(nèi)容里小編給大家整理了關(guān)于node.js使用express框架進(jìn)行文件上傳的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。
    2019-03-03
  • nodejs log4js 使用詳解

    nodejs log4js 使用詳解

    這篇文章主要介紹了nodejs log4js 使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • node內(nèi)存泄漏排查與修復(fù)過(guò)程

    node內(nèi)存泄漏排查與修復(fù)過(guò)程

    之前開(kāi)發(fā)了一個(gè)node接口,該接口使用canvas繪制產(chǎn)品圖提供給java端使用,在運(yùn)行了一段時(shí)間后發(fā)現(xiàn)了內(nèi)存泄漏問(wèn)題,本文淺記下修復(fù)過(guò)程,文章通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • 新版Node.js內(nèi)置支持.env文件使用詳解

    新版Node.js內(nèi)置支持.env文件使用詳解

    這篇文章主要為大家介紹了新版Node.js內(nèi)置支持.env文件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • node.js實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)示例詳解

    node.js實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)示例詳解

    這篇文章主要為大家介紹了node.js實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 從零開(kāi)始學(xué)習(xí)Node.js系列教程之SQLite3和MongoDB用法分析

    從零開(kāi)始學(xué)習(xí)Node.js系列教程之SQLite3和MongoDB用法分析

    這篇文章主要介紹了Node.js SQLite3和MongoDB用法,結(jié)合實(shí)例形式分析了SQLite3和MongoDB數(shù)據(jù)庫(kù)的初始化、連接、查詢等操作的實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-04-04
  • 基于promise.js實(shí)現(xiàn)nodejs的promises庫(kù)

    基于promise.js實(shí)現(xiàn)nodejs的promises庫(kù)

    promise是JavaScript實(shí)現(xiàn)優(yōu)雅編程的一個(gè)非常不錯(cuò)的輕量級(jí)框架。該框架可以讓你從雜亂的多重異步回調(diào)代碼中解脫出來(lái),并把精力集中到你的業(yè)務(wù)邏輯上。
    2014-07-07
  • node.js博客項(xiàng)目開(kāi)發(fā)手記

    node.js博客項(xiàng)目開(kāi)發(fā)手記

    本篇文章給大家總結(jié)了node.js博客項(xiàng)目開(kāi)發(fā)的相關(guān)步驟以及知識(shí)點(diǎn)分享,有興趣的朋友參考下。
    2018-03-03
  • Windows系統(tǒng)中安裝nodejs圖文教程

    Windows系統(tǒng)中安裝nodejs圖文教程

    這篇文章主要介紹了Windows系統(tǒng)中安裝nodejs圖文教程,本文分解了安裝中的各個(gè)步驟并給出圖文說(shuō)明,需要的朋友可以參考下
    2015-02-02
  • node.js中EJS 模板快速入門教程

    node.js中EJS 模板快速入門教程

    本篇文章主要介紹了EJS 模板快速入門學(xué)習(xí),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05

最新評(píng)論

河南省| 兰坪| 临城县| 荣成市| 贺州市| 泗洪县| 湘西| 洪江市| 尚义县| 隆尧县| 积石山| 双辽市| 博爱县| 梁平县| 苍梧县| 壶关县| 松原市| 温泉县| 千阳县| 仁怀市| 靖远县| 丹巴县| 平定县| 股票| 鹤庆县| 台湾省| 陕西省| 新乡市| 闽侯县| 台南市| 石门县| 衢州市| 景谷| 虞城县| 六盘水市| 平远县| 湘乡市| 鸡西市| 托里县| 博兴县| 佛教|