【问题标题】:Looping through a page until certain movie found循环浏览页面直到找到某部电影
【发布时间】:2019-12-21 18:45:16
【问题描述】:

我创建了一个while循环,它会一直抓取某个电影的页面,直到找到它,顺便说一下,这是一个外部模块/帮助程序,但由于某种原因它不起作用,这个函数后来被调用为了从 result 中获取数据结果,然后在结果数组中抓取项目,结果还将具有电影名称和价格:

const movies = "AVENGERS END GAME;

module.exports = {

    spider: function () {

        var target = 2
        const extractmovies = document.querySelectorAll('some.selector1')

        let result = [];
        let targetss = [];
        var movie_Name, movie_Price

        var i = 0;
        while (result <= 2) {
            for ( extract of extractmovies) {

                const nameChk = extract.querySelector('selector')
                    item_Name = nameChk.innerText

                const priceChk = extract.querySelector('g.iu')
                    item_Price = parseInt(priceChk.getAttribute('ggfg')) 

                result.push({
                    Name: item_Name,
                    Price: item_Price,
                })
            }

            for (myTarget of result) {
                if (myTarget.Name == keywords) {
                    targetss.push(1)
                }
            }
            i++;
        }
        return targetss;
    }
}

【问题讨论】:

  • 我猜while (result &lt;= 2) 部分你想要while (result.length &lt;= 2)。这是一个具有length 属性的数组。
  • 哦,我没看到,我会试一试,然后回复你

标签: javascript loops web-scraping while-loop


【解决方案1】:

问题出在您的while 循环中。从技术上讲,变量result 是一个具有length 属性的数组。所以我会将行从while (result &lt;= 2) 更改为while (result.length &lt;= 2)

在此处进一步阅读:Array.length

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2011-05-08
    相关资源
    最近更新 更多