0
페이지를 구문 분석 할 때 많은 제목이 있습니다. 각 제목은 데이터베이스에 체크인해야하며 데이터베이스에 추가되지 않은 경우 수행하십시오.결과가없는 경우 데이터베이스를 체크인 한 다음 node.js-mysql과 함께 추가하십시오.
예 목록 : 보시다시피
test
test2
test3
test4
test2
test
test5
test4
, 일부 중복이 있습니다.
내 코드 :
var $ = cheerio.load(body);
$(".program-table .item").each(function() {
var titleOriginal = $(this).find(".title-original").text().match(/(.+)\s\
var movieYear = titleOriginal[2];
var movieTitle = titleOriginal[1];
connection.query("SELECT id FROM movie WHERE name = ? AND year = ?", [movieTitle, movieYear], function(err, result) {
if (err) return;
if (result.length) {
console.log('Found, ID: %s', result[0].id);
} else {
connection.query("INSERT INTO movie SET name = ?, year = ?", [movieTitle, movieYear], function(err, result) {
if (err) return;
console.log('Not found, adding into database. ID: %s', result.insertId);
});
}
});
});
결과 : 데이터베이스가 제목을 포함하는 청소 또는 비 기록이있는 경우, 스크립트는 중복으로 데이터베이스에 모든 제목을 추가합니다. 두 번째로 시작하면 제목이있는 레코드가 발견되어 건너 뛰게됩니다.