【问题标题】:python process JS and loop though to get datapython处理JSON并循环获取数据
【发布时间】:2021-07-24 14:53:37
【问题描述】:

我抓取了一些复杂的 JS 数据,我需要一些帮助来循环和提取数据,这里是 JS:

score = 0;
    var AnswerRight = false;

        AnswerRight = false;

                if ($('input[name=Guess1]').val() != "") {
                    if (trim($('input[name=Guess1]').val().toUpperCase()) == '7Y-6') {

                        AnswerRight = true;
                    }
                }

                if ($('input[name=Guess1]').val() != "") {
                    if (trim($('input[name=Guess1]').val().toUpperCase()) == '-6+7Y') {

                        AnswerRight = true;
                    }
                }

                if ($('input[name=Guess1]').val() != "") {
                    if (trim($('input[name=Guess1]').val().toUpperCase()) == 'F(Y)=7Y-6') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess1]').val() != "") {
            if (AnswerRight) {
                $('#tick1').show();
                $('#cross1').hide();
                score = score + 1;
            } else {
                $('#tick1').hide();
                $('#cross1').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess2]').val() != "") {
                    if (trim($('input[name=Guess2]').val().toUpperCase()) == '-27') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess2]').val() != "") {
            if (AnswerRight) {
                $('#tick2').show();
                $('#cross2').hide();
                score = score + 1;
            } else {
                $('#tick2').hide();
                $('#cross2').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess3]').val() != "") {
                    if (trim($('input[name=Guess3]').val().toUpperCase()) == '-18') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess3]').val() != "") {
            if (AnswerRight) {
                $('#tick3').show();
                $('#cross3').hide();
                score = score + 1;
            } else {
                $('#tick3').hide();
                $('#cross3').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess4]').val() != "") {
                    if (trim($('input[name=Guess4]').val().toUpperCase()) == '4') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess4]').val() != "") {
            if (AnswerRight) {
                $('#tick4').show();
                $('#cross4').hide();
                score = score + 1;
            } else {
                $('#tick4').hide();
                $('#cross4').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess5]').val() != "") {
                    if (trim($('input[name=Guess5]').val().toUpperCase()) == '7') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess5]').val() != "") {
            if (AnswerRight) {
                $('#tick5').show();
                $('#cross5').hide();
                score = score + 1;
            } else {
                $('#tick5').hide();
                $('#cross5').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess6]').val() != "") {
                    if (trim($('input[name=Guess6]').val().toUpperCase()) == '4') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess6]').val() != "") {
            if (AnswerRight) {
                $('#tick6').show();
                $('#cross6').hide();
                score = score + 1;
            } else {
                $('#tick6').hide();
                $('#cross6').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess7]').val() != "") {
                    if (trim($('input[name=Guess7]').val().toUpperCase()) == '9X+96') {

                        AnswerRight = true;
                    }
                }

                if ($('input[name=Guess7]').val() != "") {
                    if (trim($('input[name=Guess7]').val().toUpperCase()) == '3(3X+32)') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess7]').val() != "") {
            if (AnswerRight) {
                $('#tick7').show();
                $('#cross7').hide();
                score = score + 1;
            } else {
                $('#tick7').hide();
                $('#cross7').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess8]').val() != "") {
                    if (trim($('input[name=Guess8]').val().toUpperCase()) == '9X²-2X+7') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess8]').val() != "") {
            if (AnswerRight) {
                $('#tick8').show();
                $('#cross8').hide();
                score = score + 1;
            } else {
                $('#tick8').hide();
                $('#cross8').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess9]').val() != "") {
                    if (trim($('input[name=Guess9]').val().toUpperCase()) == '4(X²-10X+32)') {

                        AnswerRight = true;
                    }
                }

                if ($('input[name=Guess9]').val() != "") {
                    if (trim($('input[name=Guess9]').val().toUpperCase()) == '4X²-40X+128') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess9]').val() != "") {
            if (AnswerRight) {
                $('#tick9').show();
                $('#cross9').hide();
                score = score + 1;
            } else {
                $('#tick9').hide();
                $('#cross9').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess10]').val() != "") {
                    if (trim($('input[name=Guess10]').val().toUpperCase()) == '12') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess10]').val() != "") {
            if (AnswerRight) {
                $('#tick10').show();
                $('#cross10').hide();
                score = score + 1;
            } else {
                $('#tick10').hide();
                $('#cross10').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess11]').val() != "") {
                    if (trim($('input[name=Guess11]').val().toUpperCase()) == '6') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess11]').val() != "") {
            if (AnswerRight) {
                $('#tick11').show();
                $('#cross11').hide();
                score = score + 1;
            } else {
                $('#tick11').hide();
                $('#cross11').show();
            }
        }

        AnswerRight = false;

                if ($('input[name=Guess12]').val() != "") {
                    if (trim($('input[name=Guess12]').val().toUpperCase()) == '-1') {

                        AnswerRight = true;
                    }
                }

        if ($('input[name=Guess12]').val() != "") {
            if (AnswerRight) {
                $('#tick12').show();
                $('#cross12').hide();
                score = score + 1;
            } else {
                $('#tick12').hide();
                $('#cross12').show();
            }
        }

    if (score < 12) {
        $('#Checkbutton').text('Check again').css({'width':'150px'});
    }
    if (score >= 9) {
        $('#Trophybutton').show();
        $('#Trophybutton').val('Claim Your Trophy for ' + score + ' out of 12');
    }
    if(score==12) {
        $('#Checkbutton').hide();
    }
}

我将举一个我想要抓取的示例,以便您可以看到每个问题都被命名为 Guess,然后是一个数字,以便您查看:

if ($('input[name=Guess1]').val() != "") {
                    if (trim($('input[name=Guess1]').val().toUpperCase()) == '7Y-6') {

                        AnswerRight = true;
                    }
                }

所以我想得到 7y-6 的值

但您可以在上面的代码中看到猜测 1 的更多答案,例如 -6+7y,因为它可以通过多种方式解决,但是我只想要每个问题的第一个答案:

所以我想得到的下一个问题答案是:

if ($('input[name=Guess2]').val() != "") {
                    if (trim($('input[name=Guess2]').val().toUpperCase()) == '-27') {

                        AnswerRight = true;
                    }
                }

我想提取的值是-27

我想对那里的每个问题都这样做,所以循环遍历每个问题,然后显示然后打印出答案。

我该怎么做?我正在考虑正则表达式 - 我已经尝试过,但它对我不起作用哈哈哈。

【问题讨论】:

    标签: python regex loops web-scraping beautifulsoup


    【解决方案1】:

    你可以使用这个回复:Guess\d{1,3}.*?==\s\'(.*?)\'

    代码

    import re
    
    answers = re.findall(r"Guess\d{1,3}.*?==\s\'(.*?)\'", js_text_code)
    

    假设您有js_text_code 中的代码。

    answers 将是这样的列表:

    ['7Y-6', '-6+7Y', ... ]
    

    模式说明

    • Guess: 文字“猜”
    • \d{1,3}:一位、两位或三位(假设这个数字至少是三位)
    • .*?==:直到第一个 == 之前的任何字符
    • \s:一个空格。
    • \':开放式简单引用。
    • (.*?)\':直到第一个封闭的简单引号之前的任何字符。并制作一组这样的角色(这是你想要的部分)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多