(Cheerio/Python) && 这段代码中阻止我抓取这个网站的错误在哪里?

问题描述

var request = require('request');
request('https://www.nasdaq.com/market-activity/pre-market',function (error,response,body) {
  if (!error && response.statusCode != 200) {
    console.log(body); // Print the web page.
  }
});

我在 Python 中使用 CHEERIO。

所以这个测试代码 ^ 已经不适用于网络抓取整个 html 文档模型。 我想要实现的是将第一个图表中的数据从 https://www.nasdaq.com/market-activity/pre-market 获取到单个数组中,以便我可以将其与另一个数据集合并。

这是我正在尝试修复的代码,但我不清楚我的错误在哪里,因为它不会用我的测试代码解析网站本身。

我正在尝试将股票代码放入股票代码数组,依此类推,使用我创建的 5 个数组。数组中应该只有 10 个项目,因为这是网站图表中开始的项目数。

心胸开阔!请随时让我知道如何做得更好。


var request = require('request'),cheerio = require('cheerio');

/////////////////////////////////////////////////
// Code Guide:                                 //
// URL: https://github.com/mikeal/request      //
// https://www.youtube.com/watch?v=LJHpm0J688Y //
///////////////////////////////////////////////////////
// information From:                                 //
// https://www.nasdaq.com/market-activity/pre-market //
// ////////////////////////////////////////////////////

Tickers = [];
Names = [];
Lasts = [];
Changes = [];
Share_Volumes = [];


request('https://www.nasdaq.com/market-activity/pre-market',function(err,resp,body){
    console.log('Hxxxxxxxxxxo!');
    if(!error && resp.statusCode == 200){
        var $ = cheerio.load(body);
        console.log('Hexxxxxx!');
        //Ticker
        $('extended-Trading-dynamic-tables__cell extended-Trading-dynamic-tables__cell--heading','#main-content').each(function(){
            var Ticker = this.text();
            Tickers.push(Ticker);
        });

        //Name
        $('extended-Trading-dynamic-tables__cell','#main-content').each(function(){
            var Name = this.text();
            Names.push(Name);
        });

        //Last
        $('extended-Trading-dynamic-tables__cell extended-Trading-dynamic-tables__cell-rightalign','#main-content').each(function(){
            var Last = this.text();
            Lasts.push(Last);
        });

        //Change
        $('extended-Trading-dynamic-tables__cell extended-Trading-dynamic-tables__cell-upcaret extended-Trading-dynamic-tables__cell-rightalign','#main-content').each(function(){
            var Change = this.text();
            Changes.push(Change);
        });

        //Share Volume
        $('extended-Trading-dynamic-tables__cell extended-Trading-dynamic-tables__cell-rightalign','#main-content').each(function(){
            var Share_Volume = this.text();
            Share_Volumes.push(Share_Volume);
        });

        console.log(Tickers.length);
        console.log(Names.length);
        console.log(Lasts.length);
        console.log(Changes.length);
        console.log(Share_Volumes.length);



    }
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)