搜索
node.js - 用node写爬虫的时候为啥有时能请求到数据,有时又请求不到数据啊?
PHP中文网
PHP中文网 2017-04-17 11:36:02
[Node.js讨论组]

刚开始是这样的

var http=require('http');
function download(url, callback) {
    http.get(url, function(res) {
        console.log(url);
        var data = "";
        res.on('data', function (chunk) {
            console.log(data+'first'); //测试是否chunk没请求到
            console.log(chunk);
            console.log(1);
            data += chunk;
            console.log(data+'two');
            console.log(chunk);
        });
        res.on("end", function() {
            console.log(2);
            callback(data);
        });
    }).on("error", function(e) {
        callback(null);
    });
}
exports.download = download; 

然后请求到空数据时,日志前面打印为

first <Buffer 3c 73 63
> 72 69 70 74 3e 64 6f 63 75 6d 65 6e 74 2e 6c 6f 63 61 74 69 6f 6e 20
> 3d 20 22 68 74 74 70 3a 2f 2f 61 71 69 63 6e 2e 6f 72 67 2f 63 69 74
> 79 2f …> 1 <script>document.location =
> "http://aqicn.org/city/beijing/";</script>two <Buffer 3c 73 63 72 69
> 70 74 3e 64 6f 63 75 6d 65 6e 74 2e 6c 6f 63 61 74 69 6f 6e 20 3d 20
> 22 68 74 74 70 3a 2f 2f 61 71 69 63 6e 2e 6f 72 67 2f 63 69 74 79 2f
> ...> 2 而正常情况是: first <Buffer 3c 73 63 72 69 70 74 3e 64 6f 63 75 6d 65
> 6e 74 2e 6c 6f 63 61 74 69 6f 6e 20 3d 20 22 68 74 74 70 3a 2f 2f 61
> 71 69 63 6e 2e 6f 72 67 2f 63 69 74 79 2f ...> 1
> ----html文档,省略很多字-------- <Buffer------------------------> ----html文档,省略很多字------ <Buffer------------------------> 1 ----html文档,省略很多字-------- <Buffer------------------------> ----html文档,省略很多字------ <Buffer------------------------>

后来换一种方式

> var request = require('request'); function download(url){
> request.get({url:url, timeout: 3000}, function (error, response, body)
> {
>     if (!error && response.statusCode == 200) {
>         console.log(body);
>           } else {
>         console.log('not success');
>     } }); }

依然有时候请求到的数据为空啊
求大神指教啊

PHP中文网
PHP中文网

认证0级讲师

全部回复(1)
PHP中文网

要看你爬的对象,有的网站会防止你爬,比如用cookies之类, 或者需要执行js后才能拿到数据, 所以,先用firebug仔细的看下浏览器的请求

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号