André Lima
André Lima

Reputation: 37

What I'm doing wrong with this webscraping code?

I'm having a problem in trying to do a webscraping. I'm not very used to programming, so I really don't know what I'm doing wrong(but I have some basic knowledge). I'm trying to do the webscraping with python and beautiful soup. Here is the code

import requests
from bs4 import BeautifulSoup
URL = 'http://www.lotece.com.br/v2/'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(class = 'dataResultado')
data_name =  soup.find(class = "data branco")
bonus_list = soup.find_all(class = "premio")
number = soup.find(class = "numeros")

for number in bonus_list    
    print(number.prettify())

the problem when I try to compile is about synthax. Here is the output:

 lotecepx.py", line 6
    results = soup.find(class = 'dataResultado')
                        ^
SyntaxError: invalid syntax
  File "c:/Users/pvictorml/Documents/lotecepx.py", line 6
    results = soup.find(class = 'dataResultado')
                        ^
SyntaxError: invalid syntax

Upvotes: 2

Views: 78

Answers (3)

Nic Wanavit
Nic Wanavit

Reputation: 2683

There are 2 mistakes

  1. you need to replace class with class_ due to it being a reserved name
  2. you forgot a colon after for loop declaration for number in bonus_list:
  3. numeros class doesnt exist (at least not within "premio")

here is an example of what a working code could look like

import requests
from bs4 import BeautifulSoup
URL = 'http://www.lotece.com.br/v2/'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(class_ = 'dataResultado')
data_name =  soup.find(class_ = "data branco")
bonus_list = soup.find_all(class_ = "numeros")

for number in bonus_list:   
    print(number.prettify())

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 98861

As @reinstate-monica answered, class is a reserved word in python. You can also use:

numeros = soup.find_all("div", {"class" : 'numeros'})
for numero in numeros:
    print(numero.text)

9689
3589
2722
...

Upvotes: 2

Lord Elrond
Lord Elrond

Reputation: 16002

class is a reserved keyword. you can't use keywords as variable names, or as keywords in a function call.

BeautifulSoup works around this by using class_ instead:

bonus_list = soup.find_all(class_="premio")

Upvotes: 2

Related Questions