403使用Cheerio时禁止

卡梅伦·希勒

我正在尝试对网站进行Web爬网,以便为项目收集一些信息,这是我的代码,它正在控制台403中返回。我正在使用request和cheerio来执行此操作,为什么会这样?注意我确实知道大多数状态码的含义。

const request = require('request');
const cheerio = require('cheerio');


request('http://www.realmeye.com/forum/', function(err, resp, html) {
    if (!err) {
        const gatherInformation = cheerio.load(html);
        console.log(html);
    }
})

叶夫根尼

您应该在请求中添加“ User-Agent”标头,以适合某些浏览器(例如chrome)。服务器可能会对其进行检查以避免陌生的客户端。

网页抓取的经验法则:在尝试在框架上重现请求之前,请使用chrome开发工具/提琴手/其他类似工具检查从客户端启动的请求(chrome,firefox等)(检查标头,cookie等) )。

我在您遇到的情况下在Fiddler上看到的原始请求(在chrome上点击您的网址时):

GET /forum/ HTTP/1.1
Host: www.realmeye.com
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
Sec-Fetch-Mode: same-origin
Sec-Fetch-Site: same-origin
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,he;q=0.8

大多数服务器会在返回200 OK响应之前检查“ Accept”和“ User-Agent”标头。

固定代码段:

const request = require('request');
const cheerio = require('cheerio');

let options = {
    url: 'https://www.realmeye.com/forum/',
    headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
    }
};

request(options, function(err, resp, html) {
    if (!err) {
        const gatherInformation = cheerio.load(html);
        console.log(html);
    }
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试使用CORS时获取403(禁止)

HTTP错误403:使用NLTK时禁止

使用Azure发布时禁止使用HTTP 403

使用Google Photos API时被禁止使用403

使用Spring Boot时出现403禁止错误-安全

上传文件时禁止使用HTTP状态403

使用Twitter Fabric获取user_timeline时禁止403

使用Netcat时HTTP GET请求禁止403错误

403使用Alexa位置API时来自Alexa的禁止响应

在GitHub上使用组织api时禁止403

禁止使用Apache VirtualHost 403

Azure存储-禁止使用403

上传网站时禁止Laravel 403

推送到Bitbucket时禁止403

加载图像时禁止jQuery 403

发布到 url 时禁止 403

nginx 403 渲染大文件时禁止

禁止:用户,使用“ curl”,“ curl”:使用curl访问kube-apiserver时出现403错误

错误:无法下载视频数据:HTTP错误403:使用youtube_dl时被禁止

使用 Node + Express 将文件上传到 DO 空间时禁止 403

使用nginx部署laravel项目时,为什么会禁止403?

使用sendgrid发送电子邮件时在Node.js中禁止403

403 尝试使用 Azure 通知中心批量发送时被禁止

使用签名的URL上传到S3时获得403(禁止)

在.net Core中使用身份验证方案时返回403(禁止)

当使用Keyclaok进行POST请求时,Spring Boot返回403被禁止

<403>在https:// URL上禁止,并且<302>在使用http:// URL时临时移动

通过SAS访问Azure Blob存储时禁止使用零散403

<urlopen错误隧道连接失败:403禁止>在pythonanywhere服务器上使用geopy时出错