help with indentation that is generating error in python 3.6

Asked

Viewed 166 times

1

I’m doing a webcrawler I’m having the following problem. I had to do a separate program to print out how many candidates passed each course. Only that the last line is not running, and it would make the array of names pass to the next course of the list. The line that is not running is the last one that is written : position = position +1
The code is this

from bs4 import BeautifulSoup
import requests  
import string
import re
import urllib 
cursos = [
'ADMINISTRAÇÃO - GOVERNADOR VALADARES - DIURNO - SISU - GRUPO A',
'ADMINISTRAÇÃO - GOVERNADOR VALADARES - DIURNO - SISU - GRUPO B',  
'ADMINISTRAÇÃO - GOVERNADOR VALADARES - DIURNO - SISU - GRUPO D',
'ADMINISTRAÇÃO - GOVERNADOR VALADARES - DIURNO - SISU - GRUPO E',
'ADMINISTRAÇÃO - JUIZ DE FORA - DIURNO - SISU - GRUPO A',
'ADMINISTRAÇÃO - JUIZ DE FORA - DIURNO - SISU - GRUPO B'

]

r = requests.get('http://www.ufjf.br/cdara/sisu-2/sisu-2017-1a-edicao/lista-de-espera-sisu-3/?id_curso=05GV&id_grupo=72')
soup = BeautifulSoup(r.text, "html.parser")
vetor = [] 
posicao =1
for node in soup.findAll("td"):
  candidato =node.get_text("td")
  vetor.append(candidato)
  contador = 0

  for s in vetor:
   contador = contador +1
  contador = int(contador/5)
  contador = 5
  contador2 = 0
  contador2 = int(contador2)
  print(contador)
  while contador2<=contador:
   print(cursos[posicao])
  posicao = posicao +1   
  • Didn’t my answer to your previous question help you? I asked you if you were going to do this. You don’t need to use urllib if you use requests

  • Yes it helped, I even gave it better

  • explain to me better what you want the program to do

  • I need to print out how many candidates are listed in each course. In this case I think the logic is right. The problem is last line that is not running. It should run as soon as the "while" ends, and this list of " courses" is reduced as there are many. If you open this url that is in the request you will understand a little better what I am saying. See that you have the course and how many candidates are listed there.

  • But you just want to know how many candidates there are? $150 in this case? I just don’t understand why so much code to do it, in my other answer does it, just change the url

  • not only knowing how many there are. I need to print each course as many times as have candidates listed on it. For example in this I need to print 150 times the name of the course, what would be done walking in this vector I called courses, only that the variable position is not being changed at the end of the print, to be able to pass to the next course

  • Miguel , really I was flying hard. Your other question saved everything , Very Thanks

  • You’re welcome to it

Show 3 more comments

2 answers

1

The position is not being incremented in the loop of the while

while contador2 <= contador:
    print(cursos[posicao])
    posicao = posicao +1

0

First of all I suggest you follow the pep8 style guide (https://wiki.python.org.br/GuiaDeEstilo) recommending 4 spaces for indentation.

Specifically about your problem: Change your While block (last block before the line you mention) to the following:

while contador2<=contador:
    print ('contador: ', contador, 'contador2: ' contador2)

And you will see that actually you are entering an infinite loop, because the variable "counter" will always have the value of 5 and the variable "counter2" the value 0 (zero).

Fix that while and the line that increases the position will be reached.

Again: Follow style convention, indentation with 4 spaces; ALWAYS.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.