NodeJS +请求-请求网站时访问被拒绝 [英] NodeJS + Request - Access denied when requesting website

查看:153
本文介绍了NodeJS +请求-请求网站时访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用request请求网站的html,但我一直收到拒绝访问的错误.我该如何克服?这是下面函数的代码:

I'm trying to request the html of a website using request but I keep getting an access denied error. How do I get past this? Here is the code for the function below:

const request = require('request');
function firstShoe() {
        request('https://www.jdsports.co.uk/product/green-nike-vapormax/281735/', function (error, response, body) {
            console.log('body:', body); 
        });
}

错误:

</BODY>
</HTML>

body: <HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>

You don't have permission to access "http&#58;&#47;&#47;www&#46;jdsports&#46;co&#46;uk&#47;product&#47;green&#45;nike&#45;vapormax&#47;281735&#47;" on this server.<P>
Reference&#32;&#35;18&#46;609d3e17&#46;1500116386&#46;15f0cb85
</BODY>
</HTML>

通过将用户代理传递到标头中找到解决方案.

Found a solution by passing the user-agent into the headers.

function firstShoe() {
        var options = {
            headers: {'user-agent': 'node.js'}
        }
        request('https://www.jdsports.co.uk/product/green-nike-vapormax/281735/', options, function (error, response, body) {
            console.log(body);
            message.channel.send(body);
        });
    }

推荐答案

您将获得 403 Forbidden ,因为该网站禁止使用非通用用户代理发送的所有请求(基本上,他们检查User-Agent 标头).这是避免报废的非常简单的保护措施.

You are getting a 403 Forbidden because that website is blocking all requests sent using non common user agents (basically they check User-Agent header). It is a very simple protection to avoid scrappers.

例如,如果您使用其标准的User-Agent发送以下cURL,则会完美收到响应:

For example, if you send the following cURL using its standard User-Agent, the response is received perfectly:

curl -v 'https://www.jdsports.co.uk/product/green-nike-vapormax/281735/'

但是,如果您重复该请求并指定了不存在的User-Agent,则该请求将被阻止:

Nevertheless, if you repeat that request specifying a non existing User-Agent, the request is blocked:

curl -v 'https://www.jdsports.co.uk/product/green-nike-vapormax/281735/' -H 'User-Agent: StackOverflow'

这篇关于NodeJS +请求-请求网站时访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆