【发布时间】: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