首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nodejs/ cheerio/ x-ray中的动态链接

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

Stack Overflow用户
提问于 2017-01-31 06:39:28
回答 1查看 568关注 0票数 1

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

代码语言:javascript
复制
<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>

到目前为止,这里的代码是我在其他网站上使用的代码

代码语言:javascript
复制
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文件中。不知道我在这里做错了什么,但肯定会感谢所有人的帮助。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-01-31 07:12:39

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41946676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档