Javascript 论坛标题网页刮刀

Javascript 论坛标题网页刮刀,javascript,jquery,node.js,web-scraping,cheerio,Javascript,Jquery,Node.js,Web Scraping,Cheerio,我正在写一个简单的网页刮板,从论坛上获取文章标题、用户名和最后一次发帖时间 问题是,scraper只提取表中的最后一个条目 例如: 如果表格的结构是这样的: 0 9分钟前 0 9分钟前 11 大约一小时前 写入.json输出文件的结果如下 { "title": "Title number 3", "author": "Object117", "lastpost": "about 1 hour ago" } 相反,它应该是这样的: { "title": "Ti

我正在写一个简单的网页刮板,从论坛上获取文章标题、用户名和最后一次发帖时间

问题是,scraper只提取表中的最后一个条目

例如: 如果表格的结构是这样的:


0
9分钟前
0
9分钟前
11
大约一小时前
写入.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"
}
我的JavaScript:

var express=require('express');
var fs=需要('fs');
var请求=要求(“请求”);
var cheerio=需要('cheerio');
var-app=express();
app.get('/scrape',函数(req,res){
//这是从中提取数据的URL
url='1〕http://www.pedalroom.com/forums/marketplace';
//第一个参数是我们的URL
//回调函数接受3个参数:错误、响应状态代码和html
请求(url、函数(错误、响应、html){
如果(!错误){
//提取HTML
var$=cheerio.load(html);
//捕获数据的变量
var标题、作者、最后一篇文章;
var json={title:,author:,lastpost:};
$('.title').filter(函数(){
var数据=$(此);
title=data.children().first().text();
json.title=标题;
})
$('.author').filter(函数(){
var数据=$(此);
author=data.children().first().text();
json.author=作者;
})
$('.last_post').filter(函数(){
var数据=$(此);
lastpost=data.text();
json.lastpost=lastpost;
})
}
fs.writeFile('output.json',json.stringify(json,null,4),函数(err){
log('File successfully writed!-检查项目目录中的output.json文件');
})
//最后,我们将向浏览器发送一条消息,提醒您此应用程序没有UI。
res.send('检查控制台!')
});
})
应用程序监听('8081')
log('Magic cours on port 8081');
导出=模块。导出=应用程序;

是我需要以某种方式循环代码还是其他东西?

在代码中,您只捕获第一行的第一个元素,因为您没有在每一行上循环

以下是工作代码:

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;