美文网首页
网络资源请求工具

网络资源请求工具

作者: 段煜华 | 来源:发表于2020-11-18 14:26 被阅读0次

    request

    截至2020年2月11日,请求完全被弃用。预计不会出现新的变化。事实上,已经有一段时间没有飞机着陆了。
    https://github.com/request/request#readme

    const request = require('request');
    request('http://www.google.com', function (error, response, body) {
      console.error('error:', error); // Print the error if one occurred
      console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
      console.log('body:', body); // Print the HTML for the Google homepage.
    });
    

    superagent

    小型渐进客户端HTTP请求库,和Node.js模块具有相同的API,支持许多高级HTTP客户端特性
    https://github.com/visionmedia/superagent

    const superagent = require('superagent');
    
    // callback
    superagent
      .post('/api/pet')
      .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
      .set('X-API-Key', 'foobar')
      .set('accept', 'json')
      .end((err, res) => {
        // Calling the end function will send the request
      });
    
    // promise with then/catch
    superagent.post('/api/pet').then(console.log).catch(console.error);
    
    // promise with async/await
    (async () => {
      try {
        const res = await superagent.post('/api/pet');
        console.log(res);
      } catch (err) {
        console.error(err);
      }
    })();
    

    cheerio

    为服务器特别定制的,快速、灵活、实施的jQuery核心实现.
    https://github.com/cheeriojs/cheerio/wiki/Chinese-README

    const cheerio = require('cheerio');
    const $ = cheerio.load('<h2 class="title">Hello world</h2>');
    
    $('h2.title').text('Hello there!');
    $('h2').addClass('welcome');
    
    $.html();
    //=> <html><head></head><body><h2 class="title welcome">Hello there!</h2></body></html>
    

    puppeteer

    以 Chromium 为基础开发的 Node 端无头浏览器
    https://github.com/puppeteer/puppeteer

    const puppeteer = require('puppeteer');
    
    (async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
      await page.goto('https://example.com');
      await page.screenshot({path: 'example.png'});
    
      await browser.close();
    })();
    

    whistle

    https://github.com/avwo/whistle
    基于 Node 实现的跨平台抓包调试代理工具,有以下基本功能:

    查看 HTTP、HTTPS、HTTP2、WebSocket、TCP 请求响应数据
    修改 HTTP、HTTPS、HTTP2、WebSocket、TCP 请求响应数据
    修改请求 url、方法、头部、内容等
    修改响应状态码、头部、内容,并支持本地替换等
    修改 WebSocket 和 TCP 收发的帧数据
    设置 hosts(支持 IPv6)、http-proxy、https-proxy、socks
    作为HTTP代理或反向代理
    集成常用的 web 调试工具,如 weinre 和 log 等
    支持用 Node 编写插件扩展

    相关文章

      网友评论

          本文标题:网络资源请求工具

          本文链接:https://www.haomeiwen.com/subject/qufciktx.html