刮cheerio.js,得到:错误:只能在暂停时执行操作

试图从这个网站刮取威士忌的名字,image_url和描述: https ://www.thewhiskyexchange.com/c/33/american-whiskey ? filter = true#productlist-filter using cheerio.js。 我想把这些信息转换成一个JSON对象数组来存储在我的MongoDB中。 无法显示整个网站的html,但这里是无序列表的相关基本结构的一部分:

<body> <div class="siteWrapper"> <div class="wrapper"> <div class="products-wrapper"> <ul class="products-list"> <li> <a> <div class="product-content"> <div class="information"> <p class="name"> " Jack Daniel's Old No. 7" <span>Small Bottle</span> </p> </div> </div> </a> </li> <li></li> <li></li> etc. </all closing tags> 

刚开始试图在<p class="name">得到威士忌的名字,没有任何来自<span>标签的文本,我在浏览器控制台中使用了这个jQuery代码,它正是我所需要的:

 $('ul.products-list > li').each(function(index) { const nameOnly = $(this).find('a div div.information p.name').first().contents().filter(function() { return this.nodeType == 3; }).text(); const whiskeyObject = {name: nameOnly}; const whiskeys = JSON.stringify(whiskeyObject); console.log(whiskeys); }) 

尝试在我的应用程序文件(whiskey-scraper.js)与cheerio相同的代码:

 const express = require('express'); const request = require('request'); const cheerio = require('cheerio'); const fs = require('fs'); const app = express(); const port = 8000; request('https://www.thewhiskyexchange.com/c/33/american-whiskey?filter=true#productlist-filter', function(error, response, body) { if(error) { console.log("Error: " + error); } console.log("Status code: " + response.statusCode); const $ = cheerio.load(body); // console.log(body); $('ul.products-list > li').each(function(index) { const nameOnly = $(this).find('a div div.information p.name').first().contents().filter(function() { return this.nodeType == 3; }).text().trim(); const whiskeyObject = {name: nameOnly}; const whiskeys = JSON.stringify(whiskeyObject); console.log(whiskeys); }) }); app.listen(port); console.log(`Stuff is working on Port ${port}!`); 

当我运行node inspect whiskey-scraper.jsterminal中node inspect whiskey-scraper.js时,控制台logging了一个200的状态码,但是也logging了这个错误:

 "Error: Can only perform operation while paused. - undefined at _pending.(anonymous function) (node- inspect/lib/internal/inspect_client.js:243:27) at Client._handleChunk (node-inspect/lib/internal/inspect_client.js:213:11) at emitOne (events.js:96:13) at Socket.emit (events.js:191:7) at readableAddChunk (_stream_readable.js:178:18) at Socket.Readable.push (_stream_readable.js:136:10) at TCP.onread (net.js:561:20)" 

无法弄清楚这是什么意思或如何解决这个错误。 任何想法如何消除这个错误,至less得到我的console.log(whiskeys); 在线工作? 如果我能做到这一点,我可以从那里拿走。

当我取消注释console.log(body); 我得到整个网站的htmllogging到控制台,所以我觉得cheerio正在从网站获取我需要的信息。 一旦我消除这个错误,我可以弄清楚获取image_url,描述,并把它放到我的MongoDB中。

谢谢!

找出解决scheme。 对于网站,您可以显示威士忌及其信息的网格格式或列表格式 – 他们是完全相同的url。 我正在查看列表格式的HTML,它使用<ul><li>格式,但cheerioselect导入网格格式,其中没有无序列表,只是多个嵌套<div> s。 从来没有想到这一点!