首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用google云功能进行抓取,它使用状态代码: 304完成

使用google云功能进行抓取,它使用状态代码: 304完成
EN

Stack Overflow用户
提问于 2018-07-05 17:19:38
回答 1查看 3.9K关注 0票数 6

我正在尝试谷歌云功能,它工作,但结束状态代码304不确定是什么原因。下面是密码,

代码语言:javascript
运行
复制
//gcloud beta functions deploy scrapeGitCollection --trigger-http
var cheerio = require('cheerio');
var request = require('request');

function getDateTime() {

    var date = new Date();

    var hour = date.getHours();
    hour = (hour < 10 ? "0" : "") + hour;

    var min  = date.getMinutes();
    min = (min < 10 ? "0" : "") + min;

    var sec  = date.getSeconds();
    sec = (sec < 10 ? "0" : "") + sec;

    var year = date.getFullYear();

    var month = date.getMonth() + 1;
    month = (month < 10 ? "0" : "") + month;

    var day  = date.getDate();
    day = (day < 10 ? "0" : "") + day;

    return year + "/" + month + "/" + day + " " + hour + ":" + min + ":" + sec;

  }  

var scrape = new Promise((resolve, reject) =>{
    var text;
    var array = [];

    request({
        method: 'GET',
        url: 'https://github.com/collections'
    }, function(err, response, body) {
        if (err) return reject(err);

        // Tell Cherrio to load the HTML
        $ = cheerio.load(body);
        $('.col-10 h2 a').each(function(i, element) {
            var node = $(this);
            text = node.text();
            //console.log(text);
            array.push(text);
        });

        text = JSON.stringify(array);
        console.log(text);
        resolve(text);
    });     
});

// [START functions_helloworld_http]
/**
 * HTTP Cloud Function.
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
 *
*/
exports.scrapeGitCollection = (req, res) => {
    console.log('Triggered @ '+getDateTime());
    scrape.then((data) =>{
        res.send(`Hello ${data || 'World'}!`);
    }).catch( (errorMessage) =>{
        console.error(errorMessage);
    });

  };
// [END functions_helloworld_http]

这是我在堆栈驱动程序中看到的日志。

代码语言:javascript
运行
复制
2018-07-05 22:19:39.181 IST
scrapeGitCollection
x6mbdi17rdj7
Function execution took 7 ms, finished with status code: 304
{
 insertId:  "000000-4f8925ba-a5a5-4f0e-9c32-906e91374e32"  
 labels: {…}  
 logName:  "projects/btd-in-16062018/logs/cloudfunctions.googleapis.com%2Fcloud-functions"  
 receiveTimestamp:  "2018-07-05T16:49:45.425756526Z"  
 resource: {…}  
 severity:  "DEBUG"  
 textPayload:  "Function execution took 7 ms, finished with status code: 304"  
 timestamp:  "2018-07-05T16:49:39.181731257Z"  
}

出于调试的目的,我在scrape()函数中添加了下面一行,它没有出现在日志中。

代码语言:javascript
运行
复制
console.log(text);

如果我强制这样说,我会得到上面的console.log文本和结果。

代码语言:javascript
运行
复制
res.status(200).send(`Hello ${data || 'World'}!`);

我不想强行说状态(200)。我想解决我早些时候得到的304。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-06 15:45:30

根据Mozilla的文档,304状态码表示请求被重定向,很可能是缓存的资源。我认为这与响应的主体总是相同有关,因为如果您将响应更改为

代码语言:javascript
运行
复制
res.send(`Hello ${data || 'World'}!` + getDateTime());

然后,您将看到每次响应代码为200。

请注意,304不是一个错误。错误状态代码为400及以上

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51196873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档