Issue
Good afternoon community! I need help writing a parser, I'm just starting to program in Python 3, maybe I'm missing something. The task is this: The site has a table with football teams, using Requests and BeautifulSoup I was able to get the source code of this table into the firsttable variable, the print command normally displays all the data I need, but when I try to display it in a list of the form:
10:00 Team 1 Team 2
11:00 Team 3 Team 4
12:00 Team 5 Team 6
And so on, I can only get the first value from the list, I tried to use the While loop (for example, While i <10), it repeats to me the first value from the table 10 times, but does not pars the remaining ones. What am I doing wrong?
def get_data(html):
soup = BeautifulSoup(html, 'lxml')
firsttable = soup.findAll('table', class_='predictionsTable')[0]
print(firsttable) #Here, all the data that I need is displayed in the console as html source
for scrape in firsttable:
try:
hometeam = scrape.find('td', class_='COL-3').text
except:
hometeam = 'Hometeam Error'
try:
awayteam = scrape.find('td', class_='COL-5').text
except:
awayteam = 'Away Team Error'
try:
btts = scrape.find('td', class_='COL-10').text
except:
btts = 'BTTS Score Error'
datenow = str(datetime.date.today())
print(datenow,hometeam,awayteam,btts)
Solution
The loop for scrape in firsttable
only has one iteration, of the entire table content, which is why you are finding only the first row. Instead of using a loop I would recommend the find_all
method. This worked for me:
url = 'https://www.over25tips.com/both-teams-to-score-tips/'
soup = BeautifulSoup(requests.get(url).content, 'lxml')
firsttable = soup.findAll('table', class_='predictionsTable')[0]
hometeams = [x.text for x in firsttable.find_all('td', {'class': 'COL-3 right-align'})]
awayteams = [x.text for x in firsttable.find_all('td', {'class': 'COL-5 left-align'})]
btts = [x.text for x in firsttable.find_all('td', {'class': 'COL-10 hide-on-small-only'})]
datenow = str(datetime.date.today())
for i in range(len(hometeams)):
print(datenow, hometeams[i], awayteams[i], btts[i])
Answered By - alec
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.