김서연

api데이터에 파싱테스트

Showing 1 changed file with 17 additions and 12 deletions
...@@ -13,28 +13,33 @@ app.use(express.static('public')); ...@@ -13,28 +13,33 @@ app.use(express.static('public'));
13 const $base_url = `https://api.odcloud.kr/api/apnmOrg/v1/list`; 13 const $base_url = `https://api.odcloud.kr/api/apnmOrg/v1/list`;
14 const $key = '4US0H%2BXj%2BmS8IR4YL0%2BUb9H4CcbTr92QxhYULfOEz1DT%2BZaaos4sRfNh6cmAD2Icli862Ysc31%2BaE4pWywDBIA%3D%3D'; 14 const $key = '4US0H%2BXj%2BmS8IR4YL0%2BUb9H4CcbTr92QxhYULfOEz1DT%2BZaaos4sRfNh6cmAD2Icli862Ysc31%2BaE4pWywDBIA%3D%3D';
15 15
16 -//임시 16 +//임시 매개변수
17 const $page=1; 17 const $page=1;
18 const $perPage=10; 18 const $perPage=10;
19 19
20 -const $api_url = $base_url + '?returnType=XML&serviceKey='+$key+'&page='+$page+'&perPage='+$perPage; 20 +const $api_url = $base_url + '?serviceKey='+$key+'&page='+$page+'&perPage='+$perPage;
21 console.log($api_url); 21 console.log($api_url);
22 22
23 +//해당 URL로 요청
24 +request($api_url,function(err,res,body){
25 + if(err) throw err;
26 +
27 + var obj = JSON.parse(body);
28 + // console.log(obj);
29 + for(var i=0; i<obj.data.length; i++){
30 + let name = obj.data[i].orgnm;
31 + let location = obj.data[i].orgZipaddr;
32 + console.log(`기관명: ${name}, 위치: ${location}\n`);
33 + }
34 +
35 + // res.render('main', {title: 'misae', data:data, pm10:pm10})
36 +
37 +})
23 38
24 app.listen(3000,function(){ 39 app.listen(3000,function(){
25 console.log('Connected 3000 port!'); 40 console.log('Connected 3000 port!');
26 }); 41 });
27 42
28 -request($api_url,function(err,res,body){
29 - $ = cheerio.load(body);
30 - const $infoList = $('item')
31 - console.log($infoList.html());
32 - console.log($infoList.length);
33 - // let name = $infoList.find('orgnm').text();
34 - // let location = $infoList.find('orgZipaddr').text();
35 - // console.log(`기관명: ${name}, 위치: ${location}`);
36 -})
37 -
38 app.post('/search',function(req,res){ 43 app.post('/search',function(req,res){
39 res.send('내가 검색한 지역은 ' + req.body.region); 44 res.send('내가 검색한 지역은 ' + req.body.region);
40 }) 45 })
......