js爬取分页数据
时间: 2023-08-15 19:04:49 浏览: 98
要使用JavaScript爬取分页数据,通常需要以下步骤:
1. 发送HTTP请求获取页面内容。
2. 解析页面内容,提取需要的数据。
3. 如果有多页数据,获取下一页的URL,并重复步骤1和2,直到获取所有数据。
下面是一个简单的示例代码,用于爬取一个有多页数据的网站:
```javascript
const axios = require('axios');
const cheerio = require('cheerio');
const baseUrl = 'https://example.com/page/';
async function getPageData(page) {
const url = baseUrl + page;
const response = await axios.get(url);
const $ = cheerio.load(response.data);
// 解析页面内容,提取需要的数据
const data = [];
$('table tr').each(function() {
const row = [];
$(this).find('td').each(function() {
row.push($(this).text());
});
data.push(row);
});
// 获取下一页的URL,并重复步骤1和2
const nextPage = $('a.next-page').attr('href');
if (nextPage) {
const nextData = await getPageData(nextPage);
return data.concat(nextData);
} else {
return data;
}
}
getPageData(1).then(console.log);
```
这个示例代码使用了axios库发送HTTP请求,cheerio库解析HTML文档。在解析页面内容时,它使用了jQuery-style的选择器语法来选取需要的数据。在获取下一页的URL时,它假设有一个CSS类名为“next-page”的链接,指向下一页的页面。