2017-01-30 29 views
1

這是我正在努力完成的。我能夠成功地抓取一個網頁,然後提取我需要的信息,並且我已經在一些網站上運行這個分頁鏈接,這些分頁鏈接在href屬性中很容易找到。我的問題是如何導航到下一個頁面時的分頁變量是動態的:nodejs/cheerio/x-ray中的動態鏈接

<ul> 
    <li> 
     <a class="clickPage" href="javascript:previousPage()">1</a> 
    </li> 
    <li> 
     <a class="clickPage active" href="javascript:currentPage()">2</a> 
    </li> 
    <li> 
     <a class="clickPage" href="javascript:nextPage()">Next Page</a> 
    </li> 

到目前爲止,這裏的代碼是什麼,我有其他網站

var request = require('request'),  // simplified HTTP request client 
    cheerio = require('cheerio'),  // lean implementation of core jQuery 
    Xray = require('x-ray'),   // 
    x = Xray(), 
    fs = require('fs');     // file system i/o 

/* 
    TODO: Make this feature dynamic, to take in the URL of the page 
    var pageUrl; 
*/ 

var status = 'for sale'; 
var counter = 0; 

x('http://www.example.com/results/1', '.results', [{ 
    id: '[email protected]', // extracts the value from the attribute id 
    title: 'div.info h2', 
    category: 'span.category', 
    price: 'p.price', 
    count: counter+1, // why doesnt this update? this never shows in the json 
    status: status  // this value never shows up in the json 
}]) 
    .paginate(whatShouldThisBe) 
    .limit(800) 
    .write('products.json'); 

也算值工作,狀態永遠不會顯示在生成的JSON文件中。不知道我在這裏做錯了什麼,但肯定會感謝所有幫助。

謝謝!

回答

0

您是否嘗試過.paginate('ul li:nth-child(3) [email protected]')

通過這種方式,您可以在<ul>中獲得第三個<li>

+0

謝謝你讓我知道。我曾經嘗試這樣做: '.paginate( 'UL李:第n個孩子一個@ HREF')' 但是我注意到你省略了屬性值( 「@href」)。有沒有完成的原因?只是一個溫和的提醒,鏈接即時創建(onClick)。 – johnanish