运行脚本后,结果总是 "undefined"
Upon runnning a script, I always get "undefined" as a result
我一直在尝试使用 axios
和 cheerio
从网页中迭代地抓取下一页 link。当我 运行 脚本时,我总是得到 undefined
作为结果。我怎样才能得到下一页 link 直到没有更多?
这是我目前的尝试:
const axios = require("axios");
const cheerio = require("cheerio");
const base = "https://whosebug.com";
const url = "https://whosebug.com/questions/tagged/web-scraping";
async function main(){
const data = await axios.get(url,{
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}
});
const $ = cheerio.load(data);
let nextPage = $('a[rel="next"]').eq(0).attr("href");
console.log(nextPage);
while(nextPage){
url = base + nextPage;
const data = await axios.get(url,{
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}
});
const $ = cheerio.load(data);
nextPage = $('a[rel="next"]').eq(0).attr("href");
console.log(nextPage);
}
}
main();
来自 axios.get
的已解决承诺是一个响应对象,其中包含 <axiosResponse>.data
下的实际 html-响应主体。因此,如果将其更改为以下内容,它应该可以工作:
...
const response = await axios.get(url,{
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}
});
const $ = cheerio.load(response.data);
...
我一直在尝试使用 axios
和 cheerio
从网页中迭代地抓取下一页 link。当我 运行 脚本时,我总是得到 undefined
作为结果。我怎样才能得到下一页 link 直到没有更多?
这是我目前的尝试:
const axios = require("axios");
const cheerio = require("cheerio");
const base = "https://whosebug.com";
const url = "https://whosebug.com/questions/tagged/web-scraping";
async function main(){
const data = await axios.get(url,{
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}
});
const $ = cheerio.load(data);
let nextPage = $('a[rel="next"]').eq(0).attr("href");
console.log(nextPage);
while(nextPage){
url = base + nextPage;
const data = await axios.get(url,{
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}
});
const $ = cheerio.load(data);
nextPage = $('a[rel="next"]').eq(0).attr("href");
console.log(nextPage);
}
}
main();
来自 axios.get
的已解决承诺是一个响应对象,其中包含 <axiosResponse>.data
下的实际 html-响应主体。因此,如果将其更改为以下内容,它应该可以工作:
...
const response = await axios.get(url,{
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}
});
const $ = cheerio.load(response.data);
...