nodejs抓取动态网页(我能够成功地抓取一个网页,然后提取我需要的信息)
优采云 发布时间: 2022-03-28 15:21nodejs抓取动态网页(我能够成功地抓取一个网页,然后提取我需要的信息)
这就是我想要完成的。我能够成功抓取一个网页,然后提取我需要的信息,我已经在几个 网站 上运行了这个页面,其中分页链接在 href 属性中很容易获得。我的问题是当分页变量是动态的时如何导航到下一页:
1
2
Next Page
到目前为止,这里的代码是我为其他网站工作
所做的
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');
此外,count和status的值永远不会显示在生成的JSON文件中。不确定我在这里做错了什么,但我们将不胜感激。
谢谢!