我无法使用nodejs从网站获取url

问题描述 投票:0回答:1

我希望使用下面的代码从该网站抓取网址:

var request = require("request");
cheerio = require("cheerio");
urls = [];

request("http://news.sabay.com.kh/topics/sport", function(err, resp, body){
    if(!err && resp.statusCode ==200){
        var $ = cheerio.load(body);
        $(".article","h4.title").each(function(){
            var url = this.attr("href");
            urls.push(url);
        });
        console.log(urls);
    }
});

但我无法得到结果。当我跑步时我得到了这个

$ node server.js
[]
node.js web-scraping web-crawler
1个回答
1
投票

首先,使用合适的 CSS 选择器:

.article h4.title > a

然后,使用正确的字段:

var url = this.attribs.href

这给出了:

var request = require("request");
cheerio = require("cheerio");
urls = [];

request("http://news.sabay.com.kh/topics/sport", function(err, resp, body){
    if(!err && resp.statusCode ==200){
        var $ = cheerio.load(body);
        $(".article h4.title > a").each(function(){
            var url = this.attribs.href;
            urls.push(url);
        });
        console.log(urls);
    }
});

和输出:

[ 'http://news.sabay.com.kh/article/546826',
  'http://news.sabay.com.kh/article/546763',
  'http://news.sabay.com.kh/article/546520',
  'http://news.sabay.com.kh/article/546568',
  'http://news.sabay.com.kh/article/546460',
  'http://news.sabay.com.kh/article/546448',
  'http://news.sabay.com.kh/article/545674',
  'http://news.sabay.com.kh/article/546235',
  'http://news.sabay.com.kh/article/545698',
  'http://news.sabay.com.kh/article/546091' ]
© www.soinside.com 2019 - 2024. All rights reserved.