-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_item_list.js
53 lines (43 loc) · 1.05 KB
/
get_item_list.js
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
var index = 0;
var urlPrefix = 'http://list.tmall.com//search_product.htm?cat=50035531&s=';
var urlPostfix = '&q=q%B1%D2&sort=d&style=g&from=.list.pc_1_searchbutton#J_Filter';
var fs = require('fs');
var out = 'item_ids.txt';
if (fs.exists(out)) {
fs.remove(out);
}
var inPath = 'item_num.txt';
var itemNum = parseInt(fs.read(inPath));
function getItemId(idx, callback)
{
var url = urlPrefix + idx + urlPostfix;
console.log('get item id: ' + url);
var page = require('webpage').create();
page.open(url, function() {
var data = page.evaluate(function () {
var l = document.querySelectorAll('div#J_ItemList div.product');
var r = [];
for (var i = 0; i < l.length; i++) {
r[i] = l[i].getAttribute('data-id');
}
return r;
});
for (var i = 0; i < data.length; i++) {
fs.write(out, data[i] + '\n', 'a');
}
page.close();
callback.apply();
});
}
function process()
{
if (index < itemNum) {
var idx = index;
index = index + 60;
getItemId(idx, process);
}
else {
phantom.exit(0);
}
}
process();