gmarket.js
3.2 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const puppeteer = require('puppeteer');
const readline = require('readline');
//gmarket_c();
// Gmarket Cart Crawling
async function gmarket_c(){
// launching headless browser
const browser = await puppeteer.launch();
// making a new page
const page = await browser.newPage();
//console input id & password
var args = process.argv;
var g_id = args[2];
var g_pw = args[3];
console.log(args[2] + " " + args[3]);
// Gmarket login page
await page.goto('https://signinssl.gmarket.co.kr/login/login?url=https://www.gmarket.co.kr/');
// Ading user information
await page.evaluate((id, pwd) => {
document.querySelector('#id').value = id;
document.querySelector('#pwd').value = pwd;
}, g_id, g_pw);
await console.log(g_id,g_pw);
// try login
await page.click('.button_login');
await page.waitForNavigation();
// goto cart page
await page.goto('https://cart.gmarket.co.kr/ko/cart');
//await page.screenshot({ path: 'gmarket.png', fullPage:true });
// container which will hold crawled data [{},{}...]
let data = [];
// crawling start! (using getOne and getAll function)
data = await getAll(page);
//data.push(await getAll(page)); // data[0][n]
//logging the result
for(let index = 0; index < data.length; index++){
console.log(data[index]);
}
await browser.close();
}
// Crawling cart informations(object) to data(array)
async function getAll(page) {
var data = [];
//const number = await page.$$eval("#cart_list > ol > li:nth-child(1) > div.cart--basket_body > div > ul > li", (data) => data.length);
const number = await page.$$eval("#cart_list > ol > li", (data) => data.length);
// counting the number of the box
for (let index = 0; index < number; index++) {
data.push(await getOne(page, index + 1));
// pushing to the array
}
return Promise.resolve(data);
}
// Crawling cart information to data(object)
async function getOne(page, index) {
var data = {};
// this is example code
//data.programPeriod = await page.$eval("#iph_content > div > div.list_type_h1.web_view.mt3 > table > tbody > tr:nth-child(" + index + ") > td:nth-child(5)", (data) => data.textContent);
// product name
data.prd_name = await page.$eval("#cart_list > ol > li:nth-child(" + index + ") > div.cart--basket_body > div > ul > li > div > div.item_info > dl > dd > div.section.item_title > a > span", data => data.textContent);
// product price
data.prd_price = await page.$eval("#cart_list > ol > li:nth-child(" + index + ") > div.cart--basket_body > div > ul > li > div > div.item_info > dl > dd > div.section.item_price > span.format-price > span > strong", data => data.textContent);
// product link
data.prd_link = await page.$eval("#cart_list > ol > li:nth-child(" + index + ") > div.cart--basket_body > div > ul > li > div > div.item_info > dl > dd > div.section.item_title > a", data => data.href);
// product image source
data.prd_img = await page.$eval("#cart_list > ol > li:nth-child(" + index + ") > div.cart--basket_body > div > ul > li > div > div.item_img > a > img", data => data.src);
// not yet
return Promise.resolve(data);
}
module.exports.gmarket_c = gmarket_c;
module.exports.getOne = getOne;
module.exports.getAll = getAll;