NodeJS GET请求在本地计算机上运行,​​但不在远程VM内运行

时间:2020-09-29 03:46:35

标签: node.js facebook-graph-api web-scraping

我正在尝试创建一个简单的工具,该工具将访问特定的Facebook页面并提取一些数据。我为此使用NodeJS。我对GET请求使用内置的https模块。我的本地计算机上的东西运行正常,但是在VM内无法获取响应,每次都失败。

我也尝试过axios,但是它也不起作用。

以前,我认为这可能是防火墙问题,但我启用了防火墙内部的所有功能,但也无法正常工作。我正面临着一个非常奇怪的问题。

这可能是什么原因?

Facebook是否阻止了我的请求?无权利?我也在发送User-Agent标头,但是它不起作用。

我的代码如下:

const options = {
  hostname: 'www.facebook.com',
  // port: 443,
  path: `/${username}`,
  method: 'GET',
  headers: {
    'user-agent':
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
    'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6',
  },
};
const request = https.request(options, (response) => {
  let body = '';
  console.log('response : \n' + response);
  response.setEncoding('utf8');

  response.on('data', (chunk) => {
    body += chunk;
    console.log('response on data chunk : \n' + chunk);
  });
  response.on('end', () => {
    console.log('body : \n' + body);
    const arrMatches = body.match(rePattern);
    if (arrMatches && arrMatches.length > 1) {
      resolve(arrMatches[1]);
    } else {
      reject(new Error('Facebook user not found'));
    }
  });
});

request.on('error', (err) => {
  console.log('request error : \n' + err);
  reject(err);
});

request.end();

0 个答案:

没有答案