Nodejs實現批量下載妹紙圖

來源:互聯網
上載者:User

Nodejs實現批量下載妹紙圖

   這篇文章主要介紹了使用Nodejs實現批量下載妹紙圖的方法和詳細代碼,十分的實用,喜歡妹紙的小夥伴們可以參考下。

  聽說最近下載妹子圖很火?

  Nodejs (javascrpt)自然不能落後~

  雖然從沒寫過像樣的Nodejs程式,但作為至少翻過書的前端同學來說,Nodejs用得還蠻順手的哈~

  花了一時間點事件學習了下Nodejs的網頁擷取和檔案下載方法,沒事亂搗騰就寫了這個半成品的下載器

  使用方法:

  1)建立一個download目錄

  2)建立download.js(其實名字隨便取),並複製到download目錄下

  3)複製兩段代碼到download.js中

  4)開啟命令列工具,並將目前的目錄轉到與download目錄下

  5)在命令列中輸入:node download.js

  6)等著收妹子圖吧~

  簡單的妹子圖對象(新增斷定下載支援)

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

var http = require('http');

var fs = require('fs');

 

function Mzitu(options) {

this.id = 1;

 

this.initialize.call(this, options);

return this;

}

 

Mzitu.prototype = {

constructor: Mzitu,

initialize: function _initialize(options) {

this.baseUrl = options.baseUrl;

this.dir = options.dir || '';

this.reg = options.reg;

this.total = options.total;

this.page = options.from || 1;

},

start: function _start() {

this.getPage();

},

getPage: function _getPage() {

var self = this,

data = null;

 

if (this.page <= this.total) {

http.get(this.baseUrl + this.page, function (res) {

res.setEncoding("utf8");

 

res.on('data', function (chunk) {

data += chunk;

}).on('end', function () {

self.parseData(data);

});

});

}

},

parseData: function _parseData(data) {

var res = [],

match;

 

while ((match = this.reg.exec(data)) != null) {

res.push(match[1]);

}

 

this.download(res);

},

download: function _download(resource) {

var self = this,

currentPage = self.page;

 

resource.forEach(function (src, idx) {

var filename = src.substring(src.lastIndexOf('/') + 1),

writestream = fs.createWriteStream(self.dir + filename);

 

http.get(src, function (res) {

res.pipe(writestream);

});

 

writestream.on('finish', function () {

console.log('page: ' + currentPage + ' id: ' + self.id++ + ' download: ' + filename);

});

});

 

self.page++;

self.getPage();

}

};

  妹子圖下載啟動方式

  ?

1

2

3

4

5

6

7

8

9

var mzitu = new Mzitu({

baseUrl: 'http://www.mzitu.com/share/comment-page-',

dir: '',

reg: /<img\s*src="(.*?)"\s*alt=".*"\s*\/>/g,

total: 141,

from: 1

});

 

mzitu.start();

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.