无法解析网页中的信息

时间:2019-10-07 18:56:00

标签: javascript node.js

我正在尝试从node.js项目上的此链接解析信息 https://stockx.com/api/products/nike-daybreak-undercover-black?includes=market

当我通过邮递员访问链接并在Web浏览器上访问URL时,我可以获得信息。但是,当我尝试通过node.js项目访问请求时,它表示访问被拒绝。知道为什么吗? 谢谢。 这是我的代码:

const express = require('express');
const request = require('request');
const cheerio = require('cheerio');
const axios = require('axios')
const app     = express();

app.get('/', function(req, res){
   let shoe =req.query.shoe;
    let url = 'https://stockx.com/api/products/nike-daybreak-undercover-black?includes=market'
    request(url, function(error, response, html) {

        if (!error) {
          var $ = cheerio.load(html);
          console.log(html)

          res.send();
        }

    });

});
app.listen('8080');
console.log('API is running on http://localhost:8080');
module.exports = app;

2 个答案:

答案 0 :(得分:0)

我尝试了以下代码,它可以正常工作


// ...
app.get('/', function(req, res){
  //  let shoe =req.query.shoe;
    let url = 'https://stockx.com/api/products/nike-daybreak-undercover-black?includes=market'

    axios({
      method : 'get',
      url,
      headers : { withCredentials: true, 'User-Agent' : 'Postman' }
    })
    .then(data => {
      console.log('data', data.data);
    })
    .catch(err => {
      console.log('err', err);
    })
    res.send().status(200);
});

答案 1 :(得分:0)

您只需要在标题中添加“ User-Agent”。您尝试从中获取数据的网站拒绝了所有没有User-Agent的请求,以避免报废。

const options = {
    url: 'https://stockx.com/api/products/nike-daybreak-undercover-black?includes=market',
    headers: {
        'User-Agent': 'request'
    }
};
request(options, function(error, response, html) {
    console.log('err: ', error);

    if (!error) {
        var $ = cheerio.load(html);
        console.log(html)

        res.send(html);
    }
});