nodejs / cheerio / x-ray中的动态链接

时间:2017-01-30 22:39:28

标签: javascript node.js phantomjs cheerio x-ray

这是我想要完成的。我能够成功地抓取一个网页,然后提取我需要的信息,我已经在几个网站上运行了这个网页,其中分页链接在href属性中随时可用。我的问题是当分页变量是动态的时候如何导航到下一页:

<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: 'div.grid@id',    // 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');

此外,count和status的值永远不会显示在生成的JSON文件中。不知道我在这里做错了什么,但肯定会感谢所有的帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

你试过.paginate('ul li:nth-child(3) a@href')吗?

通过这种方式,您可以获得<li>中的第三个<ul>

相关问题