javascript - NodeJS |等待使用请求模块重定向页面

标签 javascript node.js

访问http://www.oneblockdown.it/en/pharrell-williams-x-adidas-originals/footwear-lifestyle/men-unisex/pharrell-williams-x-adidas-originals-human-race-nmd-bb3070/4267时通过普通浏览器,它通常会在白色页面上停留 2.5 秒,运行一些 JavaScript,然后自动刷新页面,然后加载实际的产品页面。

当我使用 Node JS 请求模块(使用普通的 NodeJS http/https 模块)请求页面时,它返回空白页面的 html,而不是等待并显示产品页面。空白的 html 如下所示:https://pastebin.com/ZSeXuZRc

我该如何让它等待,像浏览器一样执行代码并允许它刷新并显示产品页面?

这是我当前的代码:

    request(
    {
        url: 'https://www.oneblockdown.it/en/pharrell-williams-x-adidas-originals/footwear-lifestyle/men-unisex/pharrell-williams-x-adidas-originals-human-race-nmd-bb3070/4267',
        method: 'GET',
        headers: {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
            'Host': 'www.oneblockdown.it',
            'Cache-Control': 'max-age=0',
            'Upgrade-Insecure-Requests': '1',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Referer': 'http://www.oneblockdown.it/en/footwear',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'en-US,en;q=0.9,la;q=0.8'

        }
    },
    function(err, res, body) {
        if(err || res.statusCode !== 200){
            console.log(`FAILED | ${err}`)
        } else {
          //parse the body here. It should return product page, but is returning the white html with javascript
        }
    });

最佳答案

不幸的是,你不能这样做。响应包括您向该端点发送请求时服务器返回的所有内容。如果在 DOM 加载时调用 javascript,即 window.onloaddocument.ready(本网页就是这种情况),您将不会获得这些更改。

即使对于网络爬虫来说,这也会是一个问题(这就是为什么有搜索引擎优化标准,涉及在不使用任何脚本的情况下提供所有重要信息,并在页面加载后留下 UI 相关任务,例如样式和交互。

关于javascript - NodeJS |等待使用请求模块重定向页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47891919/

相关文章:

javascript - 配置 ESLint 以将 .ts 和 .tsx 解析为 Typescript,将 .js 和 .jsx 解析为 Ecmascript

javascript - HOW修复计算两位小数错误的总和

javascript - Angular 添加随机 JPG 图像?

javascript - StrongLoop : How to define models in models/directory at loopback. 启动()时间?

javascript - 传单Markercluster - "this._featureGroup.addEventParent is not a function"

javascript - 使用 bower 将 3rd 方模块安装到 yeoman 生成的 Angular 应用程序中不起作用 - 没有呈现任何内容,也没有错误

php - 调整 <img> 的大小导致速度变慢

javascript - 构建动态 $match : for MongoDB aggregates

node.js - Node JS 14 : Using a SSL CA Bundle

html - 在HTML页面中通过iframe加载HTTPS URL