带有 nodejs 和 cheerio 的 Web Scraper?

Web Scraper with nodejs and cheerio?

大家好,我真的被困在这里太接近解决方案了:/我正在尝试创建网络抓取脚本。

到目前为止我有:

但我一直在尝试获取元素。 到目前为止,这是我的工作代码:

var http = require('http');
var request = require('request');
var cheerio = require('cheerio');

http.createServer(function (req, res) {
   request('http://www.xscores.com/soccer', function   (error, response, 
html) {
   if (!error && response.statusCode == 200) {
       var $ = cheerio.load(html);
       res.writeHead(200, { 'Content-Type':'text/plain'});
       res.end('html:'+html);

   }
 }); }).listen(8080); console.log('Server is running at 
http://178.62.253.206:8080/');

这仍然是 Wip 进度,我还没有设置任何数据库,所以总体计划是将所有这些信息加载到我的服务器页面上的表或 div 元素中。

我想知道如何循环遍历主队所在 class "score_home_txt score_cell wrap" 的 xscores 元素,并在我的服务器上显示这些元素?

它是这样构建的:

<div class="score_teams  score_cell">
<div class="score_home score_cell">
<div class="score_home_txt score_cell wrap">
TRACTOR SAZI
</div>

我以前用 excel VBA 做这个过程,用 cheerio 做这个是一种全新的体验。

任何帮助都将不胜感激

弗雷德里克

这是循环显示名称的方法:

var http = require('http');
var request = require('request');
var cheerio = require('cheerio');

http.createServer(function (req, res) {
    request('http://www.xscores.com/soccer', function (error, response,
        html) {
        if (!error && response.statusCode == 200) {
            var $ = cheerio.load(html);
            var list_items = "";
            $('div.score_home_txt.score_cell.wrap').each(function (i, element) {
                var a = $(this).text();
                list_items += "<li>" + a + "</li>";
                console.log(a);
            });
            var html = "<ul>" + list_items + "</ul>"
            res.writeHead(200, {
                'Content-Type': 'text/html'
            });
            res.end(html);
        }
    });
}).listen(8080);
console.log('Server is running at http://178.62.253.206:8080/');