파싱해서 DB로 저장해보기.
이쪽이 전문분야가 아니라 많이 부족합니다.
아~ 이렇게도 가능 하구나 하고 참조만 하세요.
(그누보드로 돌리면 리자님한테 혼납니다.)
[code]
var Spider = require('node-spider');
var mysql = require('mysql');
var conn = mysql.createConnection({
host :'localhost',
port : 3306,
user : 'DB아이디',
password : '비번',
database:'데이터베이스'
});
conn.connect();
if(!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
function addslashes(string) {
return string.replace(/\\/g, '\\\\').
replace(/\u0008/g, '\\b').
replace(/\t/g, '\\t').
replace(/\n/g, '\\n').
replace(/\f/g, '\\f').
replace(/\r/g, '\\r').
replace(/'/g, '\\\'').
replace(/"/g, '\\"');
}
var spider = new Spider({
// How many requests can be run in parallel
concurrent: 5,
// How long to wait after each request
delay: 0,
// A stream to where internal logs are sent, optional
logs: process.stderr,
// Re-visit visited URLs, false by default
allowDuplicates: false,
// If `true` all queued handlers will be try-catch'd, errors go to `error` callback
catchErrors: true,
// Called when there's an error, throw will be used if none is provided
error: function(err, url) {
console.log(err);
},
// Called when there are no more requests
done: function() {
},
//- All options are passed to `request` module, for example:
headers: { 'user-agent': 'node-spider' },
//encoding: 'utf8'
encoding: null
});
var handleRequest = function(doc) {
// doc.res
var content = doc.$('div.con_inner').text().trim();
var title = doc.$('#head_title').text().trim();
var contentHTML = doc.$('div.con_inner').html();
var member = doc.$('li#info_name').find('span.member').text().trim();
var PageUrl = doc.url;
if(title) title = addslashes(title);
if(member) member = addslashes(member);
if(content) content = addslashes(content);
if(contentHTML) contentHTML = addslashes(contentHTML);
if(content) {
// new page crawled
console.log('---시작---');
console.log(PageUrl); // page url
conn.query("insert into `_sc` set `member` = '"+member+"', `title` = '"+title+"', `content` = '"+content+"', `html_content` = '"+contentHTML+"', `url` = '"+PageUrl+"', `rdate` = now()");
console.log('---종료---');
}
// uses cheerio, check its docs for more info
doc.$('a').each(function(i, elem) {
// do stuff with element
var href = elem.attribs.href.split('#')[0];
var url = doc.resolve(href);
if(url.indexOf('/cm_free/') > -1) { // 자게 주소 패턴이 있을경우만..
// crawl more
spider.queue(url, handleRequest);
}
});
};
// start crawling
spider.queue('http://sir.kr/cm_free', handleRequest);
[/code]
댓글 3개
[code]
doc.$('a').each(function(i, elem) {
// do stuff with element
var href = elem.attribs.href.split('#')[0];
var url = doc.resolve(href);
if(url.indexOf('/cm_free/') > -1) { // 자게 주소 패턴이 있을경우만..
// crawl more
spider.queue(url, handleRequest);
}
});
[/code]
이 부분이 해당 내용입니다.
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5396 | 기타 |
슈퍼스타맨
|
4개월 전 | 349 | |
| 5395 | PHP |
untitled
|
5개월 전 | 822 | |
| 5394 | MySQL |
선택과집중
|
5개월 전 | 593 | |
| 5393 | 웹서버 |
techstar
|
8개월 전 | 861 | |
| 5392 |
|
1년 전 | 1217 | ||
| 5391 | 10개월 전 | 1117 | |||
| 5390 | 10개월 전 | 904 | |||
| 5389 | 9개월 전 | 874 | |||
| 5388 | 8개월 전 | 974 | |||
| 5387 | 8개월 전 | 807 | |||
| 5386 | JavaScript |
nekoieye
|
8개월 전 | 971 | |
| 5385 | 웹서버 | 8개월 전 | 989 | ||
| 5384 | JavaScript |
|
9개월 전 | 815 | |
| 5383 | 기타 | 10개월 전 | 1127 | ||
| 5382 | 기타 |
|
10개월 전 | 575 | |
| 5381 | JavaScript | 10개월 전 | 905 | ||
| 5380 | 기타 |
|
10개월 전 | 689 | |
| 5379 | JavaScript | 10개월 전 | 689 | ||
| 5378 | 10개월 전 | 1198 | |||
| 5377 | 기타 |
|
11개월 전 | 760 | |
| 5376 | jQuery |
|
11개월 전 | 574 | |
| 5375 | jQuery |
techstar
|
11개월 전 | 728 | |
| 5374 | 기타 |
|
11개월 전 | 777 | |
| 5373 | MySQL |
|
11개월 전 | 808 | |
| 5372 | 기타 |
|
11개월 전 | 1013 | |
| 5371 | JavaScript |
|
11개월 전 | 726 | |
| 5370 | JavaScript |
|
11개월 전 | 729 | |
| 5369 | PHP |
|
11개월 전 | 1243 | |
| 5368 | PHP | 1년 전 | 1408 | ||
| 5367 | 기타 |
nekoieye
|
1년 전 | 1283 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기