Node.js 如何使用ChereIO访问对象内部属性的值

Node.js 如何使用ChereIO访问对象内部属性的值,node.js,web-scraping,cheerio,Node.js,Web Scraping,Cheerio,我试图使用ChereIO将属性(“数据代码”)的值推送到数组中。但是,我不断收到错误消息“allAs[I].attr不是函数” 这是我到目前为止所拥有的 const express = require('express'); const request = require('request'); const cheerio = require('cheerio'); const app = express(); app.get('/scrape', (req, res) => {

我试图使用ChereIO将属性(“数据代码”)的值推送到数组中。但是,我不断收到错误消息“allAs[I].attr不是函数”

这是我到目前为止所拥有的

const express = require('express');
const request = require('request');
const cheerio = require('cheerio');
const app = express();

app.get('/scrape', (req, res) => {
    const url = 'http://store.emart.com/branch/list.do?id=1702';    

    request(url, (err, response, body) => {
        if(!err) {
            var idList = [];
            console.log(typeof(idList));
            var $ = cheerio.load(body);
            var allAs = $("a").filter("[data-code]");
            console.log(allAs[0].val);
            for(var i = 0; i < allAs.length; i++){
                //console.log(allAs[i]);
               idList.push(allAs[i].attr("[data-code]"));
            }
            console.log();

            res.send(body);
        } else {
            console.log("problems yo");
        }
    });
});


app.listen(3000, () => {
    console.log("Server is up and running!!!");
});
const express=require('express');
const request=require('request');
const cheerio=需要(“cheerio”);
常量app=express();
app.get('/scrape',(请求,res)=>{
常量url=http://store.emart.com/branch/list.do?id=1702';    
请求(url,(错误、响应、正文)=>{
如果(!err){
var idList=[];
console.log(typeof(idList));
var$=总负荷(车身);
var allAs=$(“a”).filter(“[数据代码]”);
console.log(allAs[0].val);
对于(变量i=0;i{
log(“服务器已启动并运行!!!”;
});

应该有330个结果被推到闲置状态

根据您的代码,更改以下内容:

idList.push(allAs[i].attr(“[data code]”)

。。。到

idList.push(allAs[i].attribs['data-code'])

-


*我上次使用cheerio已经有一段时间了,所以我不确定这是应该的还是一个bug

没错!非常感谢。我没有意识到attr不是一个函数,尽管它实际上告诉了我。我感到宽慰,但也很尴尬!