봇을 만들어 웹사이트의 정보를 수집하는 것
npm init
{
"main": "index.js", //프로젝트의 메인 파일 명시
"scripts": {
"start": "node index"
},
}
컴마랑 줄바꿈으로 구분된 값들로 실제 엑셀에서 불러들일 수 있는 파일 형식
//data.csv
타이타닉, https://movie.naver.com/movie/bi/mi/basic.nhn?code=18847
아바타, https://movie.naver.com/movie/bi/mi/basic.nhn?code=62266
.
.
파싱이란 자바스크립트가 아닌 데이타를 자바스크립트로 변환하는 것
npm i csv-parse
☸ 바퀴를 재발명하지마라
이미 있는 parser함수 사용
//index.js
const parse = require('csv-parse/lib/sync'); //csv parser 함수
const fs = require('fs'); //file sync module
const csv = fs.readFileSync('csv/data.csv'); //fs의 버퍼로 파일을 읽는 메소드
const records = parse(csv.toString('utf-8')); //버퍼를 문자열로 전환 후 2차원 배열로 parse
require('node_modules/csv-parse/lib/sync.js')
fs
readFileSync
npm start
로 실행npm i xlsx
//index.js
const xlsx = require('xlsx'); //엑셀 파서
const workbook = xlsx.readFile('xlsx/data.xlsx'); //파싱
const ws = workbook.Sheets.영화목록; //엑셀의 '영화목록' 시트 접근
const records = xlsx.utils.sheet_to_json(ws); //row들을 JS 객체형식으로
sheet_to_json
//index.js
//const xlsx = require('xlsx');
//const workbook = xlsx.readFile('xlsx/data.xlsx');
//const ws = workbook.Sheets.영화목록;
//const records = xlsx.utils.sheet_to_json(ws);
const axios = require('axios'); //html로 웹자료를 get
const cheerio = require('cheerio'); //html을 JS로 변환
const crawler = async () => {
await Promise.all(records.map(async (r) => {
const response = await axios.get(r.링크);
if (response.status === 200)
const html = response.data;
const $ = cheerio.load(html);
const text = $('.score.score_left .star_score').text(); //원하는 html 태그 지정 (jquery API 사용)
console.log(r.제목, '평점', text.trim()); //trim()으로 공백 제거
}
};
crawler();
//index.js
const crawler = async () => {
for (const [i, r] of records.entries()) {
const response = await axios.get(r.링크);
if (response.status === 200)
const html = response.data;
const $ = cheerio.load(html);
const text = $('.score.score_left .star_score').text();
console.log(r.제목, '평점', text.trim());
}
};
//엑셀 맨 위 제목행만 제거
//index.js
const records = xlsx.utils.sheet_to_json(ws, { header: 'A' });
records.shift(); //파싱된 제목 제거
엑셀 시트의 A1부터 B11까지 파싱이 목표
//index.js
//방법1
ws['!ref'] - ws['!ref'].split(':').map((v, i) => {
if (i === 0) {
return 'A2';
}
return v;
}).join(':');
const records = xlsx.utils.sheet_to_json(ws, { header: 'A' });
//방법2
ws['!ref'] = 'A2:B11';
const records = xlsx.utils.sheet_to_json(ws, { header: 'A' });
//index.js
//원하는 시트의 자료 접근
const ws = workbook.SheetNames.영화제목;
//시트별로 따로 코딩 시
for ( const name of workbook.SheetNames) {
const ws = workbook.Sheets[name];
}
엑셀파일에 도로 입력하기
//add_to_sheet.js
const xlsx = require('xlsx');
function range_add_cell(range, cell) {
var rng = xlsx.utils.decode_range(range);
var c = typeof cell === 'string' ? xlsx.utils.decode_cell(cell) : cell;
if (rng.s.r > c.r) rng.s.r = c.r;
if (rng.s.c > c.c) rng.s.c = c.c;
if (rng.e.r < c.r) rng.e.r = c.r;
if (rng.e.c < c.c) rng.e.c = c.c;
return xlsx.utils.encode_range(rng);
}
//함수에 넣을 변수들: sheet, cell, type, raw
module.exports = function add_to_sheet(sheet, cell, type, raw) {
sheet['!ref'] = range_add_cell(sheet['!ref'], cell);
sheet[cell] = { t: type, v: raw };
};
//index.js
const add_to_sheet = require('./add_to_sheet');
const crawler = async () => {
//객체에 새로운 열 추가
add_to_sheet(ws, 'C1', 's', '평점');
for (const [i, r] of records.entries()) {
const response = await axios.get(r.링크);
if (response.status === 200)
const html = response.data;
const $ = cheerio.load(html);
const text = $('.score.score_left .star_score').text();
console.log(r.제목, '평점', text.trim());
//C:2부터의 column으로 지정 (자료를 입력할 column구간)
const newCell = 'C' + (i + 2);
//객체의 새 열에 자료 추가
add_to_sheet(ws, newCell, 'n', parseFloat(text.trim()));
}
//새로운 엑셀 시트에 write
xlsx.writeFile(workbook, 'xlsx/result.xlsx');
};
Ryujinx, https://ryujinxfirmware.com/ in particular, distinguishes itself with its impressive array of features, making it a top pick for Nintendo Switch emulation. you'll have access to free Switch ROMs https://krnl.vip/nintendo-switch-roms/ without any hindrance. We offer direct download links that are free from intrusive ads.