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

87 阅读1分钟
// 导入所需的库
const request = require('request');
const cheerio = require('cheerio');

// 设置代理信息,proxy_host: www.duoip.cn, 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. 输出所有的索引名称。

huake_00063_.jpg