以下是在 JavaScript 中的 node-fetch 库中使用 User-Agent 的示例:

const fetch = require('node-fetch');

const url = 'https://www.example.com';

fetch(url, {
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
  }
})
  .then(response => response.text())
  .then(data => console.log(data))
  .catch(error => console.error(error));

在这个例子中,我们使用 node-fetchhttps://www.example.com URL 发起 HTTP GET 请求。为了指定要使用的 User-Agent,我们向作为 fetch() 函数第二个参数提供的选项对象中添加 User-Agent 头。我们使用的 User-Agent 是适用于 Windows 10 操作系统的最新版 Mozilla Firefox。之后,我们通过将响应文本输出到控制台来处理收到的响应,或者在请求失败时显示错误消息。

Node.js Node-fetch 库中 User-Agent 的使用示例

原文地址: https://www.cveoy.top/t/topic/oRoM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录