web | 在node中使用axios进行同步和异步请求
web | 在node中使用axios进行同步和异步请求
最近在看怎么用nodejs整爬虫,摸索一下axios的使用。
const axios = require('axios');
// 异步写法
axios("https://mz1.top")
.then(res=>{
console.log("https://mz1.top");
console.log(res.headers);
})
.catch(err=>{
console.log(err)
});
axios("http://blog.mz1.top")
.then(res=>{
console.log("http://blog.mz1.top");
console.log(res.headers);
})
.catch(err=>{
console.log(err)
});
console.log("\nok!\n")
// 同步写法
const req = async(url) =>{
try{
console.log(url)
return await axios.get(url);
}catch(err){
console.error(err);
}
};
const run = async() => {
var a = await req("http://mz1.top")
console.log(a.headers)
var b = await req("http://blog.mz1.top")
console.log(b.headers)
}
run();
本文来自博客园,作者:Mz1,转载请注明原文链接:https://www.cnblogs.com/Mz1-rc/p/17584625.html
如果有问题可以在下方评论或者email:mzi_mzi@163.com

浙公网安备 33010602011771号