Last active
July 29, 2018 17:00
-
-
Save JaosnHsieh/fea3bbd189f0f5887312edd7f7c69c64 to your computer and use it in GitHub Desktop.
node.js , little web parser save a div html content to mssql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
"dependencies": { | |
"cheerio": "^0.22.0", | |
"request": "^2.79.0", | |
"sequelize": "^3.27.0", | |
"tedious": "^1.14.0" | |
} | |
*/ | |
var Sequelize = require('sequelize'); | |
var request = require("request"); | |
var cheerio = require("cheerio"); | |
var sequelize = new Sequelize('Airforce_temp', 'sa', 'mypassword', { | |
host: '192.168.11.24', | |
dialectOptions: { | |
instanceName: 'MSSQLSERVER' | |
}, | |
dialect: 'mssql', | |
pool: { | |
max: 5, | |
min: 0, | |
idle: 10000 | |
} | |
}); | |
var News = sequelize.define('news', { | |
content: { | |
type: Sequelize.STRING(3000) | |
}, | |
title: { | |
type: Sequelize.STRING(3000) | |
}, | |
date: { | |
type: Sequelize.STRING(3000) | |
} | |
}); | |
// force: true will drop the table if it already exists | |
News.sync({ | |
force: false | |
}).then(function () { | |
}); | |
var urls = ['http://air.mnd.gov.tw/Publish.aspx?cnid=1732&p=73400', | |
'http://air.mnd.gov.tw/Publish.aspx?cnid=1732&p=73401&Level=1' | |
,'http://air.mnd.gov.tw/Publish.aspx?cnid=1732&p=73402&Level=1']; | |
urls.forEach(function(url){ | |
request({ | |
url: url, | |
method: "GET" | |
}, function (e, r, b) { /* Callback 函式 */ | |
// console.log(b); | |
$ = cheerio.load(b, { | |
withDomLvl1: true, | |
normalizeWhitespace: false, | |
xmlMode: false, | |
decodeEntities: false | |
}); | |
var content = $("#PublishManager1_Panel_PublicDetail").html(); | |
var mynews = News.build({ | |
content: content, | |
title: "", | |
date:"" | |
}) | |
mynews.save().then(function() { | |
/* ... */ | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment