我正在尝试抓取Google新闻,但是我使用的代码有问题:

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


 // Define the requests url

 var url = "https://news.google.com/news";

request(url,function(err,res,body){

var news=[];
 var $page = cheerio.load(body);

var $url=$page('table[class="esc-layout-table"]').find('tbody > tr > td > div > h2 > a').each(function (index, element) {
  news.push($page(element).attr('href'));
});
news={
//desc:$desc,
    url:$url,
//    img:$img,
};



console.log ('success ....'news);



});


我想链接到文章和标题以及缩略图。我将存储在firebase数据库中
但不幸的是,我在其余的控制台控制台中迷路了
我无法获得我期望的结果,相反,我在控制台中得到了这个:

    success .... { '0':
   { type: 'tag',
     name: 'a',
     attribs:
      { target: '_blank',
        class: 'article usg-AFQjCNFxodYTzKo8-hM57511iQgBdfn8xA did-3230940966728164415',
        href: 'https://www.washingtonpost.com/news/post-nation/wp/2016/12/15/jurors-begin-deliberating-in-charleston-church-shooting-trial/',
        url: 'https://www.washingtonpost.com/news/post-nation/wp/2016/12/15/jurors-begin-deliberating-in-charleston-church-shooting-trial/',
        id: 'MAA4AEgAUABgAWoCdXM',
        ssid: 'h' },
     children: [ [Object] ],
     next: null,
     prev: null,
     parent:
      { type: 'tag',
        name: 'h2',
        attribs: [Object],
        children: [Object],
        next: null,
        prev: null,
        parent: [Object] } },
  '1':
   { type: 'tag',
     name: 'a',
     attribs:
      { target: '_blank',
        class: 'article usg-AFQjCNEQY4otecPJJevDyoBp3K-IQnes2w did-141563424311867977',
        href: 'http://www.businessinsider.com/facebook-will-fact-check-label-fake-news-in-news-feed-2016-12',
        url: 'http://www.businessinsider.com/facebook-will-fact-check-label-fake-news-in-news-feed-2016-12',
        id: 'MAA4AEgBUABgAWoCdXM',
        ssid: 'h' },
     children: [ [Object] ],
     next: null,
     prev: null,
     parent:
      { type: 'tag',
        name: 'h2',
        attribs: [Object],
        children: [Object],
        next: null,
        prev: null,
        parent: [Object] } },
  '2':
   { type: 'tag',
     name: 'a',
     attribs:
      { target: '_blank',
        class: 'article usg-AFQjCNHyGG4zl4RW-AoIILTssJX_TKCybg did--2293954291931624250',
        href: 'http://www.bbc.com/news/world-middle-east-38329461',
        url: 'http://www.bbc.com/news/world-middle-east-38329461',
        id: 'MAA4AEgCUABgAWoCdXM',
        ssid: 'h' },
     children: [ [Object] ],
     next: null,
     prev: null,
     parent:
      { type: 'tag',
        name: 'h2',
        attribs: [Object],
        children: [Object],
        next: null,
        prev: null,
        parent: [Object] } },
  '3':


注意:这只是结果的一小部分,如果您运行代码,您将清楚明白我的意思
谢谢大家的建议。

最佳答案

不确定这是您想要的是什么,但是尝试一下,我得到了所有想要的网址

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


 // Define the requests url

 var url = "https://news.google.com/news";

request(url,function(err,res,body){

var news=[];

var $= cheerio.load(body);

$('table[class="esc-layout-table"]').each(function () {
  var image = $(this).find('tbody > tr > .esc-layout-thumbnail-cell > .esc-thumbnail-wrapper > .esc-thumbnail-state > .esc-thumbnail > a > .esc-thumbnail-image-wrapper > img').attr('src');
  var title = $(this).find('tbody > tr > .esc-layout-article-cell > .esc-lead-article-title-wrapper > .esc-lead-article-title > a > span').text();
  var url = $(this).find('tbody > tr > .esc-layout-article-cell > .esc-lead-article-title-wrapper > .esc-lead-article-title > a').attr('href');
  news.push({ url, title, image });
});


console.log ('success ....', news);



});


结果

success .... [ { url: 'http://www.cbc.ca/news/canada/saskatchewan/prisoner-killed-sask-penitentiary-1.3898175',
    title: '1 dead, at least 8 injured in Saskatchewan prison riot',
    image: '//t2.gstatic.com/images?q=tbn:ANd9GcRKhvczSgL4g3dO8EHkruAEB5AoqkI-PvbB8LzlHBZTPGJYh4bEooNKApDXqTzboNrLpqv3H7MG' },
  { url: 'http://www.cbc.ca/news/world/aleppo-convoy-evacuation-1.3895602',
    title: '3000 people evacuated from eastern Aleppo so far as fragile ceasefire holds',
    image: '//t2.gstatic.com/images?q=tbn:ANd9GcQ86WfdQJVFE4GrQvu_CPrjx3sqhqut0gjBRv6opfVA4JLIqsWeBDjRWURFGd7h_XN_0D0DnoQ5' },
  { url: 'http://www.cp24.com/news/five-family-members-dead-following-first-nation-fire-1.3204960',
    title: 'Five family members dead following First Nation fire',
    image: '//t1.gstatic.com/images?q=tbn:ANd9GcS6DkmtVrLs4wzVLDfNZvfOm9Js6rXvSg8ttjdoofJwWUZkM2wSjvLA-HpVZdTJN7pG-1FubXI' },
  { url: 'http://www.ctvnews.ca/business/b-c-offers-five-year-interest-free-down-payment-loans-to-first-time-buyers-1.3205119',
    title: 'BC offers five-year, interest-free down-payment loans to first-time buyers',
    image: '//t3.gstatic.com/images?q=tbn:ANd9GcR2gUEW4E0gtt5Sj-jFIJP0iC1JIIZ3qi5RbpbwD7otN7B5nKf8qXT-Q1Aaxcs5Z7FVn-LhNXU5' },


希望可以为您提供帮助:)

关于javascript - 搜刮Google新闻,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41174231/

10-09 22:26