1

这是我想要完成的。我能够成功地抓取一个网页,然后提取我需要的信息,并且我已经在几个网站上运行了这个,其中分页链接在 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');

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

谢谢!

4

1 回答 1

0

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

这样你就得到了第三<li><ul>

于 2017-01-30T23:12:38.660 回答