最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

无法从网页中解析信息

运维笔记admin9浏览0评论

无法从网页中解析信息

无法从网页中解析信息

我正在尝试从我的node.js项目上的此链接解析信息

当我通过邮递员访问链接并在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 = ''
    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;
回答如下:

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


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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论