ITPub博客

首页 > 大数据 > 数据分析 > 使用request库的get方法发起GET请求

使用request库的get方法发起GET请求

原创 数据分析 作者:金木大大大 时间:2023-11-08 10:04:56 0 删除 编辑

```javascript

// 导入所需的库

const request = require('request');

const cheerio = require('cheerio');


// 设置代理信息,proxy_host: proxy_port: 8000

const proxy = {

  host: 'jshk.com.cn',

  port: 1234

};


// 定义要爬取的URL

const url = 'http://localhost:9200/_cat/indices';


// 使用request库的get方法发起GET请求,并设置proxy参数

request.get(url, { proxy: proxy }, (error, response, body) => {

  if (!error && response.statusCode === 200) {

    // 使用cheerio库解析返回的HTML

    const $ = cheerio.load(body);

    

    // 查找所有的索引名称

    const indices = $('td').slice(1, -1).toArray().map(td => $(td).text());


    // 输出所有的索引名称

    console.log(indices);

  }

});

```

步骤:


1. 引入所需的库:request和cheerio。


2. 设置代理信息。


3. 定义要爬取的URL。


4. 使用request库的get方法发起GET请求,并设置proxy参数。


5. 使用cheerio库解析返回的HTML。


6. 查找所有的索引名称。


7. 输出所有的索引名称。


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70032566/viewspace-2993405/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2023-10-12

  • 博文量
    249
  • 访问量
    157736