您好,我正在使用 axios 和 Cheerio 来抓取一些数据。我想抓取多个页面, url 结构类似于 example.com/?page=1。我如何用计数器抓取每个页面?
axios({
method: "get",
url:
"https://example.com/?page=",
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}
}).then(res => {
最佳答案
我相信有多种方法可以实现该解决方案,但基本上您需要执行所有 axios
并以编程方式使用 Cheerio 解析所有它们。
如果您知道要抓取多少页
您可以创建一个简单的 for
循环,并将所有 axios
函数与生成的 url 一起推送到一个数组中。然后你可以使用 Promise.all
const promises = [];
for(let page = 0; page <= 5; page ++){
promises.push(
axios({method: "get",url:`https://example.com?page=${page}`})
.then(res => {
// Parse your result with Cheerio or whatever you like
})
);
}
// You can pass the responses on this resolve if you want.
Promise.all(promises).then(...)
如果您正在抓取列表页面并且总页数未知
然后,您可以创建一个异步/递归函数,用于使用 axios
分派(dispatch)请求并有条件地迭代。通过这种方式,与下面的解决方案相比,您还可以减少内存的最大使用量。而且它会更慢,因为请求不会并行。
// The function below is kind-of pseudo code so don't try to copy/paste it :)
const dispatchRequest = (page) => {
const response = axios({url: `https://example.com?page=${page}`});
// Ex: You can parse the response here with Cheerio and check if pagination is not disable
if(something){
return dispatchRequest(page+1);
}
else{
return response;
}
}
上述解决方案当然也有缺点。如果您被目标网站阻止或您的请求因某种原因失败,您将没有机会重试相同的请求或轮换代理以绕过目标网站的安全性。
我建议您实现一个队列
并将所有请求分派(dispatch)函数放在那里。通过这种方式,您可以检测失败/问题并再次将失败的请求排队。您还可以通过队列支持来实现上述两种解决方案。您可以并行运行它并更好地管理内存/CPU 消耗。
您也可以使用 SDK。我看到有几个抓取 SDK 为您提供了整个工具集,因此您不必重新发明轮子。
关于node.js - 我如何使用axios和cheerio实现多页面抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60006377/