gpt4 book ai didi

javascript - 抓取谷歌新闻

转载 作者:行者123 更新时间:2023-12-02 13:48:36 26 4
gpt4 key购买 nike

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

 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 - 抓取谷歌新闻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41174231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com