2017-07-30 4 views
1

cheerio.jsを使用して、このサイトのウィスキー名、image_url、および説明を掻き取ろうとしています:https://www.thewhiskyexchange.com/c/33/american-whiskey?filter=true#productlist-filter私はその情報をMongoDBに格納するJSONオブジェクトの配列に変換したいと思います。サイト全体のHTMLを示しているが、ここでは順不同リストの関連する基本的な構造の一部であることはできません。cheerio.jsで掻爬して、取得中:エラー:一時停止中にのみ操作を実行できます

<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> 

はちょうど<span>から任意のテキストなしで、<p class="name">でウイスキーの名前を取得しようとオフ開始タグは、私は、ブラウザのコンソールで、このjQueryのコードを使用し、それは私が必要とするまさに私を取得します。

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}!`); 

$('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); 
}) 

はチェリオで私のアプリファイル(ウイスキー-scraper.js)で同じコードを試します

私はターミナルでnode inspect whiskey-scraper.jsを実行すると、コンソールは200のステータスコードをログに記録するだけでなく、このエラーを記録します:

"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)" 

が、これは何を意味するのか、またはこのエラーを回避する方法を見つけ出すことはできません。どのようにこのエラーを排除し、少なくとも私のconsole.log(whiskeys);行を得るための任意のアイデアですか?私はそれを得ることができれば、私はそこから取ることができます。

私はコメントを外すときconsole.log(body);サイトのHTML全体がコンソールに記録されるので、cheerioがサイトから必要な情報を得ていると感じます。このエラーが解消されると、image_urlと説明を取得してMongoDBに取り込むことができます。

ありがとうございました!

答えて

0

これを解決する方法を解説しました。ウェブサイトでは、ウィスキーとその情報をグリッド形式またはリスト形式で表示することができます。これらの情報はまったく同じURLです。私は<ul><li>形式を使用するリスト形式のHTMLを見ていましたが、cheerioはグリッド形式をインポートすることを選択しました。順序付けされていないリストはなく、複数のネストされた<div>です。決してそれを考えていない!

関連する問題