verification with Playwright (Python)

Viewed 22

I have a work to deliver and I need some help: The script below opens google and does a search with the term that the user types, then goes to the "People also ask" part and prints the questions and answers that you have there. My problem is that these questions and answers also have answers in lists. I tried to do a check for, if you have the answer span > print, if you have the list class > don't do anything, but the if is not working as I wanted, the intention is to skip the questions that the answer is a list, but it is printing the question from the div which has a list and since it is not for printing a list it pulls the answer from the later question so the answer is wrong

(I'm using Playwright to do the scraping)

for i in range(len(lista_termos)):
    pagina.goto('https://www.google.com/')
    pagina.locator('.gLFyf').fill(f'{lista_termos[i]}')  # Preenche o campo de pesquisa com o que vc quiser
    pagina.locator('.lnXdpd').click()
    pagina.locator('.FPdoLc > center:nth-child(1) > input:nth-child(1)').click()

    pergunta = pagina.locator('.r21Kzd')  # Localiza a pergunta
    titulo = pagina.locator('.iDjcJe.IX9Lgd.wwB5gf > span')  # Localiza o título
    resposta = pagina.locator('.hgKElc')  # Localiza as respostas

    while c < 4:
        pergunta.nth(c).click()
        if pergunta.filter(has=pagina.locator(".hgKElc")):
            print(f'\033[1;97;43m Termo buscado: {lista_termos[i]} \033[m')
            sleep(1)
            print(f'Pergunta: {titulo.nth(c).text_content()}')
            sleep(1)
            print(f'Resposta: {resposta.nth(c).text_content()}')
            sleep(1)

        if pergunta.filter(has=pagina.locator(".TrT0Xe")):
            print('É uma lista')

        c += 1
0 Answers
Related