2016-02-28 83 views
0

当试图运行代码时,我不断收到错误$.find('.market_listing_item_name_block').each() - undefined不是一个函数,指向查找。我觉得这个发现是cheerio的一个功能?为了公平起见,我不知道,如果我连这样做的权利,这是我的代码:Cheerio错误,undefined不是函数//这是正确的方法吗?

var cheerio = require('cheerio') 
$ = cheerio.load('#searchResultsRows') 
var url = 'http://steamcommunity.com/market/search?appid=730' 
xhr.get(url).success(function(r){ 
    $.find(".market_listing_item_name_block").each(function(){ 
     var name = $(this).find(".market_listing_item_name").text(); 
     console.log(name) 
    }) 
}) 

XHR是一个对象,本质上就像AJAX。

我是这样做之前,镀铬的方式,为:

var url = 'http://steamcommunity.com/market/search?appid=730' 
var itemDiv = $("<div></div>") 
$.get(url).success(function(r){ 
    d = $(r).find('#searchResultsRows') 
    itemDiv.append(d) 
}) 

然后:

itemDiv.find(".market_listing_item_name_block").each(function(){ 
    var name = $(this).find(".market_listing_item_name").text(); 
    console.log(name) // would normally do other things, but for the purpose of this post, i'm just console logging the name 
}) 

我究竟是如何将能够重新创建^在节点/ cheerio?我相信我明显缺少了几个步骤。任何帮助非常感谢,谢谢。

+0

可能会发现不返回一个对象,将有each'的'的方法之前,使用换行元素与$,让您的查询来回空。你确定这是一个正确的调用:'$ .find(“。market_listing_item_name_block”)'?它不应该像下面的代码那样:'$(r).find(“。market_listing_item_name_block”)'? – guessimtoolate

+0

做一个'console.log($。find(“。market_listing_item_name_block”));'并且看看它是否有方法。每个,并且在这里粘贴结果 – Datsik

+0

@Datsik用$ .find做它仍然返回undefined不是一个函数,现在作为guessimtoolate说,我试过$(r).find并返回一个对象。 https://i.gyazo.com/4eeb0a86cc7f3c351347c52c9503a9ab.png – Furdew

回答

1

当你得到一个元素,你需要做进一步选择

const $ = Cheerio.load(body) 
const list = $('div.titles li') 

list.each((index, li) => { 
    const title = $(li).find('p.title').text() 

    console.log(title) 
}) 
相关问题