Reputation: 37
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
Reputation: 2683
There are 2 mistakes
class
with class_
due to it being a reserved namefor number in bonus_list:
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
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
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