0

I have some intricate JS data I scrape and I need some help looping through and extracting data here is the 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();
    }
}

I will give an example of what I want to scrape so you can see each question is named Guess then a number so you can see:

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

                        AnswerRight = true;
                    }
                }

so I would like to get the value 7y-6

but you can see in the code above there's more answers for guess 1 like -6+7y since it can be solved in multiple ways however I just want the first answer to every question:

so the next question answer I would like to get is within:

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

                        AnswerRight = true;
                    }
                }

and the value I would like to extract is -27

I would like to do this for every question there so loop through for each one and then display then just print out the answers.

How would I do this? Im thinking regex - I have tried but it doesn't work for me hahaha.

1 Answer 1

0

You could use this re: Guess\d{1,3}.*?==\s\'(.*?)\'

Code

import re

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

Assuming you have the code in js_text_code.

answers will be a list like this:

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

Pattern explanation

  • Guess: Literal text 'Guess'
  • \d{1,3}: One, two, or three digits (assuming this number could be at least of three digits)
  • .*?==: Any characters until the first ==
  • \s: A space.
  • \': The open simple quote.
  • (.*?)\': Any characters until the first closed simple quote. And making a group of this characters (this is the part you want).
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.