2016-08-27 2 views
2

포럼에서 게시물 제목, 사용자 이름 및 마지막 게시물 시간을 가져 오는 간단한 웹 스크레이퍼를 쓰고 있습니다.포럼 제목 웹 스크레이퍼

문제는 스크래퍼는 테이블의 마지막 마지막 항목 만 가져옵니다. 예를 들어

: 테이블이 이러한 방식으로 구성되는 경우

<tbody> 
<tr class=""> 
    <td class="title">  
    <a href="/forums/marketplace/8827" title="View full post details">Title number 1</a> 
    </td> 
    <td class="author"><a href="/members/pursu" title="View member, pursu">pursu</a></td> 
    <td class="count">0</td> 
    <td class="last_post">9 minutes ago</td> 
</tr> 
<tr class="color2"> 
    <td class="title"> 

    <a href="/forums/marketplace/8826" title="View full post details">Title number 2</a> 
    </td> 
    <td class="author"><a href="/members/colinatx" title="View member, colinatx">colinatx</a></td> 
    <td class="count">0</td> 
    <td class="last_post">9 minutes ago</td> 
</tr> 
<tr class=""> 
    <td class="title">  
    <a href="/forums/marketplace/8785" title="View full post details">Title number 3</a> 
    </td> 
    <td class="author"><a href="/members/Object117" title="View member, Object117">Object117</a></td> 
    <td class="count">11</td> 
    <td class="last_post">about 1 hour ago</td> 
</tr> 
</tbody> 
.json 출력 파일에 기록한다

결과 대신이

{ 
    "title": "Title number 3", 
    "author": "Object117", 
    "lastpost": "about 1 hour ago" 
} 

인 다음과 같아야합니다.

{ 
    "title": "Title number 1", 
    "author": "pursu", 
    "lastpost": "9 minutes ago" 
} 
{ 
    "title": "Title number 2", 
    "author": "colinatx", 
    "lastpost": "9 minutes ago" 
} 
{ 
    "title": "Title number 3", 
    "author": "Object117", 
    "lastpost": "about 1 hour ago" 
} 

내 자바 스크립트 :

var express = require('express'); 
var fs = require('fs'); 
var request = require('request'); 
var cheerio = require('cheerio'); 
var app  = express(); 

app.get('/scrape', function(req, res){ 

    //This is the URL to pull data from 
    url = 'http://www.pedalroom.com/forums/marketplace'; 

    // The first parameter is our URL 

    // The callback function takes 3 parameters, an error, response status code and the html 
    request(url, function(error, response, html){ 
      if(!error){ 

       //pulling HTML 
      var $ = cheerio.load(html); 

       //Variables that capture data 
      var title, author, lastpost; 
      var json = { title : "", author : "", lastpost : ""}; 

      $('.title').filter(function(){ 

       var data = $(this); 

       title = data.children().first().text(); 

       json.title = title; 
      }) 
      $('.author').filter(function(){ 

       var data = $(this); 

       author = data.children().first().text(); 

       json.author = author; 
      }) 
      $('.last_post').filter(function(){ 

       var data = $(this); 

       lastpost = data.text(); 

       json.lastpost = lastpost; 
      }) 
    } 
     fs.writeFile('output.json', JSON.stringify(json, null, 4), function(err){ 

      console.log('File successfully written! - Check your project directory for the output.json file'); 

     }) 

     // Finally, we'll just send out a message to the browser reminding you that this app does not have a UI. 
     res.send('Check your console!') 

    }); 
}) 

app.listen('8081') 
console.log('Magic happens on port 8081'); 
exports = module.exports = app; 

내가 필요 어떻게 든 루프 코드 나 다른 아마도 뭔가 있나요?

답변

3

코드에서 첫 줄의 첫 번째 요소 만 잡습니다. 각 줄마다 반복하지 않기 때문입니다.

var express = require('express'); 
var fs = require('fs'); 
var request = require('request'); 
var cheerio = require('cheerio'); 
var app  = express(); 

app.get('/scrape', function(req, res){ 

    //This is the URL to pull data from 
    url = 'http://www.pedalroom.com/forums/marketplace'; 

    // The first parameter is our URL 

    // The callback function takes 3 parameters, an error, response status code and the html 
    request(url, function(error, response, html){ 
     if(!error){ 

      //pulling HTML 
      var $ = cheerio.load(html); 

      var data = []; 

      /** 
      * New code starts here 
      */ 
      // For each row of the table 
      $('.topics tr').each(function(index, element){ 

       // If title is present on this line, write it into the json 
       if($(this).find('.title a').length > 0) 
        data.push({ 
         title: $(this).find('.title a').html(), 
         author: $(this).find('.author a').html(), 
         lastpost: $(this).find('.last_post').html() 
        }); 
      }); 
      /** 
      * Ends here :D 
      */ 
     } 
     fs.writeFile('output.json', JSON.stringify(data, null, 4), function(err){ 

      console.log('File successfully written! - Check your project directory for the output.json file'); 

     }) 

     // Finally, we'll just send out a message to the browser reminding you that this app does not have a UI. 
     res.send('Check your console!') 

    }); 
}) 

app.listen('8081') 
console.log('Magic happens on port 8081'); 
exports = module.exports = app; 
: 여기

는 작업 코드