这就是我要实现的目标。我能够成功地抓取一个网页,然后提取我需要的信息,并且我已经在几个网站上运行了这一步,其中的分页链接在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文件中。不知道我在这里做错了什么,但肯定会感谢所有人的帮助。
谢谢!
发布于 2017-01-31 07:12:39
你试过用.paginate('ul li:nth-child(3) a@href')吗?
通过这种方式,您可以获得<ul>中的第三个<li>。
https://stackoverflow.com/questions/41946676
复制相似问题