最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

使用Axios的HTTP请求

IT培训 admin 10浏览 0评论

使用Axios的HTTP请求

我正在尝试使用NodeJS爬行网站。我正在使用Axios发出HTTP请求。我只能提取加载网页时可用的那些项目。我向下滚动时加载的所有HTML都不会被获取。

这是我的代码。

const axios = require('axios');
const cheerio = require('cheerio');
var request = require('request');

// table view
const url = ";_sortDirection=asc&study=Engineering&_mode=table";

fetchData(url).then((res) => {
    const html = res.data;
    const $ = cheerio.load(html);

    const unilist = $('.TableTabular__TableContainer-febmbj-0.guaRKP > tbody > tr >td ');

    unilist.each(function() {

        let title = $(this).find('div').attr("name");

        if (typeof(title) == 'string') {
            console.log(title);
            } 

    });
})

async function fetchData(url){
    console.log("Crawling data...")

    // make http call to url
    let response = await axios(url).catch((err) => console.log(err));

    if(response.status !== 200){
        console.log("Error occurred while fetching data");
        return;
    }
    return response;

}

我正在尝试获取所有大学名称。但是,我只能获得13所大学,因为只有在手动向下滚动页面时才会加载其他大学。

我如何访问网页中的所有大学:;_sortDirection=asc&study=Engineering&_mode=table

回答如下:

使用Axios的HTTP请求

我正在尝试使用NodeJS爬行网站。我正在使用Axios发出HTTP请求。我只能提取加载网页时可用的那些项目。我向下滚动时加载的所有HTML都不会被获取。

这是我的代码。

const axios = require('axios');
const cheerio = require('cheerio');
var request = require('request');

// table view
const url = ";_sortDirection=asc&study=Engineering&_mode=table";

fetchData(url).then((res) => {
    const html = res.data;
    const $ = cheerio.load(html);

    const unilist = $('.TableTabular__TableContainer-febmbj-0.guaRKP > tbody > tr >td ');

    unilist.each(function() {

        let title = $(this).find('div').attr("name");

        if (typeof(title) == 'string') {
            console.log(title);
            } 

    });
})

async function fetchData(url){
    console.log("Crawling data...")

    // make http call to url
    let response = await axios(url).catch((err) => console.log(err));

    if(response.status !== 200){
        console.log("Error occurred while fetching data");
        return;
    }
    return response;

}

我正在尝试获取所有大学名称。但是,我只能获得13所大学,因为只有在手动向下滚动页面时才会加载其他大学。

我如何访问网页中的所有大学:;_sortDirection=asc&study=Engineering&_mode=table

回答如下:

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论